How to make the a 3d object point towards a location without using objectPointAt()
-
I'm trying to make a 3D racer game by expanding the kat racer template/demo. I have a couple of collision boxes/points on the track set up. The only thing is that I can't track the angle while using objectPointAt() to point/move to a random track Point. How do I make my own function that has the same effect, but tracks the angle.
-
The elevation in Kat Racer is the same everywhere on the track, this means that you can do this with a 2D (Y axis) rotation. It is a good thing too because otherwise you might need to keep track of 3 angles or a unit quaternion or a rotation matrix. With one angle it is simple to find it with
atan2()
, and apply it withrotateObject()
. You also will need to keep track of your object's position so that you can reset it's facing direction withobjectPointAt()
before callingrotateObject()
since rotations accumulate withrotateObject()
.// Find the angle from the object at objPosition to a target point and look at it float angle = -atan2(target.z - objPosition.z, target.x - objPosition.x) //objectPointAt(obj, target) objectPointAt(obj, objPosition + {1, 0, 0}) rotateObject(obj, {0, 1, 0}, angle)
-
@gothon Thank you so much! This will be very useful for coding the kart AI. I'll be using a 2D vector for the angle from now on.