Hi everyone, i got a problem that I don’t know how to solve.
I have an “enemy” object, a capsule, that rotates along a sphere toward a “player” object, a cube.
The movement is done by rotating a pivot positioned in the center of the sphere, but this is not important.
The problem I want to solve is that the enemy should face the direction in which is travelling.
To do this I thought about using an approximation fo the derivative: I get the global position of the enemy object, I move very little along the movement direction and take again the global position.
Now I have two positions (P0 and P1) and I can compute the direction between the two points (D=P1-P0),
all I need to do now is to orient the enemy object along the direction computed. How can I do this?
From other forum topics I found this piece of code:
var d=p1.subtract(p0) const target = p0.add(d); this.enemy.lookAt(d);
But it works only sometimes, other times the object is rotated 90° around the local y axis I think.
I prepared a playground with a basic example, my solution, and some utility function to get a random position, orient the object to stick out of the surface and so on. Around every 0,3 seconds a line is printed that represent the direction.
Any help is appreciated!