- Home /
Face target first then move forward to it - Top Down 2D
So I have a 2D top down scene with X and Y axis, what I want to happen to my object is to turn to its target first then eventually move forward to it. I have managed to make it turn to face its target but it doesn't go forward. Here's my code.
void Update ()
{
if (userInput.currentWayPoint != null) {
isRotating = true;
if (isRotating)
TurnToTarget (userInput.currentWayPoint);
else if (isMoving)
Move (userInput.currentWayPoint);
} else {
isRotating = false;
}
}
void TurnToTarget (Vector3 target) {
Vector3 diff = target - transform.position;
diff.Normalize ();
rotZ = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg;
rotationDirection = Quaternion.Euler (0, 0, rotZ - scatterForm);
transform.rotation = Quaternion.RotateTowards (transform.rotation, rotationDirection, rotateSpeed * Time.deltaTime);
isRotating = false;
isMoving = true;
}
void Move (Vector3 target) {
Vector3 diff = target - transform.position;
transform.position = Vector3.MoveTowards(transform.position, diff, Time.deltaTime * speed);
}
currentWayPoint is handling the position of the mouse which serves as my target
Answer by Creeper_Math · Jan 03, 2017 at 11:08 PM
You have very interesting "if then" statements.. here's what you have....
if (userInput.currentWayPoint != null) {
isRotating = true;
if (isRotating)
TurnToTarget (userInput.currentWayPoint);
else if (isMoving)
Move (userInput.currentWayPoint);
} else {
isRotating = false;
}
First of all... you're setting "isRotating" to true and immediately testing to see if it's true... Which can be just set to true and removed... The specific lines that you had are below
isRotating = true;
if (isRotating)
Then you're saying that if it's not true (which will never happen), then you execute your moving script (which therefore will never occur) The specific lines are below
else if (isMoving)
Move (userInput.currentWayPoint);
Therefore the moving is not working...
A fix for this would be the following
if (userInput.currentWayPoint != null) {
isRotating = true;
TurnToTarget (userInput.currentWayPoint);
if (isMoving) {
Move (userInput.currentWayPoint);
}
} else {
isRotating = false;
}
Looking further, this would still not work, as your rotating script is always saying that's it's finished every update, even if it's the first rotation update it had. This in particular includes the isRotating = false;
and isMoving = true;
.
An easy fix would be to replace your rotating script with
void TurnToTarget (Vector3 target) {
Vector3 diff = target - transform.position;
diff.Normalize ();
rotZ = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg;
rotationDirection = Quaternion.Euler (0, 0, rotZ - scatterForm);
transform.rotation = Quaternion.RotateTowards (transform.rotation, rotationDirection, rotateSpeed * Time.deltaTime);
if (transform.rotation == rotationDirection) {
isRotating = false;
isMoving = true;
}
}
Where it will now only say that isRotating is false and set "isMoving" to true when it is facing the target
Hopefully this helps you! If you want it so that it will start moving a bit quicker after it rotates then I can provide you with another replacement for the final script that I have here!
Your answer
Follow this Question
Related Questions
Player won't look in direction and travel at the same time? 2 Answers
Bullet follows player mouvement when It instintiates. 1 Answer
Rotate to where the player is moving 1 Answer
How can I rotate an object towards a collision normal in 2D? 0 Answers
How to auto rotate a object, during it moving in a path. 1 Answer