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 thoorne · Apr 02, 2013 at 06:15 PM · rotationaxisjoysticklookatflip

Rotate with joystick - LookAt axis flipping

I want to control rotation in only one axis (X) via joystick. I mean, if joystick is at top, function returns Vector2(0.0,1.0) and box is also "looking" at this point, when left returns (-1.0,0.0). I wrote function like this:

function On_JoystickMove(MovingJoystick : MovingJoystick) { Debug.Log(MovingJoystick.joystickAxis); player.transform.LookAt(Vector3(MovingJoystick.joystickAxis.x,MovingJoystick.joystickAxis.y,player.transform.position.z)); }

But in some cases ex.(1.0,0.0) gameobject 'player' flips in Y axis by 180 degrees. I tried many solutions posted here but actually none of them worked correctly.

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 LyanApps · Apr 02, 2013 at 06:40 PM

I assume you are making just a 2D platform game or something similar? Have you tried just setting the angle based on the input?

 //You may have to play around with/switch the y and x
 var angle = Mathf.Atan2(input.y,input.x) * Mathf.Rad2Deg;
 player.transform.rotation = Quaternion.Euler(angle, 0, 0);
Comment
Add comment · Show 4 · 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 thoorne · Apr 02, 2013 at 06:54 PM 0
Share

Thank you! Works good and actually solved my problem but when im passing values like (1.0,0.0) i have something like 'smooth spikes' or disturbances.

avatar image taranggodhari thoorne · Sep 07, 2016 at 05:44 AM 0
Share

for smooth transition what i used is Quaternion.Lerp and it works like charm.

Here's my code make changes as per your need.

  atan2 = $$anonymous$$athf.Atan2(myJoystick.InputDirection.y, myJoystick.InputDirection.x) * $$anonymous$$athf.Rad2Deg;
     
     transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.Euler (0, 0, (atan2-90)), trackingSpeed * Time.deltaTime); 
 
avatar image LyanApps · Apr 02, 2013 at 07:02 PM 1
Share

For smooth transitions you'll have to look into Lerp or Slerp functions. I would include it but I don't have much experience using those. Because you're only messing with one angle you can try something like:

 $$anonymous$$athf.Lerp(player.transform.rotation.eulerAngles.x , angle, Time.time);

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Lerp.html

Just saw this too:

http://docs.unity3d.com/Documentation//ScriptReference/$$anonymous$$athf.LerpAngle.html

avatar image animalphase · Sep 14, 2013 at 05:30 AM 0
Share

Thank you! I have been searching all over for this solution, and finally stumbled across this post.

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

13 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

Related Questions

LookAt() with X axis 1 Answer

Local child rotation and transform.LookAt 2 Answers

[Please!] Noob need's help with planet gravity movement. 2 Answers

How can I get my enemy to rotate to look at my character in my 2D game?? 2 Answers

Flip over an object (smooth transition) 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