- Home /
Help with Recoil for guns
Hi,
I have been trying to get my Main Camera to rotate up when ever the Fire function is called in my shooting script, just how a recoil in any game looks. I have tried multiply tacktics on this but none of them have worked. Any ideas on how when I shoot my gun, get a hold of the Main Camera (Or MouseLook on the Main Camera) and rotate it up by 5 or somthing like that? Any ideas would help. Thanks in advance!
Answer by aldonaletto · Jan 14, 2012 at 04:54 AM
As @syclamoth said, moving the camera may be impossible if it has MouseLook attached. You should "sandwich" an empty object between the player and the camera, and attach MouseLook to this empty object - this would make it possible to shake or do a recoil movement using localEulerAngles. NOTE: If you insert this object, reset the camera position and rotation, then reset this object's position and rotation!
This is a simple recoil script: attach it to the camera (or directly to the weapon) and call the function Recoil each time you effectively shoot:
var force: float = 2.5; // controls recoil amplitude var upSpeed: float = 9; // controls smoothing speed var dnSpeed: float = 20; // how fast the weapon returns to original position
private var ang0: Vector3; // initial angle private var targetX: float; // unfiltered recoil angle private var ang = Vector3.zero; // smoothed angle
function Start(){ ang0 = transform.localEulerAngles; // save original angles }
function Recoil(){ targetX += force; // add recoil force }
function Update(){ // smooth movement a little ang.x = Mathf.Lerp(ang.x, targetX, upSpeed Time.deltaTime); transform.localEulerAngles = ang0 - ang; // move the camera or weapon targetX = Mathf.Lerp(targetX, 0, dnSpeed Time.deltaTime); // returns to rest } You can call the function Recoil using SendMessage when you call your Fire function. If the script that will call Recoil is in a parent object, use BroadcastMessage("Recoil"); if it's in a child, use SendMessageUpwards("Recoil").
Thanks for the script and all the ideas!!! It works like a charm but on my gameobject called "Guns" which is the child of $$anonymous$$ain Camera. I tried switching some stuff up but it just doesnt want to work...any ideas on how to call the parent of the parent of the gun I was shooting with?
Check that..It Works like a charm when i put it on the main camera!!!!!!! Thank You So much!!! One last question though... is there a way where it keeps recoiling up until a certain point then cant recoil any higher? Any help will help!! Thanks again!
You must tweak the 3 parameters force (recoil intensity), dnSpeed (speed to return to rest position) and upSpeed (smoothing speed). In order to have a better perception about what each one does, set upSpeed to a high value (50, for instance): this will virtually null the smoothing effect, thus you can tweak the other parameters more easily (adjust upSpeed later to have a suitably smoothed movement).
If you have a $$anonymous$$imum interval between shots, force and dnSpeed together will set a maximum recoil height. If there's no apparent limit to the recoil height, you're probably calling Recoil without any interval between shots.
The best place to call the Recoil function is right after the shooting code - if you shoot with raycast, call Recoil after the if (Physics.Raycast(...)){...} - this way you will have the recoil effect only when effectively shooting (also avoids fake recoils when you run out of ammo, for instance).
Anyway, if you can't have a limited height recoil, clamp it manually:
var maxHeight = 12.0; // max deviation angle
function Recoil(){ targetX += force; // add recoil force if (targetX > maxHeight) targetX = maxHeight; }
Thank you so much!!!!! I have got it working with every gun the exact way I was planing!!! You, my friend are a life savior!!!! Thanks again and to everyone who pointed me in the right direction (syclamoth) :)
When I use this my gun spins around with the nose downwards and doesn't go back to the original position.
Answer by syclamoth · Jan 14, 2012 at 01:48 AM
If you have a 'mouselook' component directly on the camera, it won't really work. However, if you make the camera a child of the mouselook, then you can do some more interesting things.
Now that your camera can move independently of the mouseLook, you can create a 'ScreenShakeEffects' controller. On this controller, you can modify 'transform.localRotation' to make the camera move offset from the mouselook. You could even use a translation offset for shaking (for explosions or damage etc.).
Thank you for your responce and advise...So if I make another Camera under the Camera with $$anonymous$$ouseLook and use localRotation then it will work? ill try it! Thanks!
No, no, no, you don't have a camera on the mouseLook transform! Only have one camera, but put the mouselook script on a parent transform with no camera. Contrary to popular belief, you don't need a camera to use mouselook- the only thing you need a camera for is for it to make sense.
ohhhhhhhh....That clears some things up! Would an empty GameObject with mouselook do the trick? Then put all my guns under it with a new camera with no scripts...Sound right?
Thank you for putting me on the right track!!! i got it working with the folling script below! Thanks again!
Answer by simran131 · Dec 08, 2015 at 02:45 PM
void mfRecoil(){ transform.localEulerAngles = new Vector3(Random.Range(transform.localEulerAngles.x - 2f, transform.localEulerAngles.x - 3f), Random.Range(transform.localEulerAngles.y - 2f, transform.localEulerAngles.y + 2f), transform.localEulerAngles.z); } attach this script to the gameobject that you want recoil on, and call this function for recoil.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
I need horizontal Recoil to reset back to original position after player stops shooting 1 Answer
How do I animate a texture to rotate and loop around an imported object? 1 Answer
How can i rotate the aircraft elevator around the yellow axis up and down ?? as shown in the pic 0 Answers
camera rotate with limitation around x and y axis by using touch 0 Answers