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 Chippers · Apr 11, 2013 at 11:59 AM · c#lerptranslateeuleranglestransform.rotation

Using Mathf.LerpAngle to gradually rotate an objects x position

Hi, i'm trying to make an object that can rotate on the x axis back and forth 180 degrees and whilst the player is not pushing the rotate button and if it's not level(which is a 0 value) to lerp back into a level position.

However with my code it moves back instantly to a level position, am i using lerp wrong? is there a different way i can achieve this without using a for loop?

Here's my code

 using UnityEngine;
 using System.Collections;
 
 public class PlayerInput : MonoBehaviour {
     public float moveSpeed;
 
     
     private float horizontal;
     private float verticle;
     private float rotate;
     private Transform startingTransform;
     
     void Awake(){    
         startingTransform = transform;
     }
     
     void Update(){
         
         //get axis values
         horizontal = Input.GetAxis ("Horizontal") * moveSpeed;    
         verticle = Input.GetAxis ("Vertical") * moveSpeed;    
         rotate = Input.GetAxis ("Vertical") * moveSpeed;    
         //make time constant
         horizontal *= Time.deltaTime;
         verticle *= Time.deltaTime;
         //translate player
         transform.Translate(horizontal,0,0,Space.World);
         transform.Translate(0,verticle,0,Space.World);
         
         //rotate character model within 180 degrees
         if(transform.rotation.x < 0.5f && transform.rotation.x > -0.5f){
             
             transform.Rotate(rotate,0,0);
         }
         //if the player is not leveled out and is not rotateing start the rotate them back.
         if(transform.rotation.x != 0.0f && Input.GetAxis ("Vertical") == 0.0f){
     
             float angle = Mathf.LerpAngle(360,startingTransform.rotation.x, 100 * Time.deltaTime);
             transform.eulerAngles =new Vector3(angle,0,0);
         
         }
         
     
     
 }

Thanks for your time.

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

Answer by robertbu · Apr 12, 2013 at 05:32 AM

Transform.rotation is a Quaternion...a 4D construct in which the values don't represent angles in the way you are using them here. From the reference: [Quaternions] are based on complex numbers and are not easy to understand intuitively. Thus you almost never access or modify individual Quaternion components (x,y,z,w); Instead, you do have access to Transform.eulerAngles. But eulerAngles also have their issues. Again from the reference: Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.

For any "physical" rotation, there are multiple eulerAngle rotations, so reading eulerAngles will not necessarily give you the values you expect. For example you could set your eulerAngles to (180,0,0) and immediately read the value and get (0,180,180) back.

One way to deal with eulerAngles is to think of them as write-only...keep your own Vector3 representing the rotation and assign its values each frame.

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 Chippers · Apr 12, 2013 at 10:45 AM 0
Share

Thanks for that got it working, I ended up changing the $$anonymous$$athf.Lerp Angle to Quaterion.Lerp so it ended up looking like this, for anyone interested.

    transform.rotation = Quaternion.Lerp(transform.rotation, startingRotation, Time.deltaTime * moveSpeed);

Thanks for your help!

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

10 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

Related Questions

I'm using TRANSLATE but want to use LERP. C# 2 Answers

Vector2.Lerp not working properly. Horizontal jittering when not moving horizontally at all! 1 Answer

Rotation jumping to a different rotation after rotating a certain amount 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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