[SOLVED] How to determinate object direction

How do I get direction in which object is moving in 2d (top down view) so i can shoot ray forward from the object so that the ray will hit obstacle that the object would hit.

Do you have the object moving currently? If so, is it using physics or translation? If physics, debug.log the x and y velocity to see where “forward” is in your game. My guess in your 2d top down game “Forward” is Up, or North, from the player Which means a positive Y value will be Forward/Up/North or however you want to describe it. If you are using transform.position to move, just debug.log the object’s transform.position.x or .y (or Z if necessary) to find which way it moves.

Im using transform.Translate to move. I domt understand what’s u mean by using transform.position.x/y It only gives me information about position, and not current direction (objects moves with constant speed) i need to store direction of the object in some variable

transform already has these helpers like

so you can raycast for example towards “transform.up” or other directions, depending which way is forward in your gameobject.

This

I use transform.right as the front of my sprite for all my 2d games.

1 Like

The thing is i want the object to boucne around and i always want it to have its forward in actuall direction its heading (I dont want to be using physics).

then you could try calculating direction with something like:

Vector3 currentDirection = (currentPosition-previousPosition).normalized;
3 Likes

Thanks! It worka nie. One last question, hos do i tag post as solved or delete It?


Click thread tools then add [SOLVED]