Search Unity

animations not working help?

Discussion in 'Animation' started by donovanjanssen15, Jan 2, 2022.

  1. donovanjanssen15

    donovanjanssen15

    Joined:
    Sep 12, 2021
    Posts:
    1
    Hello! this is kinda my first FPS project. The movement part works fine, but when i try adding animations it doesnt work. sometimes it plays fine when i press W, but everything else just F***s up?
    idk if someone got more info on how to do this? tyvm

    ![Image from the animator component!](https://gyazo.com/f18475139c1969a4f0cdee435ab139ca)


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

    public class PlayerMovement : MonoBehaviour
    {

    //Variables
    [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;

    //Refferences
    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");
    float MoveX = Input.GetAxis("Horizontal");

    moveDirection = new Vector3(MoveX, 0, moveZ);
    moveDirection = transform.TransformDirection(moveDirection);

    if (isGrounded)
    {
    if (Input.GetKeyDown(KeyCode.Space))
    {
    Jump();
    }

    }

    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;

    controller.Move(moveDirection * Time.deltaTime);

    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);
    }

    private void Idle()
    {
    anim.SetFloat("MoveX", 0, 0.1f, Time.deltaTime);
    }

    private void Walk()
    {
    moveSpeed = walkSpeed;

    if (Input.GetKey(KeyCode.W))
    {
    anim.SetFloat("MoveX", 1, 0.1f, Time.deltaTime);
    }

    if (Input.GetKey(KeyCode.S))
    {
    anim.SetFloat("MoveX", -1, 0.1f, Time.deltaTime);
    }

    if (Input.GetKey(KeyCode.D))
    {
    anim.SetFloat("MoveZ", 1, 0.1f, Time.deltaTime);
    }

    if (Input.GetKey(KeyCode.A))
    {
    anim.SetFloat("MoveZ", -1, 0.1f, Time.deltaTime);
    }

    }

    private void Run()
    {
    moveSpeed = runSpeed;
    }

    private void Jump()
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
    }

    }
    ```
     

    Attached Files: