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 Skyteks · Apr 21, 2017 at 10:41 PM · localrotationover time

Rotating Object around local Z axis over given time and degrees and stacking if triggered before finish

Hi there, i have an object, a revolver magazine, i want to rotate it arround its local Z axis (gun itself moves freely on all axis) about 60 degrees over 0.6 seconds when an event is triggered. The complicated part is, if the event gets triggered again before the rotation is finished, i want to add the ammount and time (stacking). Lets say it is half way done, and it gets triggered again, it should rotate 90 degrees over 0.9 seconds, from that moment on. And the rotation speed shoulnt change (always same).

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
0

Answer by Commoble · Apr 21, 2017 at 10:55 PM

This isn't as complicated as it sounds; consider it like this:

-You're applying an affect to your object.

-The effect does this: Add a particular amount of degrees to rotate over a period of time.

-The angular velocity (degrees per second) is constant, so whatever adds this effect to the object only needs to know how large of a rotation to add to it, or how long to rotate it for; one can be calculated from the other (reminder: degrees per second = degrees / seconds, and from this we get seconds = degrees / dps, and degrees = seconds * dps).

-Adding the effect to the object while it's already rotating adds an additional amount of rotation to rotate through, without resetting the rotation or changing the speed.

Finally, to bring all of this together, we think of how the Update event works. The Update is called each frame (which can be somewhere around 60 FPS if there's no performance issues), so for something that happens over a period of time, we can perform a small fragment of that thing each frame. Time.deltaTime tells us how long the previous frame lasted, and so we use this to determine how much of our task to do in a single frame.

We'll probably also need a way for other scripts to tell your object to rotate. We'll make a public AddRotation (float degrees) function that can be called from any script with a reference to this object. With all this in mind, you can add something like the following to your object's script:

 protected float degreesRemaining = 0F;
 
 public const float degreesPerSecond = 100F;
 
 public void AddRotation(float degrees)
 {
     this.degreesRemaining += degrees;
 }
 
 void Update()
 {
     if (this.degreesRemaining > 0F)
     {
         // how much the object should rotate this frame
         // degrees = degrees per second * seconds, and Time.deltaTime is a fraction of a second
         float rotationThisFrame = degreesPerSecond * Time.deltaTime;
         
         // if object would finish rotating this frame, finish rotating
         if (rotationThisFrame >= degreesRemaining)
         {
             this.transform.Rotate(new Vector3(0F, 0F, degreesRemaining);
             this.degreesRemaining = 0F;
         }
         else // otherwise, rotate the amount necessary and subtract that from the counter
         {
             this.transform.Rotate(new Vector3(0F, 0F, rotationThisFrame);
             this.degreesRemaining -= rotationThisFrame;
         }
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Random Rotation To Animation HELP! 2 Answers

How to limit sprite rotation 0 Answers

Clamping objects localrotation not working (C#) 2 Answers

can't get local rotation of object 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