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 Reikyrr · Nov 24, 2010 at 07:57 PM · rotationmathscreen-coordinates

Y Rotation mousecoords and object rotation

Hello,

Im kinda new to unity, but I was getting around fine untill I tried to rotate my character to the mouse. The code I made doesn't seem to do anything :/ The idea is that I take the center of my character and translate it to screen coords. Then I will do some math with the mouse coordinates and the character screen coordinates to get my rotation.

Though.. Nothing happens

Code: (target is the object that needs to be rotated accordingly)

var target : Transform;

function Update () { var target_onscreen : Vector3 = camera.WorldToScreenPoint (target.position); // viewport coordinates range from zero to one var x=Screen.width*target_onscreen.x; var y=Screen.height*target_onscreen.y;

target.transform.rotation.y=Mathf.Atan2 ( Input.mousePosition.y-y, Input.mousePosition.x-x ) * Mathf.Rad2Deg; print (target.transform.rotation); }

Solved! Working code here:

var target : Transform;

function Update () { var target_onscreen : Vector3 = camera.WorldToScreenPoint (target.position); var x; x = target_onscreen.x; var y; y = target_onscreen.y;

target.transform.eulerAngles=Vector3(0,Mathf.Atan2 ( Input.mousePosition.y-y, Input.mousePosition.x-x ) * Mathf.Rad2Deg,0);

}

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 Jesse Anders · Nov 24, 2010 at 10:12 PM

Why are you multiplying the screen coordinates by the screen dimensions? Your comment indicates that maybe you're expecting target_onscreen to be in normalized viewport coordinates, but WorldToScreenPoint() returns coordinates in screen space.

Also, Transform.rotation is a quaternion, not a set of Euler angles, so this line of code:

target.transform.rotation.y=Mathf.Atan2 (...) * Mathf.Rad2Deg;

Doesn't do anything meaningful. If you want to set the y rotation angle directly, assign to transform.eulerAngles.y instead.

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 Reikyrr · Nov 25, 2010 at 08:55 AM 0
Share

Wow, I forgot I changed the command, thank you for pointing that out. I would like to thank you for the part about euler as well. I got it to work now. Ill post the script in case someone wants to use it.

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

No one has followed this question yet.

Related Questions

Changing the forward rotation for LookAt 2 Answers

Rotation problem: Check is failing/ endlessly spins 1 Answer

Make 3D Player look exactly at mouse with ScreenToWorldPoint? (maths Question) 2 Answers

How to draw a circle around a moving game object 1 Answer

Rotate object based on hit normal + matching camera direction 0 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