- Home /
How do I detect when my player is moving in unity 2d?
I am trying to make a top down game with movement like in the game Binding of Isaac. I want my player to move all 4 directions facing one way, but when he shoots he faces the direction of where he is shooting, not the direction he is moving towards. I can figure most parts out, but I am not sure how to detect when he is moving in ANY direction so I can set a float "speed" to how fast he is moving and make it in the player animator controller if the speed is greater than 0.1 he will play a moving animation. I know I haven't explained it very well, so I'll just say I am trying to figure out how to set a float "speed" to however fast the player is moving, and it doesn't matter what direction. Anyone have any answers? Thanks!
Answer by Jack-Mariani · Apr 30, 2019 at 06:32 PM
You may check the direction of a Rigidbody2D by checking its velocity.
ThisRigidbody2D.velocity; //catch the velocity, as a vector
ThisRigidbody2D.velocity.magnitude; //calculate the speed, always equal or more than 0
To let the trasform follow the rigidbody you may apply this:
[RequireComponent(typeof(Rigidbody2D))]
public class Rigidbody2DLookAt : MonoBehaviour
{
//to decide the minimal speed, if the rigidbody is moving slower than that we stop
public float speedThreshold = 0.1f;
//the offset if you need to adjust the rotation
public float offset = 90f;
//the rigidbody
private Rigidbody2D _rigidbody2D;
//cache the rigidbody at start (or awake)
private void Start() { _rigidbody2D = GetComponent<Rigidbody2D>(); }
private void Update()
{
//gets the velocity as a vector
var velocity = _rigidbody2D.velocity;
//stops following the rigidbody rotation if 'moving too slow
if (velocity.magnitude <= speedThreshold) return;
//calculate the angle and rotate the transform accordingly
var angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + offset));
}
}
Thanks for the answer. So if I am using a rigidbody2D, how exactly can I say "if the velocity is greater than 0 then....."?
I have tried using rb.velocity.magnitude but it just doesn't seem to work ;(
rigidbody.velocity.magnitude sends back only the speed without the direction.
Try applying this:
var velocity = rigidbody.velocity;
if(velocity.magnitude > 0.1)
this.transform.rotation = Quaternion.LookRotation(velocity, Vector2.right);
Otherwise tomorrow I'll check other solutions.
**EDIT: I've just updated the answer. The code in the answer works better for 2d. You may ignore this.
I've adjusted the answer after your comment and tested it on a scene. It is working on my side. Let me know if it works for you too.
Your answer
Follow this Question
Related Questions
Add acceleration and deceleration for top down 2D movement. 2 Answers
Character movement problem, floats away 1 Answer
(2D Movement) How do I make my sprite move up, down, left, and right, without moving diagonal? 1 Answer
Top-down movement in 2D 1 Answer
2D Directional top down movement,Topdown 2d Directional Movement 0 Answers