Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by ThatUnityNoob · Jan 14, 2012 at 01:16 AM · rotaterecoil

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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").

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThatUnityNoob · Jan 14, 2012 at 01:06 PM 0
Share

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?

avatar image ThatUnityNoob · Jan 14, 2012 at 01:23 PM 0
Share

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!

avatar image aldonaletto · Jan 14, 2012 at 05:18 PM 0
Share

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; }

avatar image ThatUnityNoob · Jan 14, 2012 at 06:28 PM 0
Share

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) :)

avatar image Scruubbi · Jun 14, 2013 at 03:40 PM 0
Share

When I use this my gun spins around with the nose downwards and doesn't go back to the original position.

Show more comments
avatar image
3

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.).

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThatUnityNoob · Jan 14, 2012 at 02:00 AM 0
Share

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!

avatar image syclamoth · Jan 14, 2012 at 02:04 AM 0
Share

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.

avatar image ThatUnityNoob · Jan 14, 2012 at 02:25 AM 0
Share

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?

avatar image ThatUnityNoob · Jan 14, 2012 at 01:24 PM 0
Share

Thank you for putting me on the right track!!! i got it working with the folling script below! Thanks again!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges