c# - unity android 忽略游戏对象拖放之间的碰撞

标签 c# android unity-game-engine

我希望能够将 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。我有 4 个层到 gameObjects,我试图调用 Physics.IgnoreLayerCollision(9, 10); 在附加到主相机的脚本上(在启动时运行)但是它没有帮助。

最佳答案

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

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

关于c# - unity android 忽略游戏对象拖放之间的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37896757/

相关文章:

c# - IHostedService 的多种实现

android - 拒绝对先前失败的类com.google.android.gms.location.LocationService进行重新初始化

android - 支持的设备极低

c# - 在 WCF REST 服务中获取原始请求 url

c# - 比较 2 个列表后获取缺失值

c# - 将当前用户添加到 NLog 输出

java - 通过 java 套接字传输大文件

c# - 按下 mouse1 时播放声音,松开时停止声音

c# - 在 Unity 中使用泛型作为变量类型

c# - 从另一个脚本 C# 调用 IEnumerator 或变量