Game Development Community

dev|Pro Game Development Curriculum

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:

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!

#1
02/06/2008 (10:02 pm)
Can you show me how this would look in the script, i'm new to this and need some help. Lets say, If I create a bot using this in console: $myBot = aiAddPlayer("bot1"); Then I create a function and keybind to call your script above. How would this look in the .cs file for targeting that bot??

Thanks
#2
04/15/2008 (2:14 pm)
Use something like this:

lookAtTarget($myBot, %target);
#3
05/25/2009 (9:36 pm)
@Nicolai, if you're still around, Im trying to -unsuccessfully- mod your resource for also setting Pitch -X axis rotation-; if you have some idea, throw it here.