【问题标题】:Unity android Ignore collision between gameObjects drag and dropUnity android忽略游戏对象拖放之间的碰撞
【发布时间】: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);(在启动时运行),但这没有帮助。

【问题讨论】:

    标签: c# android unity3d unity5


    【解决方案1】:

    您可以在 Edit -> Project Settings -> Physics 菜单中禁用图层之间的交互。

    另一个选项可以是(如果您不希望您的对象受到物理交互的影响)检查附加刚体中所有维度(x、y 和 z)的所有约束(冻结位置和冻结旋转)到对象。

    【讨论】:

    • 谢谢,我在物理菜单中禁用了它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多