Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 h00man · Jun 27, 2018 at 12:08 PM · recoil

pistol recoil rotation

hey, i been trying to make a gun recoil for my pistol and i have a script attached to my gun model.the script works fine somehow... it rotates on x axis about 15 degree. but as i shoot more and more, the gun rotates less and less until it's rotation becomes absolute zero. i created a GIF to show the issue. http://s6.uplod.ir:182/d/2k22yze74hvhuf6tpwoij6jhvjz43kmq7soreocqgk3u7tb2ezzyi6knvdjshyn7mibngw7g/GIF_20180627_162008.gif and here is the script : public class recoiltesti : MonoBehaviour { Vector3 recoilSmoothDampVelocity; public float recoliEnergy;

     public float FireRate;
     private float NextTimeToFire = 0f;
     public float recoilAngelSmoothDampVelocity;
     public Quaternion originalRotationValue; // declare this as a Quaternion
     public float rotationResetSpeed = 0.1f;
     float recoilAngle;
     void Start () {
         originalRotationValue = transform.rotation;
     }
 
     // Update is called once per frame
     void LateUpdate() {
         // animate recoil
         recoilAngle = Mathf.SmoothDamp(recoilAngle ,0 ,ref recoilAngelSmoothDampVelocity,0.1f);
 
         transform.localEulerAngles = transform.localEulerAngles + -Vector3.forward *recoilAngle;
 
 
         if (Input.GetButtonDown("Fire1") && Time.time >= NextTimeToFire) {
             NextTimeToFire = Time.time + 1f / FireRate;
 
 
             shoot ();
 
         }
         else{
             resetRot ();
 
         }}
 
 
     public void shoot ()
     {
         
         recoilAngle += 15;
         recoilAngle = Mathf.Clamp (recoilAngle, 0, 25);
 
     }
 
     public void resetRot ()
     {
         transform.rotation = Quaternion.Lerp (transform.rotation, originalRotationValue, Time.time * rotationResetSpeed);
 
     }
 }
 
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

1 Reply

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

Answer by Priyanka-Rajwanshi · Jun 28, 2018 at 03:39 PM

@h00man

You are using Time.time in your function resetRot. As game progresses, this value becomes large. Thus the gun returns to its original value abruptly. You could use:

   transform.rotation = Quaternion.Lerp (transform.rotation, originalRotationValue, (Time.time - initialTime) * rotationResetSpeed);

where initialTime is a float value that can be set in shoot () as

   initialTime = Time.time;
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 h00man · Jun 29, 2018 at 07:51 AM 0
Share

thanks. it worked :)

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

86 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Managing different weapons in scripting. 4 Answers

How can i modify this script to add vertical and also horizontal camera recoil? 0 Answers

Recoil script help 2 Answers

Aiming-recoil animation problem 0 Answers

Vector3.Lerp not working unless interpolant t is 1. 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