How to use Vector2 movetowards?
Hello, I have been following a blackthornprod tutorial to help create a boss battle and I've come across a problem with "Vector2.MoveTowards". So I've created a boss character who moves and jumps (Planning to add some more attack ect.) and the Vector2.Movetowards isn't working at all. The animations are working fine. I've tried increasing and decreasing the speed, checked whether the "Player" tag was spelt correctly and the boss does not move whatsoever. I will appreciate any help! Thanks!!
Script:
private float timer;
public float minTime;
public float maxTime;
private Transform playerPos;
public float speed;
float elapsedTime;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
playerPos = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
timer = Random.Range(minTime, maxTime);
}
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (timer <= 0)
{
animator.SetTrigger("idle");
}
else
{
timer -= Time.deltaTime;
}
Vector2 target = new Vector2(playerPos.position.x, animator.transform.position.y);
animator.transform.position = Vector2.MoveTowards(animator.transform.position, target, speed * elapsedTime);
elapsedTime += Time.deltaTime;
}
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
I have same issue, in a 3D game! Would love to know the solution,Would love to know an answer to this, i'm making a 3D game but have the same issue!
Shouldn't it be: Vector2 target = new Vector2(playerPos.position.x, playerPos.position.y);
Answer by koelibri · Jan 23, 2020 at 10:58 PM
For those who come accross this post: I had a similar problem recently. It was caused by the "Apply Root Motion" flag. Unchecking it in the Animator component solved my problem.
Your answer
Follow this Question
Related Questions
My movement script is not moving my 2D object 2 Answers
How to disable diagonal Vector3 movement of non-player object using MoveTowards? 1 Answer
[C#] The left-hand side of an assignment must be a variable, a property or an indexer 0 Answers
This script doesnt work why? (2D) 0 Answers
Looking for the optimal smooth incremental movement method. 0 Answers