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)) {

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);
  • \$\begingroup\$ What is the method you use to do the rotation? That matters; pasting some code would be helpful. \$\endgroup\$ – jhocking Jul 28 '15 at 15:49
  • 1
    \$\begingroup\$ there, edited it \$\endgroup\$ – 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).

  • \$\begingroup\$ I think you mean > 0.9 not < 0.1 \$\endgroup\$ – Alan Wolfe Jul 28 '15 at 15:39
  • \$\begingroup\$ oh right, editing \$\endgroup\$ – jhocking Jul 28 '15 at 15:47

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.

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.