Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NPC's playing wrong animations

Discussion in 'Scripting' started by Adomitas, Apr 30, 2018.

  1. Adomitas

    Adomitas

    Joined:
    Mar 10, 2018
    Posts:
    12
    Hello, I've been having a problem, which has become quite frustrating. I have made an NPC and its animations. However, the animations played are wrong. When moving right, the moving left animation is played. I'm quite sure the problem is with the code, although not sure where. Everything else associated with animations is correct as far as I'm concerned.

    Here's the script:
    Code (CSharp):
    1. public class NPCMovement : MonoBehaviour
    2. {
    3.  
    4.     public float moveSpeed;
    5.     private Rigidbody2D rb;
    6.     private bool isMoving;
    7.  
    8.     public Vector2 lastMove;
    9.     public Vector2 moveDirection;
    10.  
    11.     public float walkTime;
    12.     private float walkCounter;
    13.     public float waitTime;
    14.     private float waitCounter;
    15.  
    16.     private int walkDirection;
    17.  
    18.     public Animator anim;
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody2D>();
    23.         anim = GetComponent<Animator>();
    24.         waitCounter = waitTime;
    25.         walkCounter = walkTime;
    26.  
    27.         ChooseDirection();
    28.  
    29.     }
    30.  
    31.  
    32.     void Update()
    33.     {
    34.         if (isMoving == false)
    35.         {
    36.             lastMove.x = 0;
    37.             lastMove.y = 0;
    38.         }
    39.  
    40.         if (isMoving == true)
    41.         {
    42.             walkCounter -= Time.deltaTime;
    43.  
    44.  
    45.             switch (walkDirection)
    46.             {
    47.  
    48.  
    49.                 case 0:
    50.                     rb.velocity = new Vector2(0, moveSpeed);
    51.                     lastMove.y = 1;
    52.                     lastMove.x = 0;
    53.                     break;
    54.  
    55.                 case 1:
    56.                     rb.velocity = new Vector2(moveSpeed, 0);
    57.                     lastMove.x = 1;
    58.                     lastMove.y = 0;
    59.                     transform.localScale = new Vector3(1f, 1f, 1f);
    60.                     break;
    61.  
    62.                 case 2:
    63.                     rb.velocity = new Vector2(0, -moveSpeed);
    64.                     lastMove.y = -1;
    65.                     lastMove.x = 0;
    66.                     break;
    67.  
    68.                 case 3:
    69.                     rb.velocity = new Vector2(-moveSpeed, 0);
    70.                     lastMove.y = 0;
    71.                     lastMove.x = -1;
    72.                     transform.localScale = new Vector3(-1f, 1f, 1f);
    73.  
    74.                     break;
    75.  
    76.             }
    77.  
    78.             if (walkCounter < 0)
    79.             {
    80.                 isMoving = false;
    81.                 waitCounter = waitTime;
    82.             }
    83.         }
    84.  
    85.         else
    86.         {
    87.             rb.velocity = Vector2.zero;
    88.  
    89.             waitCounter -= Time.deltaTime;
    90.  
    91.             if (waitCounter < 0)
    92.             {
    93.                 ChooseDirection();
    94.  
    95.             }
    96.         }
    97.         anim.SetFloat("LastMoveX", lastMove.x);
    98.         anim.SetFloat("LastMoveY", lastMove.y);
    99.         anim.SetBool("IsMoving", isMoving);
    100.    
    101.     }
    102.  
    103.     public void ChooseDirection()
    104.     {
    105.         walkDirection = Random.Range(0, 4);
    106.         isMoving = true;
    107.         walkCounter = walkTime;
    108.     }
    109. }
    Thanks in advance :)