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 CheeseGoat · Jan 22, 2013 at 09:42 PM · rotationraycastangles

Pointing object at mouse using raycast only works when object is center screen.

Edit 2: Solved this by going a different route, see below.

Edit: Just wanted to note that this is for a 2D rotation

I have an object center screen and a plane to raycast to in order to get the mouse position in world space so that I can point the object towards the mouse. This works fine unless the object is not at the center of the screen.

The is object acting as if it's aiming at the mouse from the center of the screen as opposed to where it actually is and I have no idea why unless it has something to do with ScreenPointToRay.

I have a video of this happening here (youtube) in case I'm not explaining it very well

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         Vector3 CursorPos = Vector3.zero;
 
         GameObject plane = GameObject.FindGameObjectWithTag("RaycastPlane");
 
         if (plane.collider.Raycast(ray,out hit, 50f))
         {
             CursorPos = ray.GetPoint(50f);
         }
 
 
         float x = CursorPos.y - transform.position.x;
         float y = CursorPos.x - transform.position.y;
 
         float angle = Mathf.Atan2(x, y);
         angle *= Mathf.Rad2Deg;
         transform.localEulerAngles = new Vector3(transform.rotation.x, transform.rotation.y, angle + 90);

Anyone have an idea of what is causing this?

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
Best Answer

Answer by CheeseGoat · Jan 23, 2013 at 06:56 AM

I was reading about another user doing the same kind of rotation I was wanting and they did it a bit different. Instead of using a ray they went used WorldToScreenPoint and worked from that. This seems to be working for me so if anyone else has an issue like I had you can do use this also:

         Vector3 mouse_pos = Input.mousePosition;
         mouse_pos.z = 10f;
         Vector3 object_pos = Camera.main.WorldToScreenPoint(transform.position);
         mouse_pos.x = mouse_pos.x - object_pos.x;
         mouse_pos.y = mouse_pos.y - object_pos.y;
 
         float angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
 
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, 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
avatar image
0

Answer by vbbartlett · Jan 22, 2013 at 10:29 PM

It looks like the object is rotating in local space instead of rotating to face the position of the mouse on a plane in world space.

To fix this I would create a vector that points at the plane location. Then simply set the forward vector of the transform.

  Vector3 f = planePos - transform.position;
  f.Normalize();
  transform.forward = f;
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 CheeseGoat · Jan 22, 2013 at 11:20 PM 0
Share

Are you saying to use the method above ins$$anonymous$$d of transform.localEulerAngles? (Sorry if I'm misunderstanding) I should have mentioned above that I'm looking for a 2D rotation, I'll edit that in.

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

10 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

Related Questions

Rotating to Slope 1 Answer

Issue finding an angle with trigonometry 1 Answer

transform.right not working correctly? 0 Answers

SphereCast help 1 Answer

Offset raycast by specific amount 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