【发布时间】:2017-11-17 00:25:45
【问题描述】:
我正在使用光线投射来检测与可控层上的对象的碰撞。由于某种原因,不在可控层上的对象也会受到影响。这是我调用光线投射的代码。
if (Input.touches.Length > 0)
{
RaycastHit hit;
if (Physics.Raycast(firstPersonCamera.ScreenPointToRay(Input.GetTouch(0).position), out hit, controllableLayer))
{
Debug.Log("Object hit");
Debug.Log(hit.transform.gameObject.name);
Touch touch = Input.GetTouch(0);
GameObject objectHit = hit.transform.gameObject;
if (dragging)
{
Vector3 cursorScreenPoint = new Vector3(touch.position.x, touch.position.y, screenPoint.z);
Vector3 cursorPosition = firstPersonCamera.ScreenToWorldPoint(cursorScreenPoint) + offset;
objectHit.transform.position = cursorPosition;
}
// Handle finger movements based on touch phase.
switch (touch.phase)
{
// Record initial touch position.
case TouchPhase.Began:
screenPoint = firstPersonCamera.WorldToScreenPoint(objectHit.transform.position);
offset = objectHit.transform.position - firstPersonCamera.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, screenPoint.z));
dragging = true;
break;
// Determine direction by comparing the current touch position with the initial one.
case TouchPhase.Moved:
break;
// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
dragging = false;
break;
}
}
else
{
Debug.Log("Nothing hit");
}
}
这里也是我声明图层蒙版的地方
void Awake()
{
firstPersonCamera = GameObject.Find("First Person Camera").GetComponent<Camera>();
controllableLayer = LayerMask.NameToLayer("Controllable");
Debug.Log(LayerMask.LayerToName(controllableLayer));
}
【问题讨论】: