I am trying to understand a way in THREE.JS (and the mathematical foundation) to get the direction an object should rotate to lookAt other but without actually rotate it. I've tried to use lookAt but that rotates the object completely.

I want to apply a small custom parameterized rotation on that direction. So I don't want all the rotation, just the direction of that shortest to look at rotation.

So, basically I have 2 object positions and I want the shortest rotation direction of one of them so that one looks at the second object position, independently of the second object rotation. And limit the axis so it works on a over the top 2d game setting (although the graphics are 3d)..

The rotation must be around Z for example.

And relative to the current object heading as this will be computed on each game loop iteration depending on the player position.

This is intended for a game where enemies try to follow the player but turn slow to look at him.

Thank you in advance

  • \$\begingroup\$ Presumably you've looked at the Quaternion.setFromUnitVevtors method, which computes the shortest rotation that takes a vector pointing in direction A to a vector pointing in direction B? \$\endgroup\$
    – DMGregory
    Commented Jan 23, 2020 at 12:56
  • \$\begingroup\$ B vector in my case is a position of other object and not a rotation. This works in that case? Thanks \$\endgroup\$
    – Rui d'Orey
    Commented Jan 23, 2020 at 16:49
  • \$\begingroup\$ You can make a direction from a position by subtracting your starting point from the end point. \$\endgroup\$
    – DMGregory
    Commented Jan 23, 2020 at 17:04
  • \$\begingroup\$ humm ok right, true.. A-B. nice nice. And what about the rotation direction? I don't want all the rotation, just the direction. And limit axis. \$\endgroup\$
    – Rui d'Orey
    Commented Jan 23, 2020 at 17:07
  • \$\begingroup\$ Sounds like you should edit your question to be more precise about what outputs you want, what inputs you have, and how you've tried making the jump between them so far. \$\endgroup\$
    – DMGregory
    Commented Jan 23, 2020 at 17:12

1 Answer 1


Solved using this:

// player world position
const vPlayerPositionRelativeToWorld = new THREE.Vector3().copy(player.transform.position);

// player position relative to enemy transform - includes rotation of the transform
const vPlayerPositionRelativeToLocalEnemyPositionAndRotation = enemy.transform.worldToLocal(vPlayerPositionRelativeToWorld);

// get angle to rotate
const maxRotateValue = .05;
const rotationToLookAtPlayer = Math.asin(vPlayerPositionRelativeToLocalEnemyPositionAndRotation.x);
const rotateAbsoluteValue = Math.min(Math.abs(rotationToLookAtPlayer), maxRotateValue);
const orientedRotationValue =  -Math.sign(rotationToLookAtPlayer) * rotateAbsoluteValue 

enemy.transform.rotateZ( orientedRotationValue );   

Not sure if there is a more efficient method but it works for this case.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .