# Unity - Check if object is facing Vector3 coordinates

I am having a hard time figuring this out, I want to have objects that move around randomly, and when they get new coordinates to move towards, face them, and then move.

Here's explained:

1. Target moves towards point A.
2. Target reaches point A.
3. Get new coordinates.
4. Rotate towards the new coordinates WITHOUT moving
5. When facing the new target coordinates, start moving

I am trying it with Raycast, but it doesnt work, it tells me it always looks at them...

if (Physics.Raycast(transform.forward, randomTargetCoords, out hit)) {
Debug.Log("spotted");
}


I want to project the ray from the front of the object to the target coordinates (Vector3), and if true, do any code inside the if method.

How can I do it?

EDIT: Here is the code for moving and rotating the object

transform.position = Vector3.MoveTowards(transform.position, targetCoords, moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

• What is the method you use to do the rotation? That matters; pasting some code would be helpful. – jhocking Jul 28 '15 at 15:49
• there, edited it – Borislav Jul 29 '15 at 6:23

Calculate the dot product to determine how close two vectors are. The dot product is 1 when they are exactly the same, -1 when they are exactly opposite, 0 when they are perpendicular, and decimal values when partway.

So take the current direction, the target direction, then Vector3.Dot() and check if greater than .9 (or whatever threshold you decide looks good).

jhocking's answer about dot product is the right answer to the question, but if you are rotating your object over time to face the right direction, inside that function you must be calculating how much you have left to rotate so that you know what direction to rotate and so that you don't rotate too far and overshoot.

That means that inside that function, you already ought to have knowledge of when the object is done rotating and is in fact facing the right direction - even in the case that it started out facing the right direction and had to do no rotation at all.

Whatever work you are doing in there could be used for this, without having to do extra calculations. Maybe that function returns true if it had any rotations to do that frame, so that you can watch for it to return false, to know when it's time to start moving.

• well, it depends on the method being used to do the rotation. which the OP should probably clarify – jhocking Jul 28 '15 at 15:49
• Yeah true, makes sense – Alan Wolfe Jul 28 '15 at 15:54
• I edited the question, check it out please – Borislav Jul 29 '15 at 6:23
• I see the function you are using. That is unfortunate it doesn't tell you when it is done rotating! – Alan Wolfe Jul 29 '15 at 13:44

You can use Quaternion.LookAt() always and then just move your object forward whenever you want.

• there is no Quaternion.LookAt() – Borislav Jul 28 '15 at 14:54
• He meant LookRotation(), but regardless that isn't the answer to this question. – jhocking Jul 28 '15 at 15:01