Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

object must shoot at me

Rick Rutledge
Second Lifer
Join date: 9 Nov 2004
Posts: 27
12-20-2004 09:11
I would like an object to shoot a bullet at me, but I'm not sure how positions and vectors work.

I've got the object sensing the nearest AV, and pointing at it. When I shoot a bullet in that direction, it goes towards the ground, sort of in the general direction it's facing.

Here's my code, it's modded from the popgun script:

CODE

float SPEED = 50.0;
integer LIFETIME = 1;
rotation rot;
rotation target_rot;
vector vel;
vector pos;
vector target_pos;
float target_distance;

default
{
state_entry()
{
llSensorRepeat("", "", AGENT, 10.0, 7000*PI, 2.0);
}

sensor(integer total_number)
{
target_pos = llDetectedPos(0);
pos = llGetPos();
target_distance = llVecDist(target_pos, pos);

if(target_distance < 10.1)
{
llLookAt(target_pos, 20.0, 1.0);
llSleep(1.1);

rot = llGetRot(); // Get current avatar mouselook direction
vel = llRot2Fwd(rot); // Convert rotation to a direction vector

pos = pos + vel; // Create beam slightly in direction of travel
vel = vel * SPEED; // Multiply normalized vector by speed

llRezObject("Bullet", pos, vel, rot, LIFETIME);
}
}
}


Any help in getting the bullet to shoot towards the detected AV would be appreciated.

In fact, if the object could just shoot the bullet, and not even bother with facing the target, that would be okay too. I only did it that way because the popgun script shoots the bullet in whatever way the AV is facing.

Thanks,
Rick
Cross Lament
Loose-brained Vixen
Join date: 20 Mar 2004
Posts: 1,115
12-20-2004 09:48
Well, one thing you're going to run into is that llLookAt() points the +Z axis towards the target position, not the +X axis (which is 'forward')... so since it's pointing the +Z towards the target, the 'forward' vector is going to point at the ground.

Instead of using llRot2Fwd(), try using:
CODE
llVecNorm( target_pos - pos );
to generate the unit vector toward the target. The advantage to this is that the object doesn't need to rotate at all, either. :)

Here's your code again, with that change made. I haven't tested it in-world, but it should work. :)

CODE

float SPEED = 50.0;
integer LIFETIME = 1;
rotation rot;
rotation target_rot;
vector vel;
vector pos;
vector target_pos;
float target_distance;

default
{
state_entry()
{
llSensorRepeat("", "", AGENT, 10.0, 7000*PI, 2.0);
}

sensor(integer total_number)
{
target_pos = llDetectedPos(0);
pos = llGetPos();
target_distance = llVecDist(target_pos, pos);

if(target_distance < 10.1)
{
//llLookAt(target_pos, 20.0, 1.0);
//lSleep(1.1);

rot = llGetRot(); // Get current avatar mouselook direction
vel = llVecNorm( target_pos - pos ); // Convert rotation to a direction vector

//pos = pos + vel; // Create beam slightly in direction of travel
vel = vel * SPEED; // Multiply normalized vector by speed

llRezObject("Bullet", pos, vel, rot, LIFETIME);
}
}
}
_____________________
- Making everyone's day just a little more surreal -

Teeple Linden: "OK, where did the tentacled thing go while I was playing with my face?"
Nexus Nash
Undercover Linden
Join date: 18 Dec 2002
Posts: 1,084
12-20-2004 09:57
Use llLookAt(), it works very well. I have 2 sets of automated turrets that use this!
_____________________
Rick Rutledge
Second Lifer
Join date: 9 Nov 2004
Posts: 27
12-20-2004 11:12
Thanks Cross, that code did the trick! And I'm going to use both ways, a turret that points and shoots, and one that just shoots without pointing.

- Rich