Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by Khizbu · Sep 27, 2014 at 04:41 PM · 2dmouse

2D look at mouse position (z rotation) [c#]

Hello!

I have this zombie, and I want him to look at mouse position http://i.imgur.com/BNAv5NU.png I know that this question is very trivial, but I tried many ways and I still don't know how to solve this problem. Lookat()+Input.mousePosition does not work (it rotates all axis, but not z)

Could somebody help me?

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

7 Replies

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

Answer by $$anonymous$$ · Sep 27, 2014 at 06:10 PM

Try using something like this: SOLUTION

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 Khizbu · Sep 27, 2014 at 07:30 PM 0
Share
 float AngleRad = $$anonymous$$athf.Atan2(Input.mousePosition.y - transform.position.y, Input.mousePosition.x - transform.position.x);
         float AngleDeg = (180 / $$anonymous$$athf.PI) * AngleRad;
         this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);

It's supposed to work, but character acts strange, it rotates from time to time, sometimes wrong way, hmm I dunno, any ideas?

avatar image Khizbu · Sep 27, 2014 at 07:40 PM 0
Share

damn, totally forgot that pixel position is not world position, thanks!

avatar image clearit · Dec 14, 2018 at 03:39 AM 1
Share
    Vector3 mouseScreenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

     Vector3 lookAt = mouseScreenPosition;

     float AngleRad = $$anonymous$$athf.Atan2(lookAt.y - this.transform.position.y, lookAt.x - this.transform.position.x);

     float AngleDeg = (180 / $$anonymous$$athf.PI) * AngleRad;

     this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
avatar image angelginer27 clearit · Mar 03 at 10:47 AM 0
Share

This works perfect for me, the first one doesn't point to mouse at all. Thanks!!

avatar image
10

Answer by Octet_ · Nov 12, 2015 at 01:12 AM

You can simply set the of the basis vectors of the transform directly.

For example if you want the up (green in the scene view) vector to point to the mouse you can write the following code:

 // convert mouse position into world coordinates
 Vector2 mouseScreenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
 // get direction you want to point at
 Vector2 direction = (mouseWorldPosition - (Vector2) transform.position).normalized;
 
 // set vector of transform directly
 transform.up = direction;
Comment
Add comment · Show 5 · 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 MauroRezende · Mar 12, 2017 at 11:59 AM 0
Share

nice one, worked perfectly for me !

avatar image OuuGiii · Feb 06, 2018 at 09:24 AM 1
Share

You uses the variable mouseWorldPosition in direction but you make a Vector2 called mouseScreenPosition. Should the name of the variable you make be changed to mouseWorldPosition?

avatar image shivu_98 OuuGiii · Jul 14, 2020 at 09:08 AM 0
Share

Yes, it needs to be changed to mouseScreenPosition. Update code :

// convert mouse position into world coordinates Vector2 mouseScreenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

// get direction you want to point at Vector2 direction = (mouseScreenPosition - (Vector2) transform.position).normalized;

// set vector of transform directly transform.up = direction;

avatar image OfficeThrashing · Sep 10, 2018 at 04:32 AM 0
Share

Thanks It works!!Like Charm

avatar image Raikir-i-sh · Nov 13, 2018 at 12:43 PM 0
Share

I was in a tunnel. Your answer was the light that flashed my eyes and let me see the world space. Thnks.

avatar image
5

Answer by IsaiahKelly · Mar 17, 2015 at 05:55 PM

Here's a slightly refined and properly formatted solution for others:

 Camera cam;
 Transform my;
 Rigidbody2D body;
 
 
 void Awake ()
 {
     cam = Camera.main;
     my = GetComponent <Transform> ();
     body = GetComponent <Rigidbody2D> ();
 }
 
 
 void Update ()
 {
     // Distance from camera to object.  We need this to get the proper calculation.
     float camDis = cam.transform.position.y - my.position.y;
 
     // Get the mouse position in world space. Using camDis for the Z axis.
     Vector3 mouse = cam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, camDis));
 
     float AngleRad = Mathf.Atan2 (mouse.y - my.position.y, mouse.x - my.position.x);
     float angle = (180 / Mathf.PI) * AngleRad;
 
     body.rotation = angle;
 }

In this example I also rotated the Rigidbody2D instead of the transform. Since it's already a float. You could also do this in Fixed Update.

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
1

Answer by Khizbu · Sep 27, 2014 at 08:14 PM

There is full script Vector3 pozycja = Camera.main.ScreenToWorldPoint (Input.mousePosition); float AngleRad = Mathf.Atan2(pozycja.y - transform.position.y, pozycja.x - transform.position.x); float AngleDeg = (180 / Mathf.PI) * AngleRad; this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);

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 Bioder · Apr 24, 2015 at 03:51 AM

I found a solution like this:

 float camDis = cam.transform.position.y - my.position.y;
 Vector3 mouse = cam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, camDis));
 float AngleRad = Mathf.Atan2 (mouse.y - my.position.y, mouse.x - my.position.x);
 float angle = (180 / Mathf.PI) * AngleRad;
         
 body.rotation = angle;


But when i'm moving my character it begins to stutter... like from 60 fps to 1-2, but only the flashlight.

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
  • 1
  • 2
  • ›

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

15 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

Related Questions

Cannonball not shooting according to the rotation of the cannon. 1 Answer

2D mouse angle in 360 degrees? 1 Answer

Make animator rules apply to multiple sprites without all sprites with animator being effected by conditions. 1 Answer

Having problems getting a zoom to mouse function working well with smoothstep, need some help. 0 Answers

(C#) 2D Raycast wrong 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