Есть ли способ получить доступ только к одному коллайдеру, когда есть много коллайдеров с одинаковым тегом? - PullRequest
0 голосов
/ 08 февраля 2020

У меня есть скрипт, который перемещает объект, к которому вы прикасаетесь, на позицию вашего пальца, он основан на теге, поэтому, когда я касаюсь объекта с тегом, все объекты с одинаковым тегом перемещаются в эту позицию. Есть ли способ заставить двигаться только тот, кого я касаюсь?

The Script

 {
     void FixedUpdate()
     {
         if (Input.touchCount > 0)
         {            
             RaycastHit2D hitInformation = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position),Camera.main.transform.forward);            
                 if (hitInformation.collider.gameObject.tag == "RocketPrefab")
                 {                    
                     Vector3 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                     touchPosition.z = -4;
                     transform.position = touchPosition;
                     Debug.Log(touchPosition);

                 }                            
         }
     }
 } ```

Ответы [ 2 ]

3 голосов
/ 08 февраля 2020

Вы можете получить доступ к объекту, к которому ваш Raycast касается, с помощью hitInformation.collider.gameObject.

Из кода, который я вижу, я думаю, что это должно работать:

     void FixedUpdate()
     {
         if (Input.touchCount > 0)
         {            
             RaycastHit2D hitInformation = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position),Camera.main.transform.forward);            
                 if (hitInformation.collider.gameObject.tag == "RocketPrefab")
                 {                    
                     Vector3 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                     touchPosition.z = -4;
                     hitInformation.collider.gameObject.transform.position = touchPosition;
                     Debug.Log(touchPosition);
                 }                            
         }
     }
1 голос
/ 08 февраля 2020

Я предполагаю, что скрипт находится на родительском объекте всех игровых объектов "RocketPrefab", а затем вы перемещаете их все со строкой transform.position = touchPosition;

С этим предположением ... чтобы получить указанную c тот, который ударил raycast, вам нужно будет отредактировать скрипт, чтобы переместить collider.gameObjct.transform не только transform

Edited FixedUpdate

void FixedUpdate()
{
    if (Input.touchCount > 0)
    {            
        RaycastHit2D hitInformation = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position),Camera.main.transform.forward);            
        if (hitInformation.collider.gameObject.tag == "RocketPrefab")
        {                    
            Vector3 touchPosition = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            touchPosition.z = -4;
            hitInformation.collider.gameObject.transform.position = touchPosition; // Note this line and how it targets the specific transform
            Debug.Log(touchPosition);
        }                            
    }
}
...