Game Development Community

Object facing toward player

by Jeff Osborn · in Torque Game Engine · 09/10/2005 (8:58 am) · 10 replies

I was wondering if there was an easy way to make a StaticShape object automatically face toward the nearest player if that player is within a certain radius of the object.

I have a statue that I want to "become alive" when a player is near and cast spells at the player. It would be some much better if the statue turned its malevolent gaze on the player as it was doing this rather than just facing its current direction.

I'm looking for some script snippets if I can get them.

Thanks.

#1
09/11/2005 (2:49 am)
Hey, I am a complete Torque nooB, but I may have an idea. If you changed the rotation variable using triganometry then recompiled the script. I can't guarantee this idea will work at all as I have never tried it, but that is how I would expect it would be done.

Hope this helps :)

Tim
#2
09/11/2005 (4:40 am)
In scripting wouldnt that be something that sets the objects x rotation value to that of the players x position ? just a guess
#3
09/11/2005 (12:04 pm)
Well yeah, i know I have to set the rotation. i have no problem with that. What I want to know is how to know WHICH direction it should look. How do I determine the facing vector of the object based on the location of the player. Is ther a function that will give me a vector between two positional transforms? Also, how do I extract the rotation from that vector for either object?

Maybe my question is more a math question than a torque question. Also, is the best way to change the rotation to set some sort of timer (like while the player is standing in a trigger that surrounds the statue?

Thanks.
#4
09/11/2005 (2:07 pm)
You could make it an AIplayer and then use
setaimobject(%player), it would then follow the player.
or if you want to use a static object, get the vector with
%vec = vectorSub(%objpos,%statpos);
#5
09/12/2005 (6:42 am)
Thank you very much. I'll try it out as soon as I get a chance. It's not that I don't want to try it right away, but you know how these projects are-- about 10 gillion things to do at once.

I'll let you know how it turns out.
#6
09/12/2005 (6:56 am)
Check out the AIturret resource.
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5345

Your statue would be a turret and it would rotate towards the target as they approach.. then it would start shooting from the weapon it has mounted.

I implemented this once and it was quite effective.
#7
09/12/2005 (7:26 am)
Wow, thanks...

If I don't get a chance to say it elsewhere, let me say that the forum and the GG community is fantastic. It's rare to not get the help I need from you guys.

Heartfelt thanks to all.
#8
09/12/2005 (8:49 pm)
So, let's suppose I take the first solution, which involves getting the vector from the static object to the player. How do I turn it into a rotation? I know how to do it with trig-- normalize it and get the sin and cos, but is there a simple function or method that is on static object that says "face down this vector's direction?" or that can (given a vector) return the angle or radians?

I know, I'm lazy...

Thanks.
#9
09/12/2005 (9:09 pm)
I think that item has a method of this nature, called deploy or something similar.
#10
09/12/2005 (10:22 pm)
Maybe getAnglesFromVector?? Ive never used it so im not sure how or if it works?