Author Topic: Push player backwards with setVelocity  (Read 1112 times)

How exactly would you go about doing this again?

EDIT: It would also be nice to know how to detect a direction a player is facing.

you can get a player's forward vector using %player.getEyeVector();

I would suggest using that over %player.getForwardVector(), as that returns a 2D direction instead of a 3D one.

Alternatively, there is also %player.getMuzzleVector(0), which ignores when a player holds z to go into free look mode.

As far as using them with setVelocity, I believe the best implementation is

Code: [Select]
%player = <player object>;
%impulseamount = <some number>;
%player.setVelocity(VectorSub(%player.getVelocity(), VectorNormalize(%player.getMuzzleVector(0), %impulseAmount)));

This will take the player's current velocity into account, and cause them to accelerate backwards as opposed to losing all current momentum and suddenly going straight backwards.
« Last Edit: April 14, 2013, 11:01:39 PM by Nexus »

I implemented it into the code where I wanted it and saved it, but the item doesn't show up in the spawn menu now.

I implemented it into the code where I wanted it and saved it, but the item doesn't show up in the spawn menu now.

You caused a syntax error in your new code. Post it.

Quote
// Initial start up state
   stateName[0]                     = "Activate";
   stateTimeoutValue[0]             = 0.15;
   stateTransitionOnTimeout[0]       = "Ready";
   stateSound[0]               = weaponSwitchSound;

   stateName[1]                     = "Ready";
   stateTransitionOnTriggerDown[1]  = "Fire";
   stateAllowImageChange[1]         = true;
   stateSequence[1]   = "Ready";

   stateName[2]                    = "Fire";
   stateTransitionOnTimeout[2]     = "Smoke";
   stateTimeoutValue[2]            = 0.14;
   stateFire[2]                    = true;
   stateAllowImageChange[2]        = false;
   stateSequence[2]                = "Fire";
   stateScript[2]                  = "onFire";
   stateWaitForTimeout[2]         = true;
        stateSequence[1] = "%client.player.setVelocity(VectorSub(%player.getVelocity(), VectorNormalize(%player.getMuzzleVector(0), %impulseAmount)));"
   stateEmitter[2]               = LaserdanceFlashEmitter;
   stateEmitterTime[2]            = 0.05;
   stateEmitterNode[2]            = "muzzleNode";
   stateSound[2]               = LaserdanceShot1Sound;
   stateEjectShell[2]       = true;

   stateName[3] = "Smoke";
   stateEmitter[3]               = LaserdanceSmokeEmitter;
   stateEmitterTime[3]            = 0.05;
   stateEmitterNode[3]            = "muzzleNode";
   stateTimeoutValue[3]            = 0.01;
   stateTransitionOnTimeout[3]     = "Reload";

   stateName[4]         = "Reload";
   stateSequence[4]                = "Reload";
   stateTransitionOnTriggerUp[4]     = "Ready";
   stateSequence[4]   = "Ready";

};

Its most likely around this area. I know I'm going to look like an idiot when I post this and its going to be the simplest of problems able to be solved.

The issue is that you just randomly put code in the field which is supposed to specify the animation for your weapon. Since you already defined a firing method in stateFire[2] (onFire), just define yourWeaponImage::onFire( %this, %obj, %slot ), place the code there instead and properly adjust the variable names.

Thanks, but ti still has the same problem of not showing up.

Quote
//Laserdance.cs

//audio
datablock AudioProfile(LaserdanceShot1Sound)
{
   filename    = "./LaserdanceShot1.wav";
   description = AudioClose3d;
   preload = true;
};
datablock AudioProfile(bulletHitSound)
{
   filename    = "./bulletHit.wav";
   description = AudioClose3d;
   preload = true;
};


