weird loops happening

its either because i’ve wrote the code wrong because i’m new or not ticked a setting in the animation tree but when i move my character forward it will randomly get stuck in a forward loop and the animation will stay too, its like it thinks the keys still held down, only way to fix it while in game is to spam w, i’ve made sure the condition works to change the animation, the animation transitions has exit time boxes are unticked, the weird thing is when i changed Update to FIxedUpdate it happens much less, could it also be my crappy pc, codes here, ask me for screenshots not sure what to give ya.

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

public class Claytoncont : MonoBehaviour
{
public float speed = 4;
public float rotSpeed = 80;
float rot = 0f;
public float gravity = 8;
Vector3 moveDir = Vector3.zero;

CharacterController controller;
Animator anim;

void Start()
{
controller = GetComponent();
anim = GetComponent();
}

void FixedUpdate()
{
if (controller.isGrounded)
{
if (Input.GetKey(KeyCode.W))
{
anim.SetInteger(“condition”, 1);
moveDir = new Vector3(0, 0, 1);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
if (Input.GetKeyUp(KeyCode.W))
{
moveDir = new Vector3(0, 0, 0);
anim.SetInteger(“condition”, 0);
}
}
rot += Input.GetAxis(“Horizontal”) * rotSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, rot, 0);

moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}

}

happens mostly when pressing w key rapidly or using another key immediately (only a and e) after

I would recommend that if your control scheme is to set a speed WHILE a key is down, you use GetKey( ), and to remember to remove that speed when the GetKey( ) == false. Using GetKeyUp( ) to clear the speed is less reliable when the app can lose focus or you are dealing with potentially overlapping keypresses.

Also, it’s unusual to use FixedUpdate( ) for animation and keyboard processing, precisely because it can be processed more than one time per frame and it would be wasteful to adjust animations multiple times before it can be drawn once. It’s more intended for physics calculations.

1 Like

thanks very much that’s fixed it, is there anyway to give you rep? and thanks for the advice on the Update and FixedUpdate

i am still confused so what should i do exaclty to fix this ?