Author Topic: Spread ontrigger  (Read 1188 times)

I have yet to do this correctly.
Code: [Select]
  function armor::onTrigger(%this, %obj, %triggerNum, %val)
   {
      %mount = %obj.getObjectMount();

      //hack so we can shoot if we are a tank turret
      if(%obj.getDataBlock().getID() == HumveeMGTurretPlayer.getID())
         %mount = %obj;

      if(isObject(%mount))
      {
         if(%mount.getDataBlock() == HumveeMGTurretPlayer.getId() && %triggerNum == 0 && %val)
         {
            %client = %obj.client;
            if(isObject(%client))
               ServerCmdUnUseTool(%client);

            if(getSimTime() - %obj.lastShotTime < 300)
               return;

//START THE TEST
%spread = 0.0005;
%vector = %obj.getMuzzleVector(%slot);
%objectVelocity = %obj.getVelocity();
%vector1 = VectorScale(%vector, %projectile.muzzleVelocity);
%vector2 = VectorScale(%objectVelocity, %projectile.velInheritFactor);
%velocity = VectorAdd(%vector1,%vector2);
%x = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
%y = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
%z = (getRandom() - 0.5) * 10 * 3.1415926 * %spread;
%mat = MatrixCreateFromEuler(%x @ " " @ %y @ " " @ %z);
%velocity = MatrixMulVector(%mat, %velocity);
//STOP THE TEST
           
            %scaleFactor = getWord(%mount.getScale(), 2);
            %p = new Projectile()
            {
               dataBlock = gunProjectile;
               initialPosition = %mount.getSlotTransform(1);
               initialVelocity = %velocity;
               sourceObject = %obj;
               client = %obj.client;
               sourceSlot = 0;
               originPoint = %mount.getSlotTransform(1);
            };
            MissionCleanup.add(%p);
            %p.setScale(%scaleFactor SPC %scaleFactor SPC %scaleFactor);



            serverPlay3D(HumveeMGFireSound,%obj.getPosition());
         
           
           
           
            %obj.lastShotTime = getSimTime();

            return;
         }
      }
     
      Parent::onTrigger(%this,%obj,%triggerNum,%val);
   }
Can someone fix this up. I have no clue have vectors, velocity, and any of that works.

Code: [Select]
%vector = %obj.getEyeVector(); //Get the direction they're aiming
%velocity = VectorScale(%vector, 90); //Lengthen the vector to be the speed of the projectile we want
%x = (getRandom() - 0.5) * 0.015707963; //Calculate X, Y, and Z spread randomizations
%y = (getRandom() - 0.5) * 0.015707963;
%z = (getRandom() - 0.5) * 0.015707963;
%mat = MatrixCreateFromEuler(%x SPC %y SPC %z);
%velocity = MatrixMulVector(%mat, %velocity); //Randomize the lengthened vector.
%p = new projectile() //Create the projectile.
{
dataBlock = gunprojectile;
initialVelocity = %velocity;
initialPosition = %obj.getEyePoint();
sourceObject = %obj;
sourceSlot = %slot;
scale = %mount.getScale();
client = %obj.client;
};
MissionCleanup.add(%p);

The player shoots it where he is looking. The problem with that is, if you go in 3rd person and use look, you will shoot the machine gun the way you are looking, not the way the machine gun is facing. Also, the projectile is super fast and tiny.

I'm not going to script it for you, look at the code and edit it.

I have got the scale and the eye fixed. Now how would I make it so when I hold down the trigger, it will fire again, but make it wait the
Code: [Select]
if(getSimTime() - %obj.lastShotTime < 300)that is defined in the code?

That is the code that causes the shot to stop if the shot has been fired within the last 300 ms of the lastshottime variable being set. You'll want to set it on the object and schedule the armor triggering again (or move 90% of the overwrite to its own function) to create an automatic fire effect.

How would I re call an armor on trigger? Wouldn't that be like re calling making the person click again?

%this.ontrigger(%obj, %slot, %val);

