How to make an object face or look at another object
by Nicolai Dutka · 12/20/2007 (10:21 am) · 3 comments
Simply copy/paste this code to any executed script in your game:
You can then run lookAtTarget( %this, %target ) at any given time to make %this look at %target. Enjoy!
function lookAtTarget( %this, %target )
{
%forward = VectorSub(%target.getPosition(), %this.getPosition());
%x = getWord(%forward, 0);
%y = getWord(%forward, 1);
%angle = -mAtan(-%x, %y);
// make trans from position, axis and angle
%trans = %this.getPosition();
%trans = %trans SPC "0 0 1" SPC %angle;
%this.setTransform(%trans);
}You can then run lookAtTarget( %this, %target ) at any given time to make %this look at %target. Enjoy!
About the author

Torque Owner Ian Newland
Thanks