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 Lethael · Jan 25, 2018 at 04:38 AM · 2dmouselook

Weird 2D Mouse Look At Problem

Okay, so yesterday I decided to start making a 2D top-down shooter. So I started with the main mechanic; getting the player to aim at the mouse, but came across this really strange problem where the player would not fully rotate. The player has a Recttransform component, and is a child of a 'Screen space - overlay' canvas. I have tried various options for rotating the player, but opted for the following:

  Vector3 dir = Input.mousePosition - cam.WorldToScreenPoint(transform.position).normalized;
  float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);


I recorded a video showing the problem: https://www.youtube.com/watch?v=1jmFl9gpEbo


Any help would be appreciated!

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 Lethael · Jan 25, 2018 at 05:37 AM

Yay! I finally got it working. If I set the canvas's render mode to 'Screen space - Camera', it works perfectly, well nearly. (It rotates it by 90 degrees, but I was able to easily fix that.)

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 Zwithak · Jan 25, 2018 at 04:55 AM

The main problem is that you are normalizing the screen-point vector. Though, I'd recommend working in game-coordinates (as opposed to screen-coordinates) as you should be able to make a point you can simply rotate to (or set as your forward, in this case).

But, using screen coordinates, something like this, perhaps:

     public static Vector2 FaceMouse(Camera cam, Transform player)
     {
         player.forward = Input.mousePosition - cam.WorldToScreenPoint(player.position);
     }

Or just use that as a base reference and go from there.

Comment
Add comment · Show 3 · 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 Lethael · Jan 25, 2018 at 05:14 AM 0
Share

@Zwithak Thanks for the fast reply ;)
I tried what you suggested, but unfortunately did not make any positive improvements. I have tried heaps of different ways to do this, (including without normalising the screen-space vector) but haven't got it to work. I think there may be something wrong with my setup, as I have used mulitple scripts other's have made, who claim that their script's work.
Also, if i do this:

  Vector3 dir = cam.ScreenToWorldPoint(Input.mousePosition) - transform.position;


The exact same problem occurs.

avatar image Zwithak Lethael · Jan 25, 2018 at 11:30 PM 0
Share

I know you already found a solution, but I fixed the code above so that should also work now. Just keep in $$anonymous$$d that in 3d environments, you need to correct for the camera height.

avatar image Lethael Zwithak · Jan 26, 2018 at 02:49 AM 0
Share

Oh yeah, I should have mentioned, the code I posted in the question works fine, except I had to change this:

 transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);


to this:

 transform.rotation = Quaternion.AngleAxis (angle - 90, Vector3.forward);


I can't test your code right now, but I think it would also work.
So anyway, thanks for the help ;)
BTW: Did you subscribe to me on Youtube? xD

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

145 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

Related Questions

2D mouselook 4 Answers

2D Mouse Aiming on Unity 4.3 1 Answer

2D Cursor Aiming 2 Answers

Lookat mouse in 4 directions only. 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 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