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 WorpeX · May 31, 2015 at 08:16 AM · c#2dspriterotate object

Top Down Sprite Facing Mouse

I have a top down game where my character sprite rotates towards the mouse and faces it when clicked. However, the character seems to have no idea which way is his face and which way is his butt. Sometimes he will face the mouse with his face, other times his butt. Here is the code i'm using for rotation:

         Quaternion newRotation = Quaternion.LookRotation(InputTouch.hitpoint - player.transform.position);
         newRotation.x = 0f;
         newRotation.y = 0f;
         player.transform.rotation = Quaternion.Slerp (player.transform.rotation, newRotation, Time.deltaTime * 300);

Thanks for the help!

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 WorpeX · Jun 08, 2015 at 11:09 AM 0
Share

Is there anyone who might be able to help me with my question?

avatar image crohr · Jun 08, 2015 at 08:28 PM 0
Share

Are you trying to do this over time or instantaneously? Also, make sure that the player.forward is actually point in the correct direction.

avatar image maccabbe · Jun 08, 2015 at 09:19 PM 0
Share

You are setting newRotation.x and newRotation.y as though newRotation was a Vector3/eulerangle (x, y, z) ins$$anonymous$$d of a Quaternion (w, x, y, z). To zero out the x and y parts you would have to do something like.

 Quaternion newRotation = Quaternion.LookRotation(InputTouch.hitpoint - player.transform.position);
 Vector3 newRotEul=newRotation.eulerAngles;
 newRotEul.x = 0f;
 newRotEul.y = 0f;
 player.transform.rotation = Quaternion.Slerp (player.transform.rotation, Quaternion.Euler(newRotEul), Time.deltaTime * 300);

However, this will probably not work as eulerangles are not unique and using Quaternion.eulerangles does not always give the expected rotation. I'm not sure how you are getting InputTouch.hitpoint but if done right you shouldn't even have to zero out the rotation on the x and y axis so I would focus on getting the right rotation ins$$anonymous$$d of trying to fix an incorrect one.

avatar image WorpeX · Jun 08, 2015 at 11:23 PM 0
Share

Hmm, I wasn't able to get that rotation script to work at all maccabbe. I don't think I explained the issue properly and i'm not entirely sure how to describe it, so i'll show it ins$$anonymous$$d.

alt text

Sorry about the low quality. But as you can see in that, the rotation script is working properly except the character itself doesn't know the difference between his face and his butt.

2 Replies

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

Answer by fzawd · Jun 09, 2015 at 06:26 AM

Firstly, never manipulate a Quaternion's parameters directly. Quaternions use advanced math to work and unless you know what you're doing, manipulating their parameters will usually break them. See the `Quaternion` manual page for more info.

For the problem at hand, I would suggest calculating a 'look-at' vector for your gameObject.transform; and then use simple math to calculate a rotation. Assuming your calculated TouchInput.hitpoint is in world-space coordinates, this code should work:

 Vector3 lookAtVector = InputTouch.hitpoint - player.transform.position;
 float angle = Mathf.Rad2Deg * Mathf.Atan2(lookAtVector.y, lookAtVector.x);
 Quaternion newRotation = Quaternion.AngleAxis(angle, Vector3.forward);
 transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 300);

Note that the z parameter of the look-at vector is not used, so we won't get strange behavior for the cases when player.transform.position.z differs from TouchInput.hitpoint.


How it Works:

First line calculates a look-at vector that (as the name suggests) gives us the direction our character needs to look-at.

Mathf.Atan2 will do the magic for us: It takes distances along y and x axis (in that order) and calculates a single float value: The desired angle of our character, relative to the world-space forward direction. We need to convert this number to degrees before it's usable, hence Mathf.Rad2Deg.

Finally, we pass this info to Quaternion.AngleAxis(). Based on our float angle, we will have a Quaternion facing angle degrees from Vector3.forward. This Quaternion can be used in any future operations.

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 WorpeX · Jun 09, 2015 at 08:08 PM 1
Share

I'll be honest, I'm not entirely sure how that code works. But, it does! Thank you very much for your help!

avatar image fzawd · Jun 10, 2015 at 10:59 AM 1
Share

I edited my answer and added a little bit of explanation about the logic behind the code. As always, hope this helps :)

avatar image WorpeX · Jun 10, 2015 at 11:24 AM 0
Share

Wow, awesome! That helps a lot. Thanks for going above and beyond for me!

avatar image fzawd · Jun 10, 2015 at 11:36 AM 0
Share

You're welcome. Thanks for the awesome feedback :)

avatar image
1

Answer by Mark Gossage · Jun 09, 2015 at 06:30 AM

Your question is very similar to http://answers.unity3d.com/questions/630670/rotate-2d-sprite-towards-moving-direction.html

Taking their answer & adapting for your code:

 Vector3 moveDirection=InputTouch.hitpoint - player.transform.position;
 float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
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 WorpeX · Jun 09, 2015 at 08:11 PM 0
Share

This works too! Thank you very much 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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Recoloring animated sprites [Solved] 2 Answers

changing sprites depending on int value 1 Answer

Rotate the weapon sprite with a joystick 0 Answers

2 problems when workin with sprites on Unity3D 1 Answer

How to break a sprite into shapes with script 1 Answer


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