Search Unity

Question Unable to transition to other animations on my Character Controller

Discussion in 'Animation' started by s3748997, Oct 12, 2022.

  1. s3748997

    s3748997

    Joined:
    Jul 27, 2022
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     //VAR
    8.     [SerializeField] private float moveSpeed;
    9.     [SerializeField] private float walkSpeed;
    10.     [SerializeField] private float runSpeed;
    11.  
    12.     private Vector3 moveDirection;
    13.     private Vector3 velocity;
    14.  
    15.     [SerializeField] private bool isGrounded;
    16.     [SerializeField] private float groundCheckDistance;
    17.     [SerializeField] private LayerMask groundMask;
    18.     [SerializeField] private float gravity;
    19.  
    20.     [SerializeField] private float jumpHeight;
    21.  
    22.     //REF
    23.     private CharacterController controller;
    24.     private Animator anim;
    25.  
    26.     private void Start()
    27.     {
    28.         controller = GetComponent<CharacterController>();
    29.         anim = GetComponentInChildren<Animator>();
    30.     }
    31.  
    32.     private void Update()
    33.     {
    34.         Move();
    35.     }
    36.  
    37.     private void Move()
    38.     {
    39.         isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
    40.  
    41.         if(isGrounded && velocity.y < 0)
    42.         {
    43.             velocity.y = -2f;
    44.         }
    45.  
    46.         float moveZ = Input.GetAxis("Vertical");
    47.  
    48.         moveDirection = new Vector3(0, 0, moveZ);
    49.         moveDirection = transform.TransformDirection(moveDirection);
    50.  
    51.         if(isGrounded)
    52.         {
    53.             if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
    54.             {
    55.                 Walk();
    56.             }
    57.             else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
    58.             {
    59.                 Run();
    60.             }
    61.             else if (moveDirection == Vector3.zero)
    62.             {
    63.                 Idle();
    64.             }
    65.  
    66.             moveDirection *= moveSpeed;
    67.  
    68.             if (Input.GetKeyDown(KeyCode.Space))
    69.             {
    70.                 Jump();
    71.             }
    72.         }
    73.  
    74.      
    75.  
    76.         controller.Move(moveDirection * Time.deltaTime);
    77.  
    78.         velocity.y += gravity * Time.deltaTime;
    79.         controller.Move(velocity * Time.deltaTime);
    80.     }
    81.  
    82.     private void Idle()
    83.     {
    84.         anim.SetFloat("Speed", 0);
    85.     }
    86.  
    87.     private void Walk()
    88.     {
    89.         moveSpeed = walkSpeed;
    90.         anim.SetFloat("Speed", 0.5f);
    91.     }
    92.  
    93.     private void Run()
    94.     {
    95.         moveSpeed = runSpeed;
    96.         anim.SetFloat("Speed", 1);
    97.     }
    98.  
    99.     private void Jump()
    100.     {
    101.         velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    102.     }
    103. }
    104.  
    I trying to get my character shift from an idle cycle to a walking/running cycle but I don't know why it isn't working. It kept prompting:

    Parameter 'Speed' does not exist.
    UnityEngine.Animator:SetFloat (string,single)
    PlayerMovement:Idle () (at Assets/Script/PlayerMovement.cs:84)
    PlayerMovement:Move () (at Assets/Script/PlayerMovement.cs:63)
    PlayerMovement:Update () (at Assets/Script/PlayerMovement.cs:34)

    Could someone help me please!
     
  2. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    204
    Make sure that your AnimationController has a "Speed" float parameter - name of that parameter has to be exact with correct casing.