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 minip · Jan 13, 2016 at 07:40 PM · rotatespheresmooth

Rotate smoothly on sphere

Hi, I try to rotate my player on the y-axis smoothly when I turn left(y=-45) or right(y=45) and it feels like I've tried all the examples but my player just starts spinning. I guess one of the problems is that my player is moving constantly on a globe (sphere) and the rotation is constantly changing?

The closest I got is this but when I turn left it rotates the wrong way:

     protected Vector3 leftRot = new Vector3(0, -45, 0);
     protected Vector3 rightRot = new Vector3(0, 45, 0);
     protected Vector3 normalRot= new Vector3(0, 0, 0);
 
   void Update()
     {
         currentRotation = RendererGO.transform.localEulerAngles;
         float LeftRight = 0;
         if (Input.touchCount > 0)
         {
             // touch x position is bigger than half of the screen, moving right
             if (Input.GetTouch(0).position.x > Screen.width / 2)
                 LeftRight = 1;
             // touch x position is smaller than half of the screen, moving left
             else if (Input.GetTouch(0).position.x < Screen.width / 2)
                 LeftRight = -1;
  }
 
         moveDir = new Vector3(LeftRight, 0, moveSpeed).normalized;
         //Rotations
         if (LeftRight == -1){
             this.transform.localEulerAngles = Vector3.Lerp(currentRotation, leftRot, Time.deltaTime * rotatesSpeed);
 }
         else if (LeftRight == 1){
             this.transform.localEulerAngles = Vector3.Lerp(currentRotation, rightRot, Time.deltaTime * rotatesSpeed);
         }
         else{
             this.transform.localEulerAngles = Vector3.Lerp(currentRotation, normalRot, Time.deltaTime * rotatesSpeed);
         }
     }

I've tried som examples that shows Quaternion but I do not succeed... I would love to get some help with this one.

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 taylank · Jan 13, 2016 at 09:05 PM 0
Share

Can you please add the part where you are listening to user input and set the value for leftRot?

avatar image minip taylank · Jan 14, 2016 at 09:04 AM 0
Share

I have updated the code, I hope it's not too messy

avatar image taylank minip · Jan 14, 2016 at 06:04 PM 0
Share

Thanks for the extra info. O$$anonymous$$, I haven't had time to test this on my computer but here are my suggestions:

  • You are listening for touchCount > 0. So if my finger touches the screen for more than a single frame, the rotation will keep getting applied over and over. Try using (if Input.Get$$anonymous$$ouseButtonDown(0) ) ins$$anonymous$$d. This will work for touch as well and it is easier to use in general as long as you do not care for mul$$anonymous$$ch functionality in your project (i.e. pinch to zoom, etc)

  • Because you are using localEulerAngles, the rotation is relative to the parent transform. Is this object parented to anything or is it free standing? In the latter case it may be so that as the transform rotates, so does the 45 degree offset. If your goal is to clamp the player direction between certain angles in the world you may want to use transform.eulerAngles ins$$anonymous$$d for world rotation.

Show more comments

2 Replies

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

Answer by taylank · Jan 15, 2016 at 12:30 AM

When you're dealing with rotation as vectors, lerp can be difficult to work with due to ambiguity in which direction to lerp in. For instance if you're at angle 0 and you want to go 30, that's fine, but if you want to go -30 instead that can be interpreted as 330 and a long lerp from 0 to 330 clockwise.

I'd recommend using Mathf.LerpAngle instead. There is a handy example you can use in the documentation.

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
avatar image
0

Answer by minip · Jan 15, 2016 at 10:34 PM

Thank you @taylank!

The following did the trick:

 float angle = Mathf.LerpAngle(RendererGO.transform.localEulerAngles.y, maxAngle, Time.deltaTime * rotatesSpeed);
 RendererGO.transform.localEulerAngles = new Vector3(0, angle, 0);

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

How would you rotate smoothly? 0 Answers

How to fix lighting on spheres made in Blender? 1 Answer

How do I smooth motion over a cube connection? 0 Answers

Smooth rotation of cube on mouse click with no interruption, improve? 1 Answer

Why isn't my object lerping ? C# 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