Unity android忽略gameObjects拖放之间的冲突

时间:2016-06-18 12:08:01

标签: c# android unity3d unity5

我希望能够将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时,它会在它们发生碰撞时推送它。我有gameObjects的4层,我试图在附加到主摄像头的脚本上调用Physics.IgnoreLayerCollision(9, 10);(在启动时运行),但它没有帮助。

1 个答案:

答案 0 :(得分:1)

您可以在编辑 - &gt;中禁用图层之间的互动。项目设置 - &gt;物理菜单。

另一个选项可以是(如果你不希望你的物体受到物理相互作用的影响)来检查所附的Rigidbody中所有尺寸(x,y和z)的所有约束(冻结位置和冻结旋转)对象。

相关问题