Hey there, in the game concept I’m developing, the player has only two options; dodge or attack. Currently, I’m working on the dodging, where the player will return to their original position after the dodge has occurred. I tried to add animations to this, using an integer to decide which ‘state’ the player is in to determine which animation plays. However, when testing this in play, the animation doesn’t occur. Well, technically, it does happen, but it resets to the 0/Idle state so fast that the only way you notice is that the number shakes. I’ve tried fiddling with the settings to slow down the animation, the transition, ect. But it still persists.
In the spoiler, you’ll find the code.
Sorry in advance if this is obvious.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterController : MonoBehaviour
{
public float Speed;
public Transform Character;
public bool dodging;
public Animator animator;
private Vector3 moveDirection = Vector3.zero;
public Vector3 jumpVector;
private void Awake()
{
animator = GetComponent<Animator>();
}
//Dodging state is recorded so that the player will be unable to attack when doing a dodging move
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Center"))
{
dodging = false;
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Center"))
{
dodging = true;
}
}
void Update()
{
//If chacter presses any dodge keys, they move the same amount each time. The moment the key stops being held down, player returns to the center
//AnimState to determine animation. 0 = Idle, 1 = DodgeLeft, 2 = DodgeRight, 3 = DodgeDown, 4 = DodgeUp,
if (Input.GetKeyDown(KeyCode.A))
{
transform.position += Vector3.back * Speed;
animator.SetInteger("AnimState", 1);
Debug.Log("Button pressed");
}
else if (Input.GetKeyUp(KeyCode.A))
{
transform.position += Vector3.forward * Speed;
Debug.Log("Button no longer pressed");
}
else if (Input.GetKeyDown(KeyCode.D))
{
animator.SetInteger("AnimState", 2);
transform.position += Vector3.forward * Speed;
Debug.Log("Button pressed");
}
else if (Input.GetKeyUp(KeyCode.D))
{
transform.position += Vector3.back * Speed;
Debug.Log("Button no longer pressed");
}
else if (Input.GetKeyUp(KeyCode.S))
{
animator.SetInteger("AnimState", 3);
Debug.Log("Button no longer pressed");
}
else if (Input.GetKeyUp(KeyCode.W))
{
animator.SetInteger("AnimState", 4);
GetComponent<Rigidbody>().AddForce(jumpVector, ForceMode.VelocityChange);
Debug.Log("Button no longer pressed");
else
{
animator.SetInteger("AnimState", 0);
}
}
}