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
enjoy
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