C
C#15mo ago
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();
}


}
4 Replies
Kaelen
Kaelen15mo ago
This is because my currentMovement is never set to 0 at y and x even if I release the button. How can I solve it ?
Thinker
Thinker15mo ago
try #game-dev or $unity
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.