我永远不会进去(Physics.Raycast(ray,out hit,Mathf.Infinity,touchInputMask)

问题描述 投票:0回答:2

我想要做的是当用户触摸他可以移动它的对象时,特别是在屏幕上用多点触控手指触摸它

我也尝试用鼠标点击这个光线,但仍然有同样的问题

为此,我在更新时使用此代码

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;
             }
         }
     }
   }
 }

编辑

屏幕截图

enter image description here

c# unity3d raycasting
2个回答
1
投票

我根据评论中提供的信息写这个答案。

为了让Physics.Raycast正常工作,你首先需要在你希望你的Collider碰撞的物体上使用Raycast。如果你的对象没有Collider那么你的Raycast将不会返回任何东西(因为它没有与任何东西碰撞)。

ColliderRigidbody不是一回事,Collider通常用于碰撞检测,并且经常被Rigidbody用来处理基于物理的碰撞响应。具有Rigidbody的物体不会与Raycast碰撞,除非该物体也有Collider

PhysicsRigidBody将仅与3D碰撞者合作。 Physics2DRigidBody2D用于处理2d物理,你不能混合使用2D物理和3d物理组件,因为它们在Unity中不会相互配合。

LayerMask是一个工具,用于确定要处理的Collision图层,其名称下的每个对象都有一个标记和一个图层。这用于确定您想要此对象的碰撞层。如果检查物理设置,则可以确定哪个图层相互碰撞/触发。

因此,解决方案是添加一个名为TouchableObject的新图层,然后将任何想要“可触摸”的对象分配给该图层,并为其提供相应的Collider(代码中的2d Collider或3d,您需要一个3D对撞机,或者将你的Physics.Raycast电话转换为Physics2D.Raycast)。在你的Touch脚本中,你将使LayerMask只针对该层。


0
投票

我能够像这样解决问题

            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的帮助

© www.soinside.com 2019 - 2024. All rights reserved.