Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Check if player is not moving

Discussion in 'Scripting' started by Vexer, Mar 8, 2018.

  1. Vexer

    Vexer

    Joined:
    Feb 24, 2016
    Posts:
    187
    Hey guys i've been having alot of problems with my animations.. how can i check if my character is not moving and if it's not moving how can i apply The idle animation?? (When i try to add a rigidbody2d to the script it gives alot of errors aswell..)
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.  
    9.     public float playerSpeed;
    10.  
    11.     public bool Up;
    12.     public bool Left;
    13.     public bool Right;
    14.     public bool Down;
    15.     public bool Idle;
    16.  
    17.     private Animator myAnimator;
    18.  
    19.     // Use this for initialization
    20.     void Start()
    21.     {
    22.         myAnimator = GetComponent<Animator>();
    23.         playerSpeed = 1f;
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         if (Input.GetKey("w"))//Press "W" key to move up on the Y AXIS
    29.         {
    30.             transform.Translate(0, playerSpeed * Time.deltaTime, 0);
    31.             Up = true;
    32.             Down = false;
    33.             Left = false;
    34.             Right = false;
    35.             Idle = false;
    36.         }
    37.  
    38.         if (Input.GetKey("s"))//Press "S" key to move down on the Y AXIS
    39.         {
    40.             transform.Translate(0, -playerSpeed * Time.deltaTime, 0);
    41.             Down = true;
    42.             Right = false;
    43.             Left = false;
    44.             Up = false;
    45.             Idle = false;
    46.         }
    47.  
    48.         if (Input.GetKey("a"))//Press "A" key to move left on the X AXIS
    49.         {
    50.             transform.Translate(-playerSpeed * Time.deltaTime, 0, 0);
    51.             Left = true;
    52.             Down = false;
    53.             Right = false;
    54.             Up = false;
    55.             Idle = false;
    56.         }
    57.  
    58.         if (Input.GetKey("d"))//Press "D" key to move right on the X AXIS
    59.         {
    60.             transform.Translate(playerSpeed * Time.deltaTime, 0, 0);
    61.             Right = true;
    62.             Up = false;
    63.             Left = false;
    64.             Down = false;
    65.             Idle = false;
    66.         }
    67.  
    68.         if (Up) myAnimator.SetBool("Up", true);
    69.         else if (Down) myAnimator.SetBool("Down", true);
    70.         else if (Right) myAnimator.SetBool("Right", true);
    71.         else if (Left) myAnimator.SetBool("Left", true);
    72.         else if (Idle) myAnimator.SetBool("Idle", true);
    73.     }
    74. }
    75.  
     
  2. ThermodynamicsMakesMeHot

    ThermodynamicsMakesMeHot

    Joined:
    Feb 14, 2015
    Posts:
    224
    Well you need to add a rigidbody then you can check the magnitude and velocity to determine if its moving and what direction.

    Code (csharp):
    1.  
    2. Rigidbody player = GetComponent<Rigidbody>();
    3. Vector3 vel = player.velocity;
    4. if (vel.magnitude = 0) {
    5.      // do idle animations
    6. }
    7.  
    Code (csharp):
    1.  
    2. Rigidbody2D player = GetComponent<Rigidbody2D>();
    3. Vector2 vel = player.velocity;
    4. if (vel.magnitude = 0) {
    5.      // do idle animations
    6. }
    7.  
    What is the error you get when you add the rigidbody?
     
    GamdineProductions likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could add a 'moving' bool, or use GetAxis Horizontal/Vertical. With the bool, just put it equal to to true in each of your statements. With the get axis if either is not zero, you know you're moving.
    .. or you could check moving with :
    Code (csharp):
    1. if(!right && !left && !up && !down) //  == idle
    Edit: just noticed you already have an 'idle' bool. You could set that to 'true' at the beginning of that code (because any movement sets it false). You could also get rid of the idle bool and simply put "else { /* idle */ }" , as if it's not one of those 4 directions, it must be 'idle'. Lots of options ;)

    The axes are a lot easier; this code looks familiar to something I wrote up an example for once. :)

    Hope that helps.
     
  4. amitDklein

    amitDklein

    Joined:
    Apr 10, 2017
    Posts:
    20
    You can use Transform.hasChanged

    Code (CSharp):
    1.         if (!this.transform.hasChanged)
    2.         {
    3.             print("Player is not moving");
    4.         }
    5.         transform.hasChanged = false;
     
    Forg0tten and GameDevMark like this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,843
    I would just save the current position/rotation at the very end of Update. Write a method to compare the current position/rotation against the previously saved position/rotation. Pretty simple.
     
    Yoreki likes this.