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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by swterr · Mar 11, 2014 at 05:49 AM · rotationtransform.rotaterecoilsmoothly

Recoil function: smoothly rotation

Hello! I am writing recoil function. Here is the code: (recoil() called each shot)

     public GameObject RecoilCamera;
     public float RecoilPlus,RotationSpeed;    
 
 
 void Recoil(){    
         RecoilCamera.transform.Rotate (RecoilPlus,0,0);
     }



How do I rotate it smoothly?

Thanks in advance, any help is appreciated

Comment
Add comment · Show 2
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 ShadoX · Mar 11, 2014 at 08:00 AM 0
Share

Perhaps http://unity3d.com/learn/tutorials/modules/beginner/scripting/lerp might help ?

avatar image swterr · Mar 11, 2014 at 08:19 AM 0
Share

@ShadoX, can you give me an example of using Lerp in my code?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wibble82 · Mar 11, 2014 at 08:20 AM

You probably don't need to do an actual slerp or lerp here. I'd write it a little more like the stuff below. Whatever method you use you'll need to know the base 'unrecoiled' rotation, and the time you've been recoiling for.

your code would be something like:

  • if base_rotation is your camera's unrecoiled rotation

  • recoil_start_time is when you started recoiling

  • recoil_length is how long you want to recoil for

  • recoil_size is how much you want to rotate by

I would go along the lines of, each frame:

 //how long we've been recoiling
 float time_recoiling = Time.time-recoil_start_time;
 
 //a value from 0 to 1, which is 0 at the start of a recoil, and 1 at the end of the recoil
 float interpolator = time_recoiling / recoil_length;
 
 if(interpolator >= 0 && interpolator <= 1)
 {
     //cosine wave interpolator - starts at 1, goes smoothly through to -1 then back to 1
     float cos_interpolator = Mathf.Cos(interpolator*Mathf.PI*2.0f);
 
     //tweak it so it goes from 0, to 1 then back to 0
     cos_interpolator = 1-(cos_interpolator+1)/2;
 
     //now set the rotation to be the base rotation, plus some x axis rotation based on our interpolator
     RecoilCamera.transform.rotation = base_rotation*Quaternion.Euler(cos_interpolator*recoil_size);
 }
 else
 {
     //maybe do something if not recoiling? depends on your game!
 }

Note this code has 2 purposes:

  • it applies a change in angle over time as requested

  • it also uses a cosine function, to ensure the recoil smoothly transitions in and out, rather than jumping around. you could fiddle with how you choose that interpolator to get the effect you want.

a simpler interpolator if you don't want the cosine might just be: interpolator = interpolator < 0.5 ? interpolator*2 : 1-(interpolator-0.5)*2;

that's like a triangle - it zips up then straight back down. you could even get cleverer and make it transition differently going up than down. Whatever happens, you're just trying to pick a value that goes from 0, to 1, then back to 0 again. I often use excel to work out these graphs!

hope that helps - sorry if the code isn't perfect - no compiler here :)

Comment
Add comment · Show 2 · 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 swterr · Mar 11, 2014 at 09:04 AM 0
Share

Wow, it code a very hard to my simple game :)

avatar image wibble82 · Mar 11, 2014 at 05:41 PM 0
Share

Tried to make it as simple as possible - nice animation over multiple frames takes a bit of maths though!

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

22 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

Related Questions

How to instantiate an object with a set rotation in one axis, but the other two axes inherited from the parent? 1 Answer

horizontal recoil reset with slerp 1 Answer

Transform.rotate gives different result to rotation in inspector 0 Answers

Rotate Object Around Local X Axis , then Local Y Axis, then Local Z Axis (like a turtle) 0 Answers

Quaternions and EulerAngles 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