Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by euanross400 · Aug 01, 2021 at 09:04 PM · animationif-statements

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
         }
     }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image euanross400 · Aug 02, 2021 at 03:14 PM 0
Share

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.

avatar image Anonymous620 euanross400 · Aug 03, 2021 at 07:39 AM 0
Share

Ok i'll fix that then

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image euanross400 · Aug 01, 2021 at 09:53 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

302 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Don't play another animation if a specific animation is already playing? 0 Answers

Detect if a variable is a Boolean? 1 Answer

Playing the right animation when running and not running 0 Answers

How do I modify this script to use my character's animations while moving its character controller? Among other things. 0 Answers

Mecanim animation keeps running infinite 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges