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 /
  • Help Room /
avatar image
0
Question by leonardherndon · Jan 10, 2016 at 12:39 AM · rotationprogrammingdistancegameplay

Rotate Object based on distance to destination

I am making a runner game where the player ship switches lane by the swipe of the player.

I would like make the player ship bank left and right during this lane switch, and also return to normal by the time it reaches it's destination. Here is a diagram that shows what I am looking to accomplish: Diagram

I have calculated the distance of between the current lane and the destination lane. I have also calculated the current distance of the player ship while it is switching lanes. I have messed with RotateTowards and transform.rotation, but I have NO clue on how to apply this to rotation equation to get the result I am looking for.

Any help pointing me in the right direction would be greatly appreciated. Thanks!

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
2
Best Answer

Answer by taylank · Jan 10, 2016 at 03:49 AM

You will likely want to use the Quaternion.Lerp method. The Lerp method returns a linearly interpolated value between the to and from values, based on the t variable you supply. So if I do Lerp(0, 100, 0.5), I'd get 50. If I used .75 I'd get 75, etc.

Your "from" value is the neutral rotation you want when the ship is not banking. The "to" value is the maximum angle you want the ship to tilt to.

The target rotation at maximum tilt can be expressed as a Quaternion with:

 // assuming 65 degrees is the maximum tilt you want
     var targetRot = Quaternion.AngleAxis(65, shipTransform.forward); 

Based on where you are in between the lanes, you will lerp back and forth. When you are on either lane, you will want

 transform.rotation = Quaternion.Lerp(defaultRotation, targetRot, 0);

When you are right in the middle of two lanes, you will want

 transform.rotation = Quaternion.Lerp(defaultRotation, targetRot, 1);

For anywhere else, you need to express the current location of the ship as a fraction of the distance to the middle, and use that as the last variable in the Lerp method.

For instance, at 25% and 75% marks, you would use 0.25f;

You can also experiment with Quaternion.Slerp to see if it looks better.

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 leonardherndon · Jan 10, 2016 at 08:29 PM 0
Share

With the use of a $$anonymous$$athf.Sin this has worked beautifully. Thanks!

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

35 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

Related Questions

Ambient Emitter inside a BoxCollider - Rotation not recognized 0 Answers

Why does RotateTowards rotate in an "orbit" rather than on the spot? Help me understand! 0 Answers

Rotation in an if statement 1 Answer

Changing player co-ordinates 0 Answers

Rotating a 3D object with movement along a 2D background at an angle 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