//shell
datablock DebrisData(LaserdanceShellDebris)
{
   shapeFile = "./gunShell.dts";
   lifetime = 2.0;
   minSpinSpeed = -400.0;
   maxSpinSpeed = 200.0;
   elasticity = 0.5;
   friction = 0.2;
   numBounces = 3;
   staticOnMaxBounce = true;
   snapOnMaxBounce = false;
   fade = true;

   gravModifier = 2;
};



//effects
datablock ParticleData(LaserdanceTrailParticle)
{
     gravityCoefficient   = 0;
   dragCoefficient      = 5;
   windCoefficient      = 0.0;
   constantAcceleration   = 0.0;
   lifetimeMS      = 400;
   lifetimeVarianceMS   = 0;
   spinSpeed      = 100.0;
   spinRandomMin      = 50.0;
   spinRandomMax      = 150.0;
   useInvAlpha      = false;
   animateTexture      = false;

   textureName      = "./Spark3";

   //Interpolation variables
   colors[0]   = "0.0 1 0.1 1";
   colors[1]   = "0.0 1 0.2 1";
   colors[2]   = "0.0 1 0.3 1";

   sizes[0]   = 1;
   sizes[1]   = 0.4;
   sizes[2]   = 0.0;
   times[0]   = 0.0;
   times[1]   = 0.1;
   times[2]   = 1.0;
};

datablock ParticleEmitterData(LaserdanceTrailEmitter)
{
   ejectionPeriodMS = 10;
   periodVarianceMS = 1;
   ejectionVelocity = 0;
   ejectionOffset   = 0;
   velocityVariance = 0.0;
   thetaMin         = 90;
   thetaMax         = 90;
   phiReferenceVel  = 0;
   phiVariance      = 360;
   overrideAdvance = false;
   particles = "LaserdanceTrailParticle";
   uiName = "LaserdanceTrail";

};

//effects
datablock ParticleData(LaserdanceExplosionParticle)
{
   dragCoefficient      = 5;
   gravityCoefficient   = -0.1;
   inheritedVelFactor   = 0.2;
   constantAcceleration = 0.0;
   spinSpeed      = 100.0;
   spinRandomMin      = 50.0;
   spinRandomMax      = 150.0;
   useInvAlpha      = false;
   animateTexture      = false;

   textureName      = "./Spark3";

   //Interpolation variables
   colors[0]   = "0.0 1 0.1 1";
   colors[1]   = "0.0 1 0.2 1";
   colors[2]   = "0.0 1 0.3 1";

   sizes[0]   = 3;
   sizes[1]   = 1.5;
   sizes[2]   = 0.00;
   times[0]   = 0.0;
   times[1]   = 0.1;
   times[2]   = 1.0;
};

datablock ParticleEmitterData(LaserdanceExplosionEmitter)
{
   ejectionPeriodMS = 1;
   periodVarianceMS = 1;
   ejectionVelocity = 6;
   velocityVariance = 1.0;
   ejectionOffset   = 0.1;
   thetaMin         = 80;
   thetaMax         = 80;
   phiReferenceVel  = 0;
   phiVariance      = 360;
   overrideAdvance = false;
   particles = "LaserdanceExplosionParticle";

   uiName = "Laserdance Hit";
};

datablock ExplosionData(LaserdanceExplosion)
{
   //explosionShape = "";
   lifeTimeMS = 500;



   particleEmitter = LaserdanceExplosionEmitter;
   particleDensity = 10;
   particleRadius = 0.2;

   faceViewer     = true;
   explosionScale = "1 1 1";

   shakeCamera = true;
   camShakeFreq = "20.0 22.0 20.0";
   camShakeAmp = "1.0 1.0 1.0";
   camShakeDuration = 0.0;
   camShakeRadius = 10.0;

   // Dynamic light
   lightStartRadius = 0;
   lightEndRadius = 0;
   lightStartColor = "0.5 0.5 0.1";
   lightEndColor = "0 0 0";
};


AddDamageType("Laserdance",   '<bitmap:add-ons/Weapon_Laserdance/CI_Laserdance> %1',    '%2 <bitmap:add-ons/Weapon_Laserdance/CI_Laserdance> %1',0.2,1);
datablock ProjectileData(LaserdanceProjectile)
{
   projectileShapeName = "./bullet.dts";
   directDamage        = 150;
   directDamageType    = $DamageType::Laserdance;
   radiusDamageType    = $DamageType::Laserdance;

   brickExplosionRadius = 0;
   brickExplosionImpact = true;          //destroy a brick if we hit it directly?
   brickExplosionForce  = 10;
   brickExplosionMaxVolume = 1;          //max volume of bricks that we can destroy
   brickExplosionMaxVolumeFloati ng = 2;  //max volume of bricks that we can destroy if they aren't connected to the ground

   impactImpulse        = 400;
   verticalImpulse     = 400;
   explosion           = LaserdanceExplosion;
   particleEmitter     = ""; //bulletTrailEmitter;

   muzzleVelocity      = 90;
   velInheritFactor    = 1;

   armingDelay         = 00;
   lifetime            = 4000;
   fadeDelay           = 3500;
   bounceElasticity    = 0.5;
   bounceFriction      = 0.20;
   isBallistic         = false;
   gravityMod = 0.0;

   hasLight    = false;
   lightRadius = 3.0;
   lightColor  = "0 0 0.5";

   uiName = "Laserdance Bullet";
};

//////////
// item //
//////////
datablock ItemData(LaserdanceItem)
{
   category = "Weapon";  // Mission editor category
   className = "Weapon"; // For inventory system

    // Basic Item Properties
   shapeFile = "./Famas.dts";
   rotate = false;
   mass = 1;
   density = 0.2;
   elasticity = 0.2;
   friction = 0.6;
   emap = true;

   //gui stuff
   uiName = "Laserdance";
   iconName = "./Icon_Laserdance";
   doColorShift = true;
   colorShiftColor = "0.25 0.25 0.25 1.000";

    // Dynamic properties defined by the scripts
   image = LaserdanceImage;
   canDrop = true;
};

////////////////
//weapon image//
////////////////
datablock ShapeBaseImageData(LaserdanceImage)
{
   // Basic Item properties
   shapeFile = "./Famas.dts";
   emap = true;

   // Specify mount point & offset for 3rd person, and eye offset
   // for first person rendering.
   mountPoint = 0;
   offset = "0 0 0";
   eyeOffset = 0; //"0.7 1.2 -0.5";
   rotation = eulerToMatrix( "0 0 0" );

   // When firing from a point offset from the eye, muzzle correction
   // will adjust the muzzle vector to point to the eye LOS point.
   // Since this weapon doesn't actually fire from the muzzle point,
   // we need to turn this off. 
   correctMuzzleVector = true;

   // Add the WeaponImage namespace as a parent, WeaponImage namespace
   // provides some hooks into the inventory system.
   className = "WeaponImage";

   // Projectile && Ammo.
   item = BowItem;
   ammo = " ";
   projectile = LaserdanceProjectile;
   projectileType = Projectile;

   casing = LaserdanceShellDebris;
   shellExitDir        = "1.0 -1.3 1.0";
   shellExitOffset     = "0 0 0";
   shellExitVariance   = 15.0;   
   shellVelocity       = 7.0;

   //melee particles shoot from eye node for consistancy
   melee = false;
   //raise your arm up or not
   armReady = true;

   doColorShift = true;
   colorShiftColor = LaserdanceItem.colorShiftColo r;//"0.400 0.196 0 1.000";

   //casing = " ";

   // Images have a state system which controls how the animations
   // are run, which sounds are played, script callbacks, etc. This
   // state system is downloaded to the client so that clients can
   // predict state changes and animate accordingly.  The following
   // system supports basic ready->fire->reload transitions as
   // well as a no-ammo->dryfire idle state.

   // Initial start up state
   stateName[0]                     = "Activate";
   stateTimeoutValue[0]             = 0.15;
   stateTransitionOnTimeout[0]       = "Ready";
   stateSound[0]               = weaponSwitchSound;

   stateName[1]                     = "Ready";
   stateTransitionOnTriggerDown[1]  = "Fire";
   stateAllowImageChange[1]         = true;
   stateSequence[1]   = "Ready";

   stateName[2]                    = "Fire";
   stateTransitionOnTimeout[2]     = "Smoke";
   stateTimeoutValue[2]            = 0.14;
   stateFire[2]                    = true;
   stateAllowImageChange[2]        = false;
   stateSequence[2]                = "Fire";
   stateScript[2]                  = "onFire";
   stateWaitForTimeout[2]         = true;
   stateEmitter[2]               = LaserdanceFlashEmitter;
   stateEmitterTime[2]            = 0.05;
   stateEmitterNode[2]            = "muzzleNode";
   stateSound[2]               = LaserdanceShot1Sound;
   stateEjectShell[2]       = true;

   stateName[3] = "Smoke";
   stateEmitter[3]               = LaserdanceSmokeEmitter;
   stateEmitterTime[3]            = 0.05;
   stateEmitterNode[3]            = "muzzleNode";
   stateTimeoutValue[3]            = 0.01;
   stateTransitionOnTimeout[3]     = "Reload";

   stateName[4]         = "Reload";
   stateSequence[4]                = "Reload";
   stateTransitionOnTriggerUp[4]     = "Ready";
   stateSequence[4]   = "Ready";

};

function LaserdanceImage::onFire(%this,%obj,%slot)
{
   if(%obj.getDamagePercent() < 1.0)
      %obj.playThread(2, shiftAway);
   Parent::onFire(%this,%obj,%slot);
   %impulseAmount = 80;
   %player = %client.player;
   %player.setVelocity(VectorSub(%player.getVelocity(), VectorNormalize(%player.getMuzzleVector(0), %impulseAmount)));
}

This most likely won't fix your thing not showing up, but this problem will come up sooner or later.

Replace:
function LaserdanceImage::onFire(%this,%obj,%slot)
{
   if(%obj.getDamagePercent() < 1.0)
      %obj.playThread(2, shiftAway);
   Parent::onFire(%this,%obj,%slot);
   %impulseAmount = 80;
   %player = %client.player;
   %player.setVelocity(VectorSub(%player.getVelocity(), VectorNormalize(%player.getMuzzleVector(0), %impulseAmount)));
}

With:
Quote
function LaserdanceImage::onFire(%this,%obj,%slot)
{
   if(%obj.getDamagePercent() < 1.0)
      %obj.playThread(2, shiftAway);
   Parent::onFire(%this,%obj,%slot);

   %impulseAmount = 80;
   %obj.setVelocity(VectorSub(%obj.getVelocity(), VectorNormalize(%obj.getMuzzleVector(0), %impulseAmount)));
}
« Last Edit: April 15, 2013, 07:48:55 AM by Honorabl3 »

This most likely won't fix your thing not showing up, but this problem will come up sooner or later.

Replace:
With:

The weapon shows up now, buut...

It doesn't push back the player when fired.

You don't need to post the part of your code with emitter datablocks. Lots of people do it and its just an unnecessary pagestretch

Bumping. Still haven't solved the problem of pushing backwards.

Bumping. Still haven't solved the problem of pushing backwards.

Add some echoes into your code to make sure it is executing correctly while firing the gun.

You're using vectorNormalize wrong. It has only one argument, a vector which it converts to a unit vector (which getMuzzleVector already returns anyway). You'll then want to scale that vector using vectorScale(vector, scale). I recommend you split that line into several; nesting a ton of vector functions within each other makes a mess.

You're using vectorNormalize wrong. It has only one argument, a vector which it converts to a unit vector (which getMuzzleVector already returns anyway). You'll then want to scale that vector using vectorScale(vector, scale). I recommend you split that line into several; nesting a ton of vector functions within each other makes a mess.

Whoops, that's my bad