gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:15:40 29 4
gpt4 key购买 nike

我希望能够将 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com