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 Detinator10 · Sep 07, 2014 at 05:33 PM · rotationrotateeulerangles

Help with rotation code.

Right now I'm using this code to make my sprite look at the character:

 var dx = this.transform.position.x - player.transform.position.x;
         var dy = this.transform.position.y - player.transform.position.y;
         var radians = Mathf.Atan2(dy,dx);
         var angle = radians * lookangle/ Mathf.PI;
 
         var rotateZ = Mathf.LerpAngle(this.transform.rotation.z, angle, Time.time);
         
         this.transform.eulerAngles = new Vector3(this.transform.rotation.x, this.transform.rotation.y, rotateZ);

the problem with this is that I don't want my sprite to rotate past 180 degrees because then it appears upside-down. I have code that I use to flip the sprite around however if I set the look angle to 180 it will flip but won't rotate to face the player. I need something where I can set the sprite to rotate 360 degrees but not under 180 until I flip and then I would switch it around so that it will rotate up to 180. If anyone has some better rotation code that would be appreciated (Has to work in 2D though).Thank you!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by flaviusxvii · Sep 07, 2014 at 07:11 PM

this.transform.rotation.z <-- Unless you are a mathematical genius this is not what you expect. That's the z component of a Quaternion. use this.transform.rotation.eulerAngles.z to get the z rotation.

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 zharik86 · Sep 07, 2014 at 06:54 PM

Your problem consists that you misunderstand Quaternion. See docs. Quaternion is the four-component vector which each value is in the range from 0 to 1. And eulerAngles is the three-component vector which each value is in the range from 0 to 360. So, to rotate, to use Quaternion.Euler() better:

  private var rotateZ: float = 0.0; //create global variable for save current rotation

  function myFunc() { //or maybe this code in function Update()
   var dx = this.transform.position.x - player.transform.position.x;
   var dy = this.transform.position.y - player.transform.position.y;
   var radians = Mathf.Atan2(dy,dx);
   var angle = radians * 180.0/ Mathf.PI; //I dont't know what is variable lookangle, but maybe you want translate radians to degree
   rotateZ = Mathf.LerpAngle(rotateZ, angle, 5.0*Time.deltaTime);
   this.transform.rotation = Quaternion.Euler(0, 0, rotateZ);
  }

I hope that it will help you.

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

23 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

Related Questions

Rotating a model with increments 1 Answer

Smoothing eulerAngles rotation? 1 Answer

How do I call a function containing a while loop, inside the update function? 2 Answers

Rotation on Android vs. PC 0 Answers

Is 360° the same as 0°? 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