- Home /
RaycastHit2D affect all objects with that specified tag. How to make affect only the touched object?
RaycastHit2D affect all objects with that specified tag. How to make it affect only the touched object? I have this script attached to a Prefab that has the tag "Unbreakable". I have many instancies of this Prefab at the same time. I want that only the one I touch rotate. This is possible?
The code I'm using is this:
private void RotateOnTouch()
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position), Vector2.zero);
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began && hit.collider != null)
{
if (hit.collider.gameObject.CompareTag("Unbreakable"))
{
RotateUnbreakable();
RandomizeSide();
}
}
}
Answer by trapazza · Sep 12, 2018 at 09:03 PM
What about https://docs.unity3d.com/ScriptReference/Physics2D.GetRayIntersection.html ?
"Cast a 3D ray against the colliders in the scene returning the first collider along the ray."
Your answer
Follow this Question
Related Questions
Raycasting2D to Compare Tag of GameObject 1 Answer
Check if Object is in a Position? (Using Compare tag) 1 Answer
Get the name of the collided object 2 Answers
Raycast2D only detects objects directly over origin 0 Answers
RaycastHit2D not detecting a specific gameobject in a for (for path smoothing in A*) 1 Answer