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 davidwalmsley · Oct 22, 2014 at 04:56 AM · rotationtweeneasing

What is the best way to ease a rotation? (js)

I have a gameObject, Player Controller. This gameObject handles all of the movement, which basically moves you in the direction of the input relative to the screen. Inside the Player Controller I have the gameObject playerAvatar as playerConroller's child which has the model with all the animations and whatnot. (playerController ---> playerAvatar)

In combat you have option to target an enemy, in which case the playerAvatar faces the target. I used this code in the update function to accomplish this:

 targetGetRot = Quaternion.LookRotation(battleTarget.transform.position - transform.position);
 
 targetFacingRot = targetGetRot.eulerAngles.y;
 

Also in the update I have a boolean variable enemyTargeted that looks like this:

 if(enemyTargeted && !rotationRestricted){
 
 playerAvatar.transform.eularAngles.y = targetFacingRot;
 
 }

It all functions perfectly up to this point. I can turn the enemyTargeted boolean on an off to have the character face the enemy, and set the playerAvatar y value to 0 to have him facing the direction he's moving when he's not targeted.

The issue I am having is that when the character attacks or casts a spell, the targeting rotation gets turned off and he keeps his facing direction with the rotationResticted variable (boolean). However when I want to put the targeting back on I can't find an effective way to tween the rotation back to targeted. Since the enemy is moving, even when I try to use iTween there is a hiccup from where the rotation was when the tween started to the targetFacingRot when its finished. I tried to use a Lerp in the update function but the results made the playerAvatar spin in all kinds of different directions before it landed on the correct rotation.

Does anyone know an effective way to do this without making the transition look like a slideshow? Any help or nudges in the right direction would be greatly appreciated.

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

Answer by robertbu · Oct 22, 2014 at 05:38 AM

Do the 'targetGetRot' this way.

  var v = battleTarget.transform.position - transform.position;
  v.y = 0.0;
  targetGetRot = Quaternion.LookRotation(v);

Then you are going to do the rotation one of the following two ways:

 if (enemyTargeted && !rotationRestricted) {
      playerAvatar.transform.rotation = Quaternion.Slerp(playerAvatar.transform.rotation, targetFacingRot, speed * Time.deltaTime);
  
  }

...or:

 if (enemyTargeted && !rotationRestricted) {
      playerAvatar.transform.rotation = Quaternion.RotateTowards(playerAvatar.transform.rotation, targetFacingRot, speed * Time.deltaTime);
  }

'speed' is a variable you declare and assign. In the first form, typical values are 3 - 10 and produce an eased rotation. In the second form, 'speed' will be degrees per second and therefore be a constant speed rotation.

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 davidwalmsley · Oct 28, 2014 at 03:21 AM 0
Share

Thanks, this worked beautifully.

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

27 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

Related Questions

Tween to moving target 1 Answer

Rotation and Translation children parent and tween 0 Answers

Rotation Ease-In-Out with MAX Rotation Speed 1 Answer

easing camera rotation after mouse up? 0 Answers

Quadratic easing motion confusion! 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