Search Unity

Movement & idle script variables resetting

Discussion in 'Scripting' started by LordFluffyJr, Feb 22, 2018.

  1. LordFluffyJr

    LordFluffyJr

    Joined:
    Feb 21, 2018
    Posts:
    2
    I'm currently using a PlayerController.cs script to control player movement. I am trying to have it so when they player stops moving the script saves a variable called lastMoveX and lastMoveY so the animator knows which way to keep the player model facing. When I play the game it seems that the lastMoveX and lastMoveY are resetting to 0,0 at the end of every frame, even if the player isn't moving.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public float moveSpeed;
    9.     private Vector2 axisMovement;
    10.     private bool playerMoving;
    11.     private Vector2 lastMove;
    12.     private Animator anim;
    13.  
    14.     void Start()
    15.     {
    16.         anim = GetComponent<Animator>();
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         playerMoving = false;
    22.  
    23.         if (Input.GetAxisRaw("Horizontal") != 0){ //Horizontally
    24.        
    25.             transform.Translate(new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f));
    26.             playerMoving = true;
    27.             lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    28.  
    29.         }
    30.  
    31.  
    32.         if (Input.GetAxisRaw("Vertical") != 0f) { //Move Vertically
    33.        
    34.             transform.Translate(new Vector2(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime));
    35.             playerMoving = true;
    36.             lastMove = new Vector2 (0f, Input.GetAxisRaw("Vertical"));
    37.         }
    38.  
    39.         anim.SetFloat ("moveX", Input.GetAxisRaw("Horizontal"));
    40.         anim.SetFloat ("moveY", Input.GetAxisRaw("Vertical"));
    41.         anim.SetFloat ("lastMoveX", Input.GetAxisRaw("Horizontal"));
    42.         anim.SetFloat ("lastMoveY", Input.GetAxisRaw("Vertical"));
    43.         anim.SetBool ("playerMoving", playerMoving);
    44.            
    45.      
    46.     }
    47.  
    48. }
    49.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Well, it looks like you're setting a "lastMove" variable onlyif there is motion, but then you're setting `anim.SetFloat` for lastMoveX and lastMoveY to the input, regardless of whether it's 0 or not. Should your SetFloats for lastMoveX/Y only be called if motion isn't 0?
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You should cache your Input vertical and Input horizontal instead of rereading.
    float vert = Input.GetAxisRaw("Vertical");
    And you can get lastMoveX from lastMove.x
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712