3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1] 2

Author Topic: A scripted Gun recoil - for FPS games  (Read 4813 times)

jestermon

« on: February 02, 2011, 11:47:24 PM »
When the demo runs, press the left mouse button to shoot, and the gun will recoil.
The HoldGun function has a dual purpose. It holds the gun in position to the camera, and also does the recoil when fired.
Please do not link the gun to the camera, else it will not work.

You can change the gun offset to the camera by tweaking these variables.
float xOffs = 0.3;  //x offset to camers  (left-right)
float yOffs = -0.3;  //y offset to camers (up-down)
float zOffs = 0.5;  //z offset to camers  (forwards - backwards)

Link your objects, as required by the script

Code: [Select]
int GUN = OBJ_0;           //Add your gun here
int CAM1 = OBJ_22;         //Add your first person Cam here
int PROJECTILE = OBJ_44;   //Add your projectile here

bool RECOIL = false;        //Recoil control state
float recoilZ = 0;          //recoil offset
float recoilDelta = -0.05;  //reoild change amount per frame
float maxRecoil = 0.2;      //Max recoil Z offset

///--------------------------------------------------------------------------
void LocalToParent(int PARENT, int OBJ, Vector3 rotation, Vector3 translation)
{
   Vector3 parentLocation;
   Quaternion parentOrientation;
   Quaternion newOrientation;
   Quaternion objectOrientation;
   Vector3 forward, location, newLocation;
   float x,y,z,ud,lr,fb;
   x = rotation.x; y = rotation.y; z = rotation.z;
   fb = translation.z; lr = translation.x; ud = translation.y;
   iObjectLocation(PARENT, parentLocation);
   iObjectOrientation(PARENT,parentOrientation);
   iObjectOrientationSet(OBJ,parentOrientation);
   iVectorRotate(forward,Vector3(lr,ud,fb),parentOrientation);
   newLocation = parentLocation + forward;
   iObjectLocationSet(OBJ,newLocation);
   iQuaternionFromEulerAngles(newOrientation,x,y,z,"xyz");
   iQuaternionMultiply(newOrientation,newOrientation,parentOrientation);
   iObjectOrientationSet(OBJ,newOrientation);
}

///--------------------------------------------------------------------------
void HoldGun()
{
   float xOffs = 0.3;  //x offset to camers
   float yOffs = -0.3;  //y offset to camers
   float zOffs = 0.5;  //z offset to camers

   if(RECOIL){
      recoilZ += recoilDelta;
      if(recoilZ < -maxRecoil) recoilDelta = -1 * recoilDelta;
      if(recoilZ > 0){
         recoilZ = 0;
         recoilDelta = -1 * recoilDelta;
         RECOIL = false;
      }
   }else{
      recoilZ = 0;
   }

   LocalToParent(CAM1,GUN,Vector3(0,0,0),Vector3(xOffs,yOffs,zOffs + recoilZ));
}

///--------------------------------------------------------------------------
void Main()
{
   if(iMouseButtonClick(0)){
      RECOIL = true;
      iObjectStart(PROJECTILE);
   }
   
   HoldGun();
}

enjoy
« Last Edit: February 02, 2011, 11:57:28 PM by jestermon »

Akazi14

« Reply #1 on: February 03, 2011, 12:54:03 PM »
It works great! Thanks!  ;)
« Reply #2 on: February 03, 2011, 02:10:06 PM »
it doesn't work great for me... when i pressed the trigger, the demo exploded my 3DRAD...  ;D

must've been some  bad gunpowder in those shells  :D  :D

or...

the xhairs sprite image appeared to be missing from my download...
(i replaced it with one i already had and the project ran fine afterwards)


anywhooo... nice demo...  this will come in handy...


btw...


would ya mind a small modification...


when looking at the firing it seemed a lil unrealistic and mechanical... i mean, the gun just recoils back in one direction... a real gun would kick back and rotate back a lil too...

so did a lil quick and dirty mod of the existing code...   i added a recoilX variable and used recoilZ to set recoilX, multiplied it by 100,  and plugged that into the L2P call for the x rotation angle...

this gave the gun a lil kick up in addition to the movement backwards...
i think it looks a lil more realistic now...


here's the code... pretty much the same as before... just the addition i mentioned above...

Code: [Select]
int GUN = OBJ_0;           //Add your gun here
int CAM1 = OBJ_22;         //Add your first person Cam here
int PROJECTILE = OBJ_44;   //Add your projectile here

bool RECOIL = false;        //Recoil control state
float recoilZ = 0;          //recoil Z offset
float recoilX = 0;          //recoil X offset to rotate gun a bit when fired
float recoilDelta = -0.05;  //recoild change amount per frame
float maxRecoil = 0.2;      //Max recoil Z offset

///--------------------------------------------------------------------------
void LocalToParent(int PARENT, int OBJ, Vector3 rotation, Vector3 translation)
{
   Vector3 parentLocation;
   Quaternion parentOrientation;
   Quaternion newOrientation;
   Quaternion objectOrientation;
   Vector3 forward, location, newLocation;
   float x,y,z,ud,lr,fb;
   x = rotation.x; y = rotation.y; z = rotation.z;
   fb = translation.z; lr = translation.x; ud = translation.y;
   iObjectLocation(PARENT, parentLocation);
   iObjectOrientation(PARENT,parentOrientation);
   iObjectOrientationSet(OBJ,parentOrientation);
   iVectorRotate(forward,Vector3(lr,ud,fb),parentOrientation);
   newLocation = parentLocation + forward;
   iObjectLocationSet(OBJ,newLocation);
   iQuaternionFromEulerAngles(newOrientation,x,y,z,"xyz");
   iQuaternionMultiply(newOrientation,newOrientation,parentOrientation);
   iObjectOrientationSet(OBJ,newOrientation);
}

///--------------------------------------------------------------------------
void HoldGun()
{
   float xOffs = 0.3;  //x offset to camers
   float yOffs = -0.3;  //y offset to camers
   float zOffs = 0.5;  //z offset to camers

   if(RECOIL){
      recoilZ += recoilDelta;
      recoilX+= recoilDelta *100;
      if(recoilZ < -maxRecoil) recoilDelta = -1 * recoilDelta;
      if(recoilZ > 0){
         recoilZ = 0; recoilX=0;
         recoilDelta = -1 * recoilDelta;
         RECOIL = false;
      }
   }else{
      recoilZ = 0;
   }

   LocalToParent(CAM1,GUN,Vector3(recoilX,0,0),Vector3(xOffs,yOffs,zOffs + recoilZ));
}

///--------------------------------------------------------------------------
void Main()
{
   if(iMouseButtonClick(0)){
      RECOIL = true;
      iObjectStart(PROJECTILE);
   }
   
   HoldGun();
}



just copy and paste over the existing gun script code...


and thx for this handy bit of code... and the nice lil handgun too  ;D
i've already got some plans for this...


--Mike
« Last Edit: February 03, 2011, 02:34:42 PM by Mike Hense »
« Reply #3 on: February 03, 2011, 03:55:03 PM »
AwEsOmE!!!

jestermon

« Reply #4 on: February 03, 2011, 04:46:37 PM »
Nice touch Mike. That will add some extra realism for sure. Thanks for posting your update back,. it keeps the "open and share" concepts very much alive.

Also glad to see you are getting the hang of L2p, and understanding how much has been packed into that little nut :)
« Last Edit: February 03, 2011, 04:48:46 PM by jestermon »
« Reply #5 on: February 03, 2011, 05:04:35 PM »
Heh, sweet stuff.  Even gave me an idea for a game from messing around with the demo.   ;D
« Reply #6 on: March 24, 2011, 09:59:19 AM »
The cross-hair is missing. How can I add it  ???

I made my own and linked it to the script object but it's not working.

It just crashes . help. total n00b ???

« Reply #7 on: March 24, 2011, 04:06:51 PM »
Theres a crosshair sprite that comes with 3D Rad that has the proper alpha channel to display just the "+"
or if your homemade one has the alpha channel, -add that at 0,0 screen coordinates. Don't link it to anything. besides the cam -if you were using multiple cameras/ views

You could do a Show/Hide thing with an Event On Input -to show/hide the crosshairs.

Beyond that, more of an advance usage of a sprite crosshair; -you could have it only show when a certain weapon is displayed or even change to a different style of crosshair for different weapons ...etc...
3D Rad: The best abandonware ever!
« Reply #8 on: March 24, 2011, 04:17:06 PM »
Also, besides making your own, you can use a TextPrint with a plus sign (+) -any color or size for crosshairs. -or- different characters like :
*
x . o ...
3D Rad: The best abandonware ever!

jestermon

« Reply #9 on: March 24, 2011, 05:14:21 PM »
xHair added
7.03
« Reply #10 on: March 27, 2011, 08:40:18 PM »
that's cool!!!!

Good proyect!!!!! 8) 8) 8) 8) 8) 8) 8) ;D ;D ;D ;D
never give up :D
« Reply #11 on: June 29, 2011, 01:46:58 PM »
For some reason it only works when my gun is out of ammo??
« Reply #12 on: June 29, 2011, 02:20:04 PM »
I got it I had to change
Code: [Select]
if(iMouseButtonClick(0)){
      RECOIL = true;
      iObjectStart(PROJECTILE);
   }
to
Code: [Select]
if(iMouseButtonDown(0)){
      RECOIL = true;
      iObjectStart(PROJECTILE);
   }
« Reply #13 on: June 29, 2011, 02:35:39 PM »
I got it I had to change
Code: [Select]
if(iMouseButtonClick(0)){
      RECOIL = true;
      iObjectStart(PROJECTILE);
   }
to
Code: [Select]
if(iMouseButtonDown(0)){
      RECOIL = true;
      iObjectStart(PROJECTILE);
   }
aren't those both the same thing?
« Reply #14 on: June 29, 2011, 03:00:16 PM »
well Yea...that's why it is weird.
Pages: [1] 2