- Home /
MoveTowards with animation
Hello,
I need help with animating my character while using the Vector3.MoveTowards function. I use a character controller which animates my character based on speed and key input but I have a sequence in the game where the player presses a certain key and the character should move on its own, without the player pressing keys. I have managed to move the character but he just glides through the scene because no inputs have been pressed by the player. How do I implement this?
//CHARACTER CONTROLLER FUNCTIONS
private void ProcessInput()
{
movementDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
movementSpeed = Mathf.Clamp(movementDirection.magnitude, 0.0f, 1.0f);
movementDirection.Normalize();
}
public void Move()
{
step = movementSpeed * MOVEMENT_BASE_SPEED;
rigidBody.velocity = movementDirection * step;
}
public void Animate()
{
if(movementDirection != Vector2.zero)
{
animator.SetFloat("Horizontal", movementDirection.x);
animator.SetFloat("Vertical", movementDirection.y);
}
animator.SetFloat("Speed", movementSpeed);
}
I need to implement my animation when the player is moving with this function:
player.transform.position = Vector2.MoveTowards(player.transform.position, closestTravelPoint.transform.position, playerMovement.MOVEMENT_BASE_SPEED * Time.deltaTime);
Answer by GrayLightGames · Nov 05, 2019 at 02:38 PM
You could set a flag (isMoving or something) when the player is moving and check it in Update(). If it's true, call Animate(). Just make sure you set it to false when you want the animation to stop. Hope that helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220612214352im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Even / Constant Speed and Jumping 2D 1 Answer
2D Animation does not start 1 Answer
LimbSolver2D missing "create effector" button 1 Answer
Damn, HOW CAN I KNOW, IF MY 2d ANIMATION SPRITE IS FINISHED??!! 0 Answers
Isometric Character Question 0 Answers