Kaelen
❔ Animation never stop c#
Hey, I have an animation on my character, and when I'm starting walking, my character my game doesnt stop the animation, here is myy code :
public class PlayerMovement : MonoBehaviour
{
float speed = 4;
private CharacterController controller;
private Vector3 direction;
private Vector2 move;
private float gravity = 9.81f;
public static float velocity;
private string WALK_ANIMATION = "Walk";
private Animator anim;
PlayerInput input;
private Vector2 currentMovement;
private bool movementPressed;
void OnMove(InputValue value){
move = value.Get<Vector2>();
}
void Awake(){
input = new PlayerInput();
}
void handleMovement() {
input.Player.Move.performed += ctx => {
currentMovement = ctx.ReadValue<Vector2>();
movementPressed = currentMovement.x != 0 || currentMovement.y != 0;
};
bool isWalking = anim.GetBool(WALK_ANIMATION);
if(movementPressed && !isWalking){
anim.SetBool(WALK_ANIMATION, true);
}
if(!movementPressed && isWalking){
anim.SetBool(WALK_ANIMATION, false);
}
Debug.Log(anim.GetBool(WALK_ANIMATION));
Debug.Log(currentMovement);
}
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if(!controller.isGrounded){
velocity += gravity * Time.deltaTime;
}
direction = new Vector3(move.x, velocity, move.y);
controller.Move(-direction * speed * Time.deltaTime);
handleMovement();
}
void OnEnable(){
input.Player.Enable();
}
void OnDisable(){
input.Player.Disable();
}
}
public class PlayerMovement : MonoBehaviour
{
float speed = 4;
private CharacterController controller;
private Vector3 direction;
private Vector2 move;
private float gravity = 9.81f;
public static float velocity;
private string WALK_ANIMATION = "Walk";
private Animator anim;
PlayerInput input;
private Vector2 currentMovement;
private bool movementPressed;
void OnMove(InputValue value){
move = value.Get<Vector2>();
}
void Awake(){
input = new PlayerInput();
}
void handleMovement() {
input.Player.Move.performed += ctx => {
currentMovement = ctx.ReadValue<Vector2>();
movementPressed = currentMovement.x != 0 || currentMovement.y != 0;
};
bool isWalking = anim.GetBool(WALK_ANIMATION);
if(movementPressed && !isWalking){
anim.SetBool(WALK_ANIMATION, true);
}
if(!movementPressed && isWalking){
anim.SetBool(WALK_ANIMATION, false);
}
Debug.Log(anim.GetBool(WALK_ANIMATION));
Debug.Log(currentMovement);
}
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if(!controller.isGrounded){
velocity += gravity * Time.deltaTime;
}
direction = new Vector3(move.x, velocity, move.y);
controller.Move(-direction * speed * Time.deltaTime);
handleMovement();
}
void OnEnable(){
input.Player.Enable();
}
void OnDisable(){
input.Player.Disable();
}
}
5 replies