- Home /
Using recoil on a Gun?
Today I want to know, how would I go about recoil on a gun? My gun is a child of a weapons object, that's a child of the camera. Here's my shooting code:
var bloodPrefab: GameObject; // drag the blood prefab here, if any
var sparksPrefab: GameObject; // drag the sparks prefab here, if any
var muzzleFlash : GameObject;
var gun : Transform;
var target : Transform;
var MaxBullets = 9;
var Bullets = 9;
var Clip = 10;
var TPB = 0.1;
function Update(){
if (Input.GetButtonDown("Fire1")){
if (Bullets > 0){
if (Clip > 0){
Shoot();
Bullets -= 1;
}
}
}
if (Bullets == 0){
if (Clip >= 1){
Clip -= 1;
Bullets = MaxBullets;
}
if (Clip == 0){
Bullets = 0;
}
}
OnGUI();
}
function Shoot(){
if (shotSound) audio.PlayOneShot(shotSound); // play the shot sound
var hit: RaycastHit;
MuzzleFlashOn();
if (Physics.Raycast(transform.position, transform.forward, hit)){
// prepare rotation to instantiate blood or sparks
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if (hit.transform.tag == "Enemy"){ // if enemy hit...
if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); // make it bleed...
hit.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver); // and consume its health
} else { // otherwise emit sparks at the hit point
if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
}
}
Recoil();
}
function MuzzleFlashOn()
{
var muzzleFlashLength = 0.1;
muzzleFlash.active = true;
yield WaitForSeconds(muzzleFlashLength);
muzzleFlash.active = false;
}
function Recoil(){
//Camera.main.transform.rotation.z = 10;
Camera.main.transform.localEulerAngles = new Vector3(0, 0, 30);
print("Recoil!");
}
function OnGUI () {
GUI.Box (Rect(100,0,100,50), "Bullets:" + " " + Bullets);
GUI.Box (Rect (0,0,100,50), "Clip:" + " " + Clip);
//GUI.Box (Rect (100,0,100,50), "Clip:" + " " + Clip);
}
I have tried a few code samples but they didn't seem to work.
Every time you shoot you could just change the x rotation of your camera.
How would I go doing that? I know i need to create a variable for the camera to transform, but how would I change the Z rotation? forgot that code.
Camera.main.transform.rotation.z = whatever
should work (in UnityScript. C# is a bit stricter).
You could always make an animation of your gun recoiling, then make sure that that script above is attached to the actual gun object itself, then attach the recoil animation and in the script above in the Shoot() function, add animation.Play("gunRecoilAnimation"); Then make sure on the animation itself that "loop" is not on. That would be one way you could achieve this, I did this in one of my games when I wanted the slide of a pistol to cock (slide back and forth really quickly) each time the player shot the gun!!!!
Not working.
function Recoil(){
Camera.main.transform.rotation.z = 10;
}
I call that function at the end of Shoot().
Answer by AlphaSniper2001 · Mar 21, 2016 at 11:21 PM
M1 Garands and Mosins use 'clips', most modern day guns use magazines.
Your answer
Follow this Question
Related Questions
FPS Gun Recoil (Accuracy) 1 Answer
CS:GO-like recoil 1 Answer
How can I convert this script to automatic fire? 2 Answers
Recoil with the FPS controller 1 Answer
Camera Recoil,Please help me. 2 Answers