Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 dark-storm · Jan 19, 2019 at 02:03 PM · rotation3dcursortopdownorientation

Issue pointing towards mouse

Hi, I searched around for a while but I couldn't figure out how to solve this nasty bug. I am using a top down view (pointing towards -z), basically 2d with 3d objects and camera in perspective mode.

I need to orient an object towards the mouse , ignoring the z aspect, as everything moves on the same plane.

I am using the following code within FixedUpdate:

     Vector3 mouseToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 1f));
     mouseToWorld.z = 0f;
     Vector3 difference = mouseToWorld - transform.position;
     difference.Normalize();

     float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

     transform.rotation = Quaternion.Euler(0f, 0f, angle - 90);


Unfortunately it only works when the object is at 0.0,0.0. As soon as it moves, the rotation goes off and is not aligned anymore (it rotates less, as it calculates the wrong angle between the two vector positions). I am not sure what's wrong as all of the answers I found used this method. It's off if I assign the object to a parent and shift its position, too.

As a side note, it was working perfectly in 2d, that is, with the camera set to orthographic.

After spending 2 hours on it I thought someone smarter than me could lend a hand :D ,Hi, I searched around for a while but I couldn't figure out how to solve this nasty bug. I am using a top down view (pointing towards -z), basically 2d with 3d objects and camera in perspective mode.

I need to orient an object towards the mouse , ignoring the z aspect, as everything moves on the same plane.

I am using the following code:

     Vector3 mouseToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 1f));
     mouseToWorld.z = 0f;
     Vector3 difference = mouseToWorld - transform.position;
     difference.Normalize();

     float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

     transform.rotation = Quaternion.Euler(0f, 0f, angle - 90);


Unfortunately it only works when the object is at 0.0,0.0. As soon as it moves, the rotation goes off and is not aligned anymore (it rotates less, as it calculates the wrong angle between the two vector positions). I am not sure what's wrong as all of the answers I found used this method. It's off if I assign the object to a parent and shift its position, too.

After spending 2 hours on it I thought someone smarter than me could lend a hand :D

Comment
Add comment · Show 1
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 dark-storm · Jan 19, 2019 at 02:07 PM 0
Share

EDIT: I noticed the issue is only when the object moves. Could it be because the camera is following the object with a little drag (cinemachine)?

1 Reply

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

Answer by dark-storm · Jan 20, 2019 at 02:02 AM

I figured out you need to subtract the distance between the player and the camera to the initial mouse position, as the camera had a different Z:

Vector3 mouseToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition - new Vector3(0, 0, Camera.main.transform.position.z));

Here the working script:

     Vector3 mouseToWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition - new Vector3(0, 0, Camera.main.transform.position.z));
     //Debug.DrawLine(transform.position, mouseToWorld);
     mouseToWorld.z = 0f;
     Vector3 difference = mouseToWorld - transform.position;
     difference.Normalize();

     float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
     transform.rotation = Quaternion.Euler(0f, 0f, angle - 90);
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

149 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotation Question in Top-down 3D Game 0 Answers

change direction by rotating object around y axis and adding relative torque 1 Answer

iOS - Smooth auto-rotation of orientation 1 Answer

Manipulating cursor direction in 3D space 0 Answers

How to use mathf.clamp? 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