Question by
wenson16 · Jan 28, 2017 at 03:16 AM ·
movement scripttop-downturningvector3.angle
Gameobject not turning in the right direction
So I have a 2d top down scene with XY axis, what I want to happen is to make the gameobject face the position of my mouse input and then move forward. With my current code it turns right then move forward when the position of the mouse is greater than its transform position in X axis(transform.position.x) which is good. However, if the position of the mouse is less than its transform.position.x it doesn't turn and moves forward right away. I want the gameObject to turn left first then move if this condition happens.
Here's my code:
// waypPoint is the Input.mousePosition
float angle = Vector3.Dot (transform.right, (wayPoint - transform.position).normalized);
if (angle >= 0.05f) {
// Do some turn function here...
} else {
// Do some move forward here...
}
Comment
sorry here's my code for turning and moving forward to target
// Turning function
// $$anonymous$$ouse click point as target
void TurnToWayPoint (Vector3 target) {
Vector3 diff = target - myTransform.position;
diff.Normalize ();
rotZ = $$anonymous$$athf.Atan2 (diff.y, diff.x) * $$anonymous$$athf.Rad2Deg;
rotationDirection = Quaternion.Euler (0, 0, rotZ - 90f);
transform.rotation = Quaternion.RotateTowards (myTransform.rotation, rotationDirection, rotateSpeed * Time.deltaTime);
// $$anonymous$$ove Function
void $$anonymous$$ove () {
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, wayPoint, speed * Time.deltaTime * 0.25f);
}