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

Animations not working after multiple tries

Discussion in 'Animation' started by plusplusgames, Aug 13, 2022.

  1. plusplusgames

    plusplusgames

    Joined:
    Jul 26, 2021
    Posts:
    64
    Hello,

    I have been trying for a while to get a working animator player controller. I have watched multiple tutorials and other stuff but I can not get past the idle animation.

    Due to me making a multiplayer game I have to use parameters and I cannot use Blend Trees. Everything is rigged up using Mixamo. I simply need to play my idle animation and my run animation from script


    Here is my current player controller



    And Here is my player controller script

    Code (CSharp):
    1.    [SerializeField] private float moveSpeed;
    2.     [SerializeField] private float runSpeed;
    3.  
    4.     private Vector3 moveDirection;
    5.     private Vector3 velocity;
    6.  
    7.     [SerializeField] private bool isJumping = false;
    8.     [SerializeField] private bool isGrounded = true;
    9.     [SerializeField] private float groundCheckDistance;
    10.     [SerializeField] private LayerMask groundMask;
    11.     [SerializeField] private float gravity;
    12.  
    13.     [SerializeField] private float jumpHeight;
    14.  
    15.     public CharacterController controller;
    16.     public Animator m_Animator;
    17.  
    18.  
    19.  
    20.  
    21.     void Start()
    22.     {
    23.         controller = GetComponent<CharacterController>();
    24.         m_Animator = GetComponentInChildren<Animator>();
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         Move();
    30.     }
    31.  
    32.     private void Move()
    33.     {
    34.         isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
    35.  
    36.         if(isGrounded && velocity.y < 0)
    37.         {
    38.             velocity.y = -2f;
    39.         }
    40.  
    41.         float moveZ = Input.GetAxis("Horizontal");
    42.  
    43.         moveDirection = new Vector3(0, 0, moveZ);
    44.         moveDirection = transform.TransformDirection(moveDirection);
    45.  
    46.         moveDirection *= runSpeed;
    47.  
    48.         if (isGrounded)
    49.         {
    50.             if (Input.GetKeyDown(KeyCode.Space) | (Input.GetKeyDown(KeyCode.W)) | (Input.GetKeyDown(KeyCode.UpArrow)))
    51.             {
    52.                 Jump();
    53.             }
    54.  
    55.             else if (moveDirection == Vector3.zero && isGrounded)
    56.             {
    57.                 moveSpeed = 0;
    58.                 Idle();
    59.             }
    60.         }
    61.  
    62.  
    63.         if (moveDirection != Vector3.zero)
    64.         {
    65.             Run();
    66.         }
    67.  
    68.  
    69.         moveDirection *= moveSpeed;
    70.  
    71.         controller.Move(moveDirection * Time.deltaTime);
    72.  
    73.         velocity.y += gravity * Time.deltaTime;
    74.         controller.Move(velocity * Time.deltaTime);
    75.     }
    76.  
    77.  
    78.     private void Idle()
    79.     {
    80.  
    81.     }
    82.  
    83.  
    84.     private void Run()
    85.     {
    86.  
    87.         moveSpeed = runSpeed;
    88.     }
    89.  
    90.     private void Jump()
    91.     {
    92.         velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    93.     }
    94.