關於Unity3D中滑鼠移動指定物體的解決方案

來源:互聯網
上載者:User

標籤:觀察   down   ack   osi   csdn   cal   bsp   ==   座標   

一、滑鼠拾取物體的原理

在Unity3D當中,想要在觀察面(Aspect)中拾取物體(有碰撞屬性)的方法一般如下:

1、聲明一個觀察的攝像機、一個從攝像機原點出發的射線Ray以及一個用於檢測碰撞的RaycastHit;

2、將射線Ray定義為從攝像機原點出發並且指向當前滑鼠所在的座標(螢幕座標);

3、定義碰撞RaycastHit為射線Ray與有碰撞屬性的物體的碰撞點。

具體代碼實現如下(C#代碼):

using System.Collections;using System.Collections.Generic;using UnityEngine;public class camera : MonoBehaviour{    public Camera ca;    private Ray ra;    private RaycastHit hit;    // Use this for initialization    void Start()    {    }    // Update is called once per frame    void Update()    {            ra = ca.ScreenPointToRay(Input.mousePosition);            if (Physics.Raycast(ra, out hit))            {                                   }    }}

 

應用一:當滑鼠按住不動時,移動被選定物體隨滑鼠一起移動

using System.Collections;using System.Collections.Generic;using UnityEngine;public class camera : MonoBehaviour{    public Camera ca;    private Ray ra;    private RaycastHit hit;    // Use this for initialization    void Start()    {    }    // Update is called once per frame    void Update()    {        if (Input.GetMouseButton(0))        {            ra = ca.ScreenPointToRay(Input.mousePosition);            if (Physics.Raycast(ra, out hit))            {                hit.collider.gameObject.transform.position = ca.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, hit.collider.gameObject.transform.position.z));            }        }    }}

 

應用二:當滑鼠點擊物體時,物體隨滑鼠一起移動;當滑鼠再次點擊時,放下物體。

using System.Collections;using System.Collections.Generic;using UnityEngine;public class camera : MonoBehaviour {    public Camera ca;    private Ray ra;    private RaycastHit hit;    private int flag = 0;    // Use this for initialization    void Start () {            }        // Update is called once per frame    void Update () {        if (Input.GetMouseButtonDown(0))        {            ra = ca.ScreenPointToRay(Input.mousePosition);            if (Physics.Raycast(ra, out hit))            {               if (flag == 0)                {                    flag = 1;                } else                {                    flag = 0;                }            }        }        if (flag == 1)        {            hit.collider.gameObject.transform.position = ca.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, hit.collider.gameObject.transform.position.z));        }    }}

 出自http://blog.csdn.net/sysujackjiao/article/details/69396274

關於Unity3D中滑鼠移動指定物體的解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.