Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Help debugging a gun type script

Fenix Eldritch
Mostly harmless
Join date: 30 Jan 2005
Posts: 201
12-19-2009 07:08
Hi guys, I need a second pair of eyes on this.... I can't figure out what's wrong with this code. I modeled it off one of the early Linden templates. My problem is that when I fire my weapon, the projectile just kinda falls out of the barrel instead of actually traveling forward.

firing code:
CODE

control(key name, integer level, integer edge)
{
integer start = level & edge;
integer end = ~level & edge;
//integer held = level & ~edge;
//integer untouched = ~(level | edge);

if((start & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
{
llOwnerSay("set...");
//play the sound
}
else if((end & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON)
{
llOwnerSay("...fire!");

//turn hook (child prim) invisible
llSetLinkAlpha(6,0,ALL_SIDES);
//play sound

//rez/fire projectile
rot = llGetRot();
vel = llRot2Fwd(rot);
pos = llGetPos();
pos = pos + vel;
pos.z += 0.75;
vel = vel * SPEED; //currently 20.0
llRezObject("Hook", pos, vel, rot, 0);
}
}

object_rez(key k)
{
target_parameters = [ PSYS_SRC_TARGET_KEY, k ];
llParticleSystem( particle_parameters + target_parameters );
}


and the begining of the projectile code:
CODE

state_entry()
{
llOwnerSay("Firing!");
HIT=FALSE;
llSetStatus(STATUS_PHYSICS, TRUE);
llSetStatus(STATUS_DIE_AT_EDGE, FALSE);
llSetBuoyancy(1);
llCollisionSound("", 0);
//start a timer to limit the hook's travel time (reach)
llSetTimerEvent(2.0);
llListen(-64,"Hookshot","","HEnd");
}


I've instrumented most of the functions with llOwnerSay() so I know there is plenty of time for the bullet to travel before recoiling back. I see the bullet rez, then slowly fall to the ground, like it's got no velocity at all...
Indeterminate Schism
Registered User
Join date: 24 May 2008
Posts: 236
12-19-2009 08:19
Possibly a dumb suggestion because I've never used the function but ...
vel = llRot2Fwd(rot);
If your parent object isn't facing the way you want to fire, might this result in a 'sideways' velocity that then gets killed-off as the hook immediately hits the rest of your gun?
Fenix Eldritch
Mostly harmless
Join date: 30 Jan 2005
Posts: 201
12-19-2009 08:36
To be honest, I don't understand how that part of the template code works... perhaps I should first ask if someone can better explain it to me?

The gun, when attached, has an x rotation of 90.
The projectile has a zero rotation (but once I get this to correctly launch, I want to make it rez at a new angle... it's a pyramid that I want to have it moving "point" first).
Innula Zenovka
Registered User
Join date: 20 Jun 2007
Posts: 1,825
12-19-2009 10:32
Why not make the projectile physical before you put in the gun? That way you can be sure that the velocity parameter is actually doing something.
Fenix Eldritch
Mostly harmless
Join date: 30 Jan 2005
Posts: 201
12-19-2009 10:42
I've tried that with no change in behavior.

Besides... the Linden guns seem to all work on the premise that the projectile is non-physical at first
Innula Zenovka
Registered User
Join date: 20 Jun 2007
Posts: 1,825
12-19-2009 11:41
Hmm... well, this, if you put in a prim, will fire a pyramid point first if the pyramid is physical to start with:
CODE
string projectile;
default
{
state_entry()
{
// llSay(0, "Hello, Avatar!");
}

touch_start(integer total_number)
{
projectile = llGetInventoryName(INVENTORY_OBJECT,0);
llRezAtRoot(projectile,llGetPos()+ < 1.0,0.0,0.0 > *llGetRot(), < 42.0,0.0,0.0 > *llGetRot(), llEuler2Rot( < 0.0,90.0,0.0 > *DEG_TO_RAD)*llGetRot(),0);
}
}



The identical script, if I make the projectile prim physical when it's rezzed rather than before I put it in the rezzer, causes the projectile to rez and drop straight to the ground, as you describe yours doing.
Fenix Eldritch
Mostly harmless
Join date: 30 Jan 2005
Posts: 201
12-19-2009 16:28
Having the bullet physical at the start seems to work a lot better. I could have sworn the linden weapons I used (in world) as a reference worked the other way - but this works, so I'll stick with it. Thanks!

[Edit]
Just read this again after a few days... and realized my last post contradicts myself. I should have said that it worked better when I had JUST a physical projectile with no script in it. So my original bullet script must have been mucking things up.

Also, my original bullet script would turn the bullet non-physical after a collision and also after a 2 second time interval - so it's entirely possible when I thought I was putting a physical bullet into the gun's inventory, it was in fact not physical.