- Home /
How to raycast at the direction my object is facing ?
Hi, i'm making a top-down 2d game and i want enemies (ships) to fire only when facing the target, so how can i make the Ray actually go in the direction the ship's facing ? I'm sure it has something to do with its z rotation but i can't do it right. If anyone can help i'll be really appreciated !
Answer by ShadoX · Aug 10, 2019 at 07:52 PM
Depending on how you implement it (assuming the script's on the ship) .. try using https://docs.unity3d.com/ScriptReference/Transform-forward.html as direction for the direction the Ray shoots in https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
$$anonymous$$y problem is in the direction vector, when the ship rotates the ray keeps the same down direction ( i believe) In the ship's script i wrote : RaycastHit2D hit = Physics2d.Raycast( transform.position, direction, 5f, layer.mask("target") )
But i don't know how to set the value for "direction"