- Home /
Unity2D, How do i check which way a sprite is facing?
In the game im creating, the enemies can only move forward and apply torque to turn(i know how to do this). What i need to do is find out which way the sprite is facing so i can add logic to decide whether the AI applies torque in the correct direction or moves forward.
We need more infos here (or an image to explain). You could use Quaternion.Angle() to compare the rotation angle between 2 sprites (but in don't think this is what you want) or trace a raycast with Physics.Raycast() to see if your sprite is rotated in front of a another one, etc.
$$anonymous$$$$anonymous$$akesGames has got the idea of what i want, ill uplaod a picture now though.
Answer by TomMakesGames · Mar 19, 2014 at 07:28 PM
Do you actually want to apply torque? Do you need the physics. In many 2D games you may be better off making your objects kinematic and using angles etc.
Anyway, if you DO need the physics, then I think you want to look at using transform.forward. This will return a vector which gives the forward facing direction of your enemy. Compare that to whatever angle is relevant and then apply logic accordingly.
e.g. (pseudo-code)
if enemy's transform.forward is towards the player, keep moving forward. If enemy's transform.forward is not towards player, then turn clockwise to face them.
etc.
Does that help?
@$$anonymous$$$$anonymous$$akesGames Yeah that does help, how do i check if the forward facing direction is towards my hero though? And could you possibly explain the other method of kinematics?
Unfortunately I'm a beginner as well, and I wouldn't be able to come up with a solution off the top of my head and I don't have time at the moment to work on something for you. But it does look like you want the physics route, since you're effectively making asteroids. Stick with that (based on your pic).