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 William_Weaver · May 05, 2014 at 07:23 AM · rotatespeedlocalrotationfromtorotation

Rotate object from localRotation A to localRotation B at rate C.

Afternoon all!

This should be simple, yet I seem to be struggling with it. Scenario is I have a capsule character, a child RotationPivot empty inside him, and a WeaponPoint (child of RotationPivot) outside him.

All I want to do, is if(Input.GetKeyDown("mouse 0")), smoothly rotate RotationPivot from local (0,0,0) to local (0,-180,0) at a variable speed, and stop. Doing so would be simple script-based swing of the WeaponPoint across the front of the Capsule. Like I said, should be simple. :p

I've looked at Quaternion.RotateTowards and Vector3.RotateTowards, and seem to be having trouble getting them to work right. (Put simply, they're not moving anything at all.) Is there a better function to use for this, or am I simply inept? lol

Quaternion.Lerp looks like a good candidate, so I'm testing that now.

Comment
Add comment · Show 4
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 William_Weaver · May 05, 2014 at 06:57 AM 0
Share

As I suspected, I got it working with Quaternion.Lerp acceptably. If there's a better way though, I'm all ears. :p

avatar image Ed unity · May 05, 2014 at 04:06 PM 0
Share

You should post the solution to your problem as an answer. That was others can see exactly what you did if they have the same issue and others can still post other solutions.

avatar image William_Weaver · May 05, 2014 at 09:40 PM 0
Share

What I have 'works', but the question is specifically to ask if there's a better way. This one has a $$anonymous$$or issue that it slows down as it approaches rotation B.

avatar image CaioRosisca · May 05, 2014 at 09:48 PM 0
Share

Quaternion.Lerp is exactly what you are looking for

2 Replies

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

Answer by William_Weaver · May 06, 2014 at 02:03 AM

Apologies for the vague answer. It was a year ago when I was brand new to scripting. ^_^; I've pulled up the full script I used after I got it working, and I can confidently say that I would do it all differently. My code was just... bad. So I won't confuse the subject by posting it all here. BUT! Here's the important part that made the pivot rotate:

 function SwingWeapon()
 {
         swingEnd = Quaternion.Euler(0,180,0);
         swingDuration = 0.35;
                 while(swingDuration > 0)
                 {
                     swingDuration -= Time.deltaTime;
                     swingPivot.localRotation = Quaternion.Lerp(swingPivot.localRotation, swingEnd, swingSpeed);
                     yield;
                 }
 }

Using other helper functions to receive click input and call this function, the above made the pivot (a variable of type transform, assigned in the inspector to the empty transform mentioned in the original post) rotate from it's current rotation to the specified end rotation.

You'll want to look up Lerp and Quaternion.Euler, but it's my understanding the the mechanic I was working towards is usually handled by rigging and animation. This was a very rough workaround to make a simple character swing a weapon horizontally in front of it 180 degrees.

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 CaioRosisca · May 06, 2014 at 02:13 PM 0
Share

If you want a constant speed movement use Lerp, if you want it to be slower towards the start and the end use Slerp.

avatar image andyastro · Sep 19, 2015 at 09:53 AM 0
Share

Can you please post a line example on how you achieved that? It is, on how to use Quaternin.Lerp to do that? I am looking for the exact same solution and would rather avoid having to post a duplicate question, in case you can provide the solution you had found. Thanks!

avatar image
0

Answer by andyastro · Sep 19, 2015 at 08:55 PM

You should always post a detailed answer when you find one, in case other people have the same question and should not suffer all over again to find a proper solution. So, in case anyone else wonder how did you use Quaternion.Lerp to achieve what was asked in the OP, I send here an example I am currently employing:

  //targetedlocalRotation should be a Quaternion:
  go.transform.localRotation = Quaternion.Lerp(go.transform.localRotation, targetedolcaolRotation,speed);
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

23 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

Related Questions

Controlling the speed of Vector3.RotateTowards 0 Answers

FromToRotation - Smoothly! 1 Answer

Smoother rotation when following a joystick axis 0 Answers

problem with the rotation of an object 1 Answer

How to rotate object by X degrees with Y speed C# 1 Answer


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