I think putting the shoot into a separate function would work better, but so far it is failing. I click and nothing happens. None of the echos appear either.
Code: [Select]
  function Player::HumveeShoot(%obj,%slot,%val)
   {
      if(!%val)
{
cancel(%obj.Humvee_reshoot);
echo("Returned- VAL Error");
return;
}
      %mount = %obj.getObjectMount();

      //hack so we can shoot if we are a tank turret
      if(%obj.getDataBlock().getID() == HumveeMGTurretPlayer.getID())
         %mount = %obj;

      if(isObject(%mount))
      {
         if(%mount.getDataBlock() == HumveeMGTurretPlayer.getId() && %triggerNum == 0 && %val)
         {
            %client = %obj.client;
            if(isObject(%client))
               ServerCmdUnUseTool(%client);



%vector = %mount.getEyeVector(); //Get the direction they're aiming
%velocity = VectorScale(%vector, 60); //Lengthen the vector to be the speed of the projectile we want
%x = (getRandom() - 0.5) * 0.015707963; //Calculate X, Y, and Z spread randomizations
%y = (getRandom() - 0.5) * 0.015707963;
%z = (getRandom() - 0.5) * 0.015707963;
%mat = MatrixCreateFromEuler(%x SPC %y SPC %z);
%velocity = MatrixMulVector(%mat, %velocity); //Randomize the lengthened vector.
%scaleFactor = getWord(%mount.getScale(), 2); //Get the scale so we can scale the projectile.
%p = new projectile() //Create the projectile.
{
dataBlock = gunprojectile;
initialVelocity = %velocity;
initialPosition = %mount.getSlotTransform(1);
sourceObject = %obj;
sourceSlot = %slot;
client = %obj.client;
scale = %scaleFactor;
};
MissionCleanup.add(%p);
serverPlay3D(HumveeMGFireSound,%obj.getPosition());
%obj.lastShotTime = getSimTime();
%ReshootDelay = 300;
%ShootDelay = 1000;
%obj.Humvee_reshoot= %obj.schedule(%ReshootDelay,Humvee_Shoot,%slot,%val);
%obj.client.Humvee_LastFireTime=$Sim::Time+%ShootDelay/1000;

            return;
         }
      }
     
      Parent::onTrigger(%this,%obj,%triggerNum,%val);
   }




   
  function armor::onTrigger(%this, %obj, %triggerNum, %val)
   {
if(%obj.getClassName()$="Player")
{
if(%slot $= 0)
{
if(%val)
{
if($Sim::Time<%obj.client.Humvee_LastFireTime)
{
echo("Returned- Time error");
return;
}
}
%obj.HumveeShoot(%slot,%val);
}
}
   }

You're using %slot in some places and %triggerNum in others.

It shoots, but it doesn't work when I hold, and it returns a val error using the echo I set up. I have no clue what val is so I have no clue how to fix that. I am using the same code as before except with fixed triggernum and slot names.

%val is whether the button is up or down. When you press the jet button, armor::onTrigger(x, x, 4, 1); is called. When you release the jut button, armor::onTrigger(x, x, 4, 0); is called. I don't know why the shot wouldn't loop, though.

I have no clue either. The whole armor on trigger thing is really annoying, calling it when it is released, holding it down, not holding it down, it's really stupid that you have to go to great lengths to perform simple tasks. I ripped code out of shootonclick, which I dislike using due to it not mounting emitters and not shooting accurately. But at least shoot on click can actually loop.
Code: [Select]
$TP::Version=1.1; //Version number, do NOT change this unless you know what you are doing.

if($Support::ShootonclickB<$Prop::Version) {return;} //Newer version already loaded.
$Support::ShootonclickB=$TP::Version;

package ShootonclickB_Pack
{
function armor::onTrigger(%db,%obj,%slot,%val)
{
if(%obj.getClassName()$="Player")
{
if(%slot $= 0)
{
if(%val)
{
if($Sim::Time<%obj.client.SOCB_LastFireTime)
{
return;
}
}
%obj.SOCB_Shoot(%slot,%val);
}
}
return Parent::onTrigger(%db,%obj,%slot,%val);
}
};
ActivatePackage(ShootonclickB_Pack);

