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 Ropez94 · May 27, 2012 at 05:00 AM · rotationlerpif

Simple lerp roation problem

For a game i am making i need the character to be able to turn on the y axis to point forward, right, backward, and left. To do this i am using Mathf.Lerp with if statements to allow me to change the direction to these simple four directions. whenever i do this though, the characted will only point forward (0 degrees)or backward (180 degrees), can someone tell me where I went wrong, here is the script.

#pragma strict

function Start () {

}

function Update () {

if(Input.GetKeyUp(KeyCode.UpArrow)){

 transform.rotation.y=Mathf.Lerp(transform.rotation.y,0,Time.time);

};//endif

if(Input.GetKeyUp(KeyCode.RightArrow)){

 transform.rotation.y=Mathf.Lerp(transform.rotation.y,90,Time.time);
 

};//endif

if(Input.GetKeyUp(KeyCode.DownArrow)){

 transform.rotation.y=Mathf.Lerp(transform.rotation.y,180,Time.time);

};//endif

if(Input.GetKeyUp(KeyCode.LeftArrow)){

 transform.rotation.y=Mathf.Lerp(transform.rotation.y,270,Time.time);

};//endif }

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
4

Answer by aldonaletto · May 27, 2012 at 06:04 AM

This is a recurrent problem: transform.rotation is a quaternion, and you should not change its components directly unless you know exactly what you're doing. You should use transform.eulerAngles instead: this property is what you can see in the Rotation field, in the Inspector.
But there's another problem: with this Lerp code, the object will always rotate instantaneously to the target angle (the Lerp example in the docs sucks!)
To solve both problems, you could have two variables: curAngle and destAngle; each Update, curAngle Lerp's to destAngle, and is copied to the object rotation; when a key is pressed, set the destAngle:

private var curAngle: float; private var destAngle: float = 0;

function Update () { if(Input.GetKeyUp(KeyCode.UpArrow)){ destAngle = 0;
} if(Input.GetKeyUp(KeyCode.RightArrow)){ destAngle = 90; } if(Input.GetKeyUp(KeyCode.DownArrow)){ destAngle = 180; } if(Input.GetKeyUp(KeyCode.LeftArrow)){ destAngle = 270; } // choose the shortest distance: var dif = destAngle - curAngle; if (dif > 180) curAngle += 360; if (dif < -180) curAngle -= 360; // lerp curAngle to the destAngle each Update: curAngle = Mathf.Lerp(curAngle, destAngle, Time.deltaTime); // copy curAngle to the object Y angle: transform.eulerAngles = Vector3(0, curAngle, 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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Annoying smooth rotation jitter bug 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do you smoothly transition/Lerp into a new rotation? 3 Answers

Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers

Clamped Turret Doesn't Want to Lerp the Other Way 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