Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question I need help figuring out what's wrong with my transitions that's making my character stop.

Discussion in 'Animation' started by One_Learning_Man, Mar 2, 2022.

  1. One_Learning_Man

    One_Learning_Man

    Joined:
    Sep 30, 2021
    Posts:
    81
    Current Behavior: My character when doing very sharp turns: forward straight to backward, left and straight to right (and their opposites), the character stops for half a second and then starts running the idle -> run transition sequence again.

    Expected Behavior: I expect the character to stay in the running state when quickly pressing A (left) and D (right). It shouldn't be stopping nor should it be redoing the whole idle to run transition sequence.

    Solution Attempts: Keeping the character permanently in the run state yields no problem when spamming left and right keys. Tinkering with the RUN -> IDLE transition actually returned some results where it affected the timing of how long it takes for my character to resume running again. I would have thought it was idle to run to be honest. In looking at debug logs, my character's vector magnitude stayed > 0 when pressing keys so I'm not sure if it's the code.

    Here is the code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     public CharacterController characterController;
    8.     public Transform cam;
    9.  
    10.     public Animator animatorController;
    11.  
    12.     public float speed = 6f;
    13.     public float turnSmoothTime = 0.1f;
    14.     private float turnSmoothVelocity;
    15.  
    16.     private void Awake()
    17.     {
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.         Cursor.visible = false;
    20.     }
    21.  
    22.     private void Start()
    23.     {
    24.         animatorController = GetComponent<Animator>();
    25.     }
    26.  
    27.     private void FixedUpdate()
    28.     {
    29.         float horizontal = Input.GetAxisRaw("Horizontal");
    30.         float vertical = Input.GetAxisRaw("Vertical");
    31.         Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
    32.  
    33.         if(direction.magnitude > 0f)
    34.         {
    35.             //Returns the desired angle from the x and y vectors in radians. Use Rad2Deg to convert it to degrees.
    36.             float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
    37.             float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
    38.             transform.rotation = Quaternion.Euler(0f, angle, 0f);
    39.  
    40.             Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
    41.             characterController.Move(moveDirection.normalized * speed * Time.deltaTime);
    42.             animatorController.SetBool("RunState", true);
    43.             Debug.Log(direction);
    44.         }
    45.         else
    46.         {
    47.             animatorController.SetBool("RunState", false);
    48.         }
    49.     }
    50. }
    51.  
    upload_2022-3-1_21-52-7.png

    upload_2022-3-1_21-52-17.png

    upload_2022-3-1_21-52-28.png