function repeatedVectorAddwithScale(%vecs)
{
%vec="0 0 0";
%cnt=getFieldCount(%vecs);
for(%i=0;%i<%cnt;%i++)
{
%fld=getField(%vecs,%i);
%tvec=getWords(%fld,0,3);
%scale=getWord(%fld,3);
%svec=vectorScale(%tvec,%scale);
%vec=vectorAdd(%vec,%svec);
}
return %vec;
}

function SimObject::getLeftVector(%obj)
{
return vectorCross(%obj.getEyeVector(),%obj.getUpVector());
}

function SimObject::getRightVector(%obj)
{
return vectorScale(%obj.getLeftVector(%obj),-1);
}

function Player::SOCB_Shoot(%obj,%slot,%val)
{
if(!%val) {cancel(%obj.SOCB_reshoot);return;}

%mnt= %obj.getObjectMount();
if(!isObject(%mnt))
{
return;
}

%data= %mnt.getDatablock();

%mountObj= %mnt.getMountNodeObject(%data.ShootonclickB_RequiredSlot);
if(%mountObj!$=%Obj) {return;}

if(%data.ShootonclickB)
{
//Checks OK, shoot it all.
%cnt=%data.ShootonclickB_ProjectileCount;
for(%i=0;%i<%cnt;%i++)
{
%pos= %mnt.getPosition();
%PVec= %data.ShootonclickB_Position[%i];
%VVec= %data.ShootonclickB_Velocity[%i];
%iPos= repeatedVectorAddwithScale(
%mnt.getEyeVector() SPC getWord(%PVec,0) TAB
%mnt.getLeftVector() SPC getWord(%PVec,1) TAB
%mnt.getUpVector() SPC getWord(%PVec,2)
);
%iVel= repeatedVectorAddwithScale(
%mnt.getEyeVector() SPC getWord(%VVec,0) TAB
%mnt.getLeftVector() SPC getWord(%VVec,1) TAB
%mnt.getUpVector() SPC getWord(%VVec,2)
);
%scale=%data.ShootonclickB_Scale[%i];
if(%scale$="") {%scale="1 1 1";}
%p= new Projectile()
{
dataBlock= %data.ShootonclickB_Projectile[%i];
initialPosition= vectorAdd(%pos,%iPos);
initialVelocity= %iVel;
sourceObject= %obj;
client= %obj.client;
sourceSlot= %slot;
scale= %scale;
};missionCleanup.add(%p);
}
serverPlay3d(%data.ShootonclickB_Sound,%pos);

//If hold, schedule.
if(%data.ShootonclickB_Hold)
{
//Delay according to datablocks.
%obj.SOCB_reshoot= %obj.schedule(%data.ShootonclickB_ReshootDelay,SOCB_Shoot,%slot,%val);
}
%obj.client.SOCB_LastFireTime=$Sim::Time+%data.ShootonclickB_ShootDelay/1000;
}
}

Well, I have got it all working, but if I aim at a certain angle, it shoots the vehicle it is mounted to. how would I make it so the projectile is spawned farther from the gun so it won't hit the vehicle it is mounted to?
Code: [Select]
%vector = %mount.getEyeVector(); //Get the direction they're aiming
%velocity = VectorScale(%vector, 60); //Lengthen the vector to be the speed of the projectile we want
%x = (getRandom() - 0.5) * 0.015707963; //Calculate X, Y, and Z spread randomizations
%y = (getRandom() - 0.5) * 0.015707963;
%z = (getRandom() - 0.5) * 0.015707963;
%mat = MatrixCreateFromEuler(%x SPC %y SPC %z);
%velocity = MatrixMulVector(%mat, %velocity); //Randomize the lengthened vector.
%scaleFactor = getWord(%mount.getScale(), 2); //Get the scale so we can scale the projectile.
%p = new projectile() //Create the projectile.
{
dataBlock = gunprojectile;
initialVelocity = %velocity;
initialPosition = %mount.getSlotTransform(1);
sourceObject = %obj;
sourceSlot = %triggerNum;
client = %obj.client;
scale = %scaleFactor;
};
MissionCleanup.add(%p);

Try changing the projectile's initialposition to %mount.getEyePoint();.