using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//VAR
[SerializeField] private float moveSpeed;
[SerializeField] private float walkSpeed;
[SerializeField] private float runSpeed;
private Vector3 moveDirection;
private Vector3 velocity;
[SerializeField] private bool isGrounded;
[SerializeField] private float groundCheckDistance;
[SerializeField] private LayerMask groundMask;
[SerializeField] private float gravity;
[SerializeField] private float jumpHeight;
//REF
private CharacterController controller;
private Animator anim;
private void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponentInChildren<Animator>();
}
private void Update()
{
Move();
}
private void Move()
{
isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float moveZ = Input.GetAxis("Vertical");
moveDirection = new Vector3(0, 0, moveZ);
moveDirection = transform.TransformDirection(moveDirection);
if(isGrounded)
{
if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
{
Walk();
}
else if (moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
{
Run();
}
else if (moveDirection == Vector3.zero)
{
Idle();
}
moveDirection *= moveSpeed;
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
controller.Move(moveDirection * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
private void Idle()
{
anim.SetFloat("Speed", 0);
}
private void Walk()
{
moveSpeed = walkSpeed;
anim.SetFloat("Speed", 0.5f);
}
private void Run()
{
moveSpeed = runSpeed;
anim.SetFloat("Speed", 1);
}
private void Jump()
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
}
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!