Unity android 忽略游戏对象拖放之间的碰撞
Unity android Ignore collision between gameObjects drag and drop
我希望能够将 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)中的所有约束(冻结位置和冻结旋转)附加到对象的刚体。
我希望能够将 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)中的所有约束(冻结位置和冻结旋转)附加到对象的刚体。