- Home /
Is there a way to only acces one collider when there are many colliders with the same tag?
I have a script that moves the object you're touching to the position of your finger, it's based on a tag so when i touch an object with the tag all the objects with the same tag move to that position. Is there a way to separate them?
The script
public class ObjectDrag : MonoBehaviour
{
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);
}
}
}
}
Answer by Batzuga · Feb 07, 2020 at 09:59 PM
I assume the ObjectDrag is a singleton script. Inside the if(hitinformation) you are using transform.position. which moves the object the Object Drag is under. you might want to move the hitInformation.collider.transform.position. which then moves the object you are hitting with the raycast.
Thank you for your response that fixed the issue, but now when i move the object sometimes i get a NullReferenceExpection at line 13(if(hitInformation).collider...) what could be causing that?
Your answer
Follow this Question
Related Questions
How can i shoot 2D ball/bounce of wall using Raycast? 1 Answer
HOW TO ONLY DETECT ANGLES BY COLLIDERS2D FACES 0 Answers
Is my 2D Raycast set up correctly? because it does not seem to hit anything when i use a layer mask. 1 Answer
CircleCast hits emitter with Collider2D 0 Answers
Physics2D.Linecast ignoring walls 1 Answer