Getting a script object to point at the player?

Post your scripting questions / solutions here

Moderator: Moderators

matt_moh
Sergeant
Posts: 65
Joined: Sat Feb 08, 2003 12:04 am

Getting a script object to point at the player?

Post by matt_moh »

I made a script object out of brushes that looks like a gun and was wondering if it was possible to get the "gun" to turn and point at the player as he moves around, kind of like how a tank turret moves. Is there any simple way to get the gun to work? I don't have any advanced 3d graphic tools or the money to buy them, so making my own model is out of the question. If there is no simple way to make my gun work, I did come up with a complicated idea involving trigonometric ratios. If I have to resort to this, is it possible to put a command in my script that calculates the inverse cosine of a variable?
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

I think it can be done pretty easily. First calculate a vector to the target, e.g.

local.vec = local.target.origin - self.origin

Then make the script_object rotate to that vector:

self rotateto local.vec
self speed 15
self waitmove

I don't know if this would change the pitch of the object as well. If it does, just force the z coord of the vector to be 0.
Image
matt_moh
Sergeant
Posts: 65
Joined: Sat Feb 08, 2003 12:04 am

Post by matt_moh »

Great! Thanks alot, but what do I have to do to change the z coord of the vector?
nuggets
General
Posts: 1006
Joined: Fri Feb 28, 2003 2:57 am
Location: U-england-K (england in the UK) :P
Contact:

Post by nuggets »

local.vec[2] = 0

although if the player isn't on the ground floor it'd be a bit strange
hope this helps, prob not cos it's all foreign 2 me :-/
matt_moh
Sergeant
Posts: 65
Joined: Sat Feb 08, 2003 12:04 am

Post by matt_moh »

Well, after finishing another project, I finaly got to test out the gun. It doesn't work. What happens is that the gun, instead of pointing at the player, spins around in a apiral motion and ends up pointing at the ground. I tried changing the values of each of the three coordinates of the vector to zero, but it still does not work. These are the exact lines of my script:

cannon_aim: //threaded by a trigger_multiple in the bsp

$car1_turret1 thread turretmove
//$car1_barrel1 thread barrelmove
end

turretmove:
local.target = $player
local.vec = local.target.origin - self.origin
local.vec[2] = 0
self rotateto local.vec
self time 2
self waitmove
end
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Hmmm

Post by jv_map »

Try:

Code: Select all

cannon_aim: //threaded by a trigger_multiple in the bsp

$car1_turret1 thread turretmove
//$car1_barrel1 thread barrelmove
end

turretmove:
local.target = $player
local.vec = local.target.origin // now is a location instead of a direction
local.vec[2] = 0
self rotateto local.vec
self time 2
self waitmove
end
Image
nuggets
General
Posts: 1006
Joined: Fri Feb 28, 2003 2:57 am
Location: U-england-K (england in the UK) :P
Contact:

Post by nuggets »

or...

cannon_aim
$car1_turret1 thread turretmove
end

turretmove:
local.angles = (vector_toangles ($player.origin - self.origin))
self speed 15
self rotateto $player
self waitmove
end
hope this helps, prob not cos it's all foreign 2 me :-/
User avatar
tltrude
Chuck Norris
Posts: 4774
Joined: Sun Jul 07, 2002 4:03 am
Location: Oklahoma, USA
Contact:

lookat

Post by tltrude »

Can't you use the "lookat" command somehow? To me, it looks like your threads will only find the player one time and stop. I think he wants it to follow the player's movements.
Tom Trude,

Image
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

Well I don't think the turret gun script_object has an eye bone :roll:

But you could always modify the script so that it traces the player (just put it in a while loop).
Image
User avatar
bdbodger
Moderator
Posts: 2596
Joined: Tue Feb 25, 2003 7:34 am
Location: canada
Contact:

Post by bdbodger »

nuggets whouldn't that just give you the angles from the object to the player? maybe you could make two spript origins and put one at the base of the gun and one at the end of the gun . After that you could use the use the vector_toangles ($origin1 $origin2) to find the direction of the gun and then use that to point the gun at the player?
User avatar
tltrude
Chuck Norris
Posts: 4774
Joined: Sun Jul 07, 2002 4:03 am
Location: Oklahoma, USA
Contact:

eye bone

Post by tltrude »

You would have to bind an "eye bone" to the gun pivit point--a script origin.

There are some pretty cool things in the vehicles_thinkers.scr script. It uses commands like "vector_within" and "vector_length" for the tanks turrets. I guess the AI tanks don't have have "eye bones" either!

Here is something that might work from the turnto.scr script:

// parameters:
// local.target - target aiming at (can be anything which has an origin)
start local.target:
self turnto local.target
Last edited by tltrude on Sun May 11, 2003 11:18 pm, edited 1 time in total.
Tom Trude,

Image
nuggets
General
Posts: 1006
Joined: Fri Feb 28, 2003 2:57 am
Location: U-england-K (england in the UK) :P
Contact:

Post by nuggets »

yes yes no no and yes no!!!

i don't know about the order but from what i've read some of it's right and some wrong

when a gun will be facing the player it won't move, therefore irrelevant of the position origin or whatever you want to call it, it will only be moving around the angles it needs to move to

attaching an eye to a gun?? what the hell are you thinking man, it's like giving a person wheels so you can use the drive command
hope this helps, prob not cos it's all foreign 2 me :-/
User avatar
tltrude
Chuck Norris
Posts: 4774
Joined: Sun Jul 07, 2002 4:03 am
Location: Oklahoma, USA
Contact:

Mg42

Post by tltrude »

An mg42 can track players without an AI standing by it. At least I do think before posting my thoughts.
Tom Trude,

Image
nuggets
General
Posts: 1006
Joined: Fri Feb 28, 2003 2:57 am
Location: U-england-K (england in the UK) :P
Contact:

Post by nuggets »

more information would b grateful... :roll:

and another thing i 4got 2 mention before, with a trigger multiple it will follow the player whenever the player is in the trigger multiple... which will stop unnecessary scripting calling when the player isn't in sight of the "gun" / possibly an mg42
hope this helps, prob not cos it's all foreign 2 me :-/
User avatar
tltrude
Chuck Norris
Posts: 4774
Joined: Sun Jul 07, 2002 4:03 am
Location: Oklahoma, USA
Contact:

Test_map

Post by tltrude »

I made a test map for it and got the gun working, but it is still weird. It rotates on all axis and flips around if you move along the north wall (past 0 angle). It also seems to point at the top of the player's head. Here is a zip with the bsp, map file, and script:

http://pages.sbcglobal.net/tltrude/Temp/auto_gun.zip

I added two origins at both ends of the barrel, as per bdbodger suggestion, to get it working like it is now. It should probably be made so that only the barrel moves up and down with changes in the player's elevation. In other words, the rotation and the elevation should be seperate movements.

I do understand why "lookat" and "turnto" wont work now. Those commands can not be used with "scriptSlaves" like a script_object--that's what the console said anyway.
Tom Trude,

Image
Post Reply