I want the Player follow the Ball and get it. Then with the Ball I want the Player follow the Post. I’ve done two targets to be followed sequentially but the Player skips the first target and follows the second target (Post).
My code is like this:
void Start()
{
transformBall = GameObject.Find("Ball").transform;
transformPost = GameObject.Find("Post").transform;
animator = GetComponent<Animator>();
}
void Update()
{
if ((transform.position - targetBall.position).magnitude > EPSILON)
{
animator.SetBool("IsMoving", true);
Vector3 lookAtPosition = transformBall.position;
lookAtPosition.y = transform.position.y;
transform.LookAt(lookAtPosition);
Vector3 movedirection = transform.position - targetBall.position;
Vector3 moveSpeed = new Vector3(movedirection.normalized.x * movementSpeed * Time.deltaTime, 0, movedirection.normalized.z * movementSpeed * Time.deltaTime);
transform.position -= moveSpeed;
}
else
{
animator.SetBool("IsMoving", false);
}
if ((transform.position - targetPost.position).magnitude > EPSILON)
{
animator.SetBool("IsMoving", true);
Vector3 lookAtPosition = transformPost.position;
lookAtPosition.y = transform.position.y;
transform.LookAt(lookAtPosition);
Vector3 movedirection = transform.position - targetPost.position;
Vector3 moveSpeed = new Vector3(movedirection.normalized.x * movementSpeed * Time.deltaTime, 0, movedirection.normalized.z * movementSpeed * Time.deltaTime);
transform.position -= moveSpeed;
}
else
{
animator.SetBool("IsMoving", false);
}
}
}
Any idea would be greatly appreciated…