我想要做的是当用户触摸他可以移动它的对象时,特别是在屏幕上用多点触控手指触摸它
我也尝试用鼠标点击这个光线,但仍然有同样的问题
为此,我在更新时使用此代码
public LayerMask touchInputMask;
private static List<GameObject> touchList = new List<GameObject>();
public static Dictionary<int, objectst> touchobjects = new
Dictionary<int, objectst>();
private GameObject[] touchesOld;
private RaycastHit hit;
public GUIText Count, IndexLift;
private Vector3 targetPos;
public struct objectst { public Vector3 screenPoint; public Vector3 offset;
}
void Update(){
if (nbTouches > 0)
{
//nbTouches = 5;
//print(nbTouches + " touch(es) detected");
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
for (int i = 0; i < nbTouches; i++)
{
Touch touch = Input.GetTouch(i);
//print("Touch index " + touch.fingerId + " detected at
position " + touch.position);
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out hit,Mathf.Infinity,
touchInputMask))
{
GameObject recipient = hit.transform.gameObject;
print("#### touch hit name object "+recipient.name);
touchList.Add(recipient);
//recipient.;
if (touch.phase == TouchPhase.Began)
{
print("#### tcouh begin");
objectst tempobj = new objectst();
tempobj.screenPoint =
Camera.main.WorldToScreenPoint(recipient.transform.position);
tempobj.offset = recipient.transform.position -
Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x,
touch.position.y, tempobj.screenPoint.z));
touchobjects.Add(touch.fingerId, tempobj);
}
if (touch.phase == TouchPhase.Stationary || touch.phase
== TouchPhase.Moved)
{
print("#### tcouh stationary or moved");
Vector3 curScreenPoint = new Vector3(touch.position.x, touch.position.y,
touchobjects[touch.fingerId].screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + touchobjects[touch.fingerId].offset;
print("#### objet doit être deplacer x = "+curPosition.x+"y = "+curPosition.y);
recipient.transform.position = curPosition;
}
if (touch.phase == TouchPhase.Ended)
{
print("#### tcouh ended");
Vector3 curScreenPoint = new Vector3(touch.position.x, touch.position.y,
touchobjects[touch.fingerId].screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) - touchobjects[touch.fingerId].offset;
recipient.transform.position = curPosition;
}
}
}
}
}
编辑
屏幕截图
我根据评论中提供的信息写这个答案。
为了让Physics.Raycast
正常工作,你首先需要在你希望你的Collider
碰撞的物体上使用Raycast
。如果你的对象没有Collider
那么你的Raycast
将不会返回任何东西(因为它没有与任何东西碰撞)。
Collider
和Rigidbody
不是一回事,Collider
通常用于碰撞检测,并且经常被Rigidbody
用来处理基于物理的碰撞响应。具有Rigidbody
的物体不会与Raycast
碰撞,除非该物体也有Collider
。
Physics
和RigidBody
将仅与3D碰撞者合作。 Physics2D
和RigidBody2D
用于处理2d物理,你不能混合使用2D物理和3d物理组件,因为它们在Unity中不会相互配合。
LayerMask
是一个工具,用于确定要处理的Collision图层,其名称下的每个对象都有一个标记和一个图层。这用于确定您想要此对象的碰撞层。如果检查物理设置,则可以确定哪个图层相互碰撞/触发。
因此,解决方案是添加一个名为TouchableObject的新图层,然后将任何想要“可触摸”的对象分配给该图层,并为其提供相应的Collider
(代码中的2d Collider或3d,您需要一个3D对撞机,或者将你的Physics.Raycast
电话转换为Physics2D.Raycast
)。在你的Touch
脚本中,你将使LayerMask
只针对该层。
我能够像这样解决问题
RaycastHit2D hit = Physics2D.Raycast(new Vector2(Camera.main.ScreenToWorldPoint(touch.position).x, Camera.main.ScreenToWorldPoint(touch.position).y), Vector2.zero, 0f);
if (hit.rigidbody != null)
当然,点击的刚体是我点击的对象
感谢@Eddge的帮助