- Home /
Check if player is moving but not by checking velocity.
Hello I am making a 2D top down game and I have been having problems with playing a running animation (there is only one running animation). First I tried checking the velocity to see if the player was moving but It wouldn't work (which I now know was because my player does not move with any force so it has no velocity). So I tried an IF statement: if (Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("s") || Input.GetKeyDown("d")) { // Player is moving animator.SetBool("isRunning", true); }
But this was inconsistent sometimes when moving diagonally and the other IF statement for when the player was not moving would activate and even thought two keys were still being held "isRunning" is not set to true.
Other IF:
if (Input.GetKeyUp("w") || Input.GetKeyUp("a") || Input.GetKeyUp("s") || Input.GetKeyUp("d"))
{
// Player is not moving
animator.SetBool("isRunning", false);
}
If anyone has a better idea of how to do this please leave a comment.
Answer by Anonymous620 · Aug 02, 2021 at 01:36 AM
this is basically a script that compares the last position to the position before that
public class MovementCheck : MonoBehaviour
{
Vector3 firstPosition;
Vector3 lastPosition;
bool isMoving;
// Start is called before the first frame update
void Start()
{
firstPosition = this.transform.position;
}
// Update is called once per frame
void FixedUpdate()
{
lastPosition = this.transform.position;
if(lastPosition != firstPosition)
{
isMoving = true;
}
else
{
isMoving = false;
}
print(isMoving);
firstPosition = lastPosition;
}
}
Thank you for your answer but unfortunately the script almost works. isMoving is set to true but then its set to false every frame even when the player is still moving. But I tried FixedUpdate and now its works.
Answer by Pangamini · Aug 01, 2021 at 09:23 PM
Depends. Do you want the animation to run when the input keys are held, even if you are running into a wall? Do you want the animation to play even if something other than key input is pushing your character? Do you want it to play when your character is not grounded, mid-air? Answering these questions may get you closer to your solution. Since we don't see any of your code that actually moves the character, it's hard to tell. Perhaps a very simple solution would be to obtain the velocity somehow. Just because there is no rigidbody, it doesn't mean your object doesn't have a velocity, you just haven't defined it properly. One way would be to compare the position from previous FixedUpdate and divide it by fixedDeltaTime. Or, perhaps, you calculate the velocity from the input keys pressed. If you are not using forces, then you are using your own code to move the character, so the code must know the velocity somewhere
Sorry if I was a bit vague, my player is top down when WASD is pressed the bool parameter is set to true and the animation plays.
When I say it only has one animation for running I mean that my sprite is a character looked at from the side and when they move left or right I flip the players sprite with the scale.
It also seems I forgot to say that the player does have a rigidbody2D.
PlayerMovement script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
Vector2 movement;
public Animator animator;
// Update is called once per frame
public void Update()
{
//Input
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if (Input.GetKeyDown("a"))
{
transform.localScale = new Vector3(-1f, 1f, 1f);
}
if (Input.GetKeyDown("d"))
{
transform.localScale = new Vector3(1f, 1f, 1f);
}
if (Input.GetKeyDown("w") || Input.GetKeyDown("a") || Input.GetKeyDown("s") || Input.GetKeyDown("d"))
{
// Player is moving
animator.SetBool("isRunning", true);
}
if (Input.GetKeyUp("w") || Input.GetKeyUp("a") || Input.GetKeyUp("s") || Input.GetKeyUp("d"))
{
// Player is not moving
animator.SetBool("isRunning", false);
Debug.Log("Player is not moving");
}
/*
// Debug.Log(rb.velocity.magnitude);
if (rb.velocity.magnitude > 0)
{
// Player is moving
animator.SetBool("isRunning", true);
Debug.Log("Player is moving");
}
*/
}
private void FixedUpdate()
{
//Movement
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
/*
if (rb.velocity.magnitude > 0)
{
// Player is not moving
animator.SetBool("isRunning", false);
Debug.Log("Player is not moving");
}
*/
}
}
Your answer
