【发布时间】:2016-06-18 22:03:44
【问题描述】:
我希望能够将gameObject 拖到另一个(2D 游戏)上,以拖放对象我使用此脚本:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Dragger : MonoBehaviour
{
float tempZAxis;
public SpriteRenderer selection;
void Start()
{
}
void Update()
{
Touch[] touch = Input.touches;
for (int i = 0; i < touch.Length; i++)
{
Vector2 ray = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
RaycastHit2D hit = Physics2D.Raycast(ray, Vector2.zero, 100f, (1 << 8 | 1 << 9 | 1 << 10 | 1 << 11));
switch (touch[i].phase)
{
case TouchPhase.Began:
if (hit)
{
selection = hit.transform.gameObject.GetComponent<SpriteRenderer>();
if (selection != null)
{
tempZAxis = selection.transform.position.z;
}
}
break;
case TouchPhase.Moved:
Vector3 tempVec = Camera.main.ScreenToWorldPoint(touch[i].position);
tempVec.z = tempZAxis;
if (selection != null)
{
selection.transform.position = tempVec;
}
break;
case TouchPhase.Ended:
selection = null;
break;
}
}
}
}
问题是我必须将BoxCollider2D 附加到每个gameObject 才能使用RaycastHit2D,现在当我将object1 拖到object2 时,它会在它们碰撞时推动object2。 gameObjects 有 4 层,我尝试在附加到主摄像头的脚本上调用 Physics.IgnoreLayerCollision(9, 10);(在启动时运行),但这没有帮助。
【问题讨论】: