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
0
Question by chrisall76 · Jun 13, 2012 at 09:10 AM · javascriptfpsgunrecoil

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.

Comment
Add comment · Show 11
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 FLASHDENMARK · Jun 13, 2012 at 09:16 AM 0
Share

Every time you shoot you could just change the x rotation of your camera.

avatar image chrisall76 · Jun 14, 2012 at 04:26 AM 0
Share

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.

avatar image asafsitner · Jun 14, 2012 at 08:06 AM 0
Share

Camera.main.transform.rotation.z = whatever should work (in UnityScript. C# is a bit stricter).

avatar image Grady · Jun 14, 2012 at 09:11 AM 0
Share

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

avatar image chrisall76 · Jun 14, 2012 at 08:05 PM 0
Share

Not working.

 function Recoil(){
     Camera.main.transform.rotation.z = 10;
 }

I call that function at the end of Shoot().

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AlphaSniper2001 · Mar 21, 2016 at 11:21 PM

M1 Garands and Mosins use 'clips', most modern day guns use magazines.

Comment
Add comment · Show 1 · 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 Cherno · Mar 21, 2016 at 11:32 PM 1
Share

That's right, but it's no answer to the question.

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

10 People are following this question.

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

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


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