Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
2
Question by KrisIsHidden · Dec 16, 2014 at 07:48 PM · rotationmouseplayer movementmousepositionxaxis

Make a Player Model Rotate towards Mouse Location

Hello i have a player in my game and Im trying to make it rotate towards where the mouse is facing, but the model itself doesnt rotate and I'm getting no errors what so ever how would i make the model rotate towards the cursor location(This is a Diablo style game with a 3D yet 2D view). This is an overhead style game so it has to rotate on a horizontal plane aka the x axis. I need it so that when the mouse is to the left of the player it sends the Vector3 up, and when the mouse is to the right of the player it sends it down. How would i do this?

     using UnityEngine;
     using System.Collections;
     
     public class SCR_mouseRotation : MonoBehaviour {
         
         void Update () {
             Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.z, 10);
             Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
             lookPos = lookPos - transform.position;
             float angle = Mathf.Atan2(lookPos.z, lookPos.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.AngleAxis(angle, Vector3.down); // Turns Right
                 transform.rotation = Quaternion.AngleAxis(angle, Vector3.up); //Turns Left
             
         }
     }
 
 

This script is really buggy does anyone else have any better suggestions to make the player rotate towards the mouse location?

Comment
Add comment · Show 4
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 MajdHamada · Dec 16, 2014 at 10:29 PM 0
Share

Use transform.LookAt ....

avatar image Ericool · Dec 17, 2014 at 12:44 PM 0
Share

I think what you are trying to do is you want the player to look at a GUI so that's why you are converting the 3d point to 2d . But it is not relevant , I explain : Camera.main.ScreenToWorldPoint(mousePos);

Is when you want the GUI to be at a specific position on screen when an object is on the ground. Like for a $$anonymous$$i map

avatar image brashadam85 · Feb 19, 2021 at 04:56 PM 0
Share

Is there anyway to make this turn the player the full 360 degrees? Im really new to unity and game dev in general so the answer is probably really obvious, im just to much of a noob to spot it, any help would be greatly appreciated.

avatar image brashadam85 brashadam85 · Feb 19, 2021 at 06:08 PM 0
Share

Figured it out, now just to make the player shoot towards the cursor.

4 Replies

· Add your reply
  • Sort: 
avatar image
16

Answer by BenZed · Dec 17, 2014 at 11:29 AM

link textThere's lots of ways, but here's an easy one:

 public class LookTowardMouse : MonoBehaviour {
 
     // Update is called once per frame
     void Update () 
     {
         
         //Get the Screen positions of the object
         Vector2 positionOnScreen = Camera.main.WorldToViewportPoint (transform.position);
         
         //Get the Screen position of the mouse
         Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
         
         //Get the angle between the points
         float angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
 
         //Ta Daaa
         transform.rotation =  Quaternion.Euler (new Vector3(0f,0f,angle));
     }
 
     float AngleBetweenTwoPoints(Vector3 a, Vector3 b) {
         return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
     }
 
 }
 

Enclosed is a unityPackage with working example.


looktowardmouse.unitypackage.zip (17.8 kB)
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 endermanjd · Oct 04, 2015 at 04:21 PM 0
Share

@BenZed This doesn't work.

avatar image BOB_KSE · Apr 20, 2018 at 08:27 PM 0
Share

Thanks, this works perfectly. guys just add the script to the game object & it should work perfectly.

avatar image Menzle · May 01, 2020 at 05:20 PM 0
Share

thank you sooo much :D

avatar image beyhdgg · Dec 28, 2020 at 11:13 PM 1
Share

It works in a nasty way because as soon as you get an Angle of 45° it is problematic. It uses your Aspect Ratio of the Viewport and that is a distortion of the real Aspect Ratio 1:1. Better use the world positions (That means the first Vector might be like:

 Vector2 position1 = transform.position;

and the second one:

  Vector2 position2 = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);



avatar image Single_Projects · Apr 30 at 09:05 PM 0
Share

is it possible to change the rotation position thing (it's on z but i want it on y)

avatar image
1

Answer by ashleyjlive · Dec 17, 2014 at 01:56 AM

As @mh92 stated. For this case transform.LookAt seems like a logical way to do it.

Link - http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

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 starrtennis · Nov 01, 2019 at 03:35 AM 0
Share

It DOES sound like a logical way to do it! Only, when I have my up-axis argument as (0,0,-1) (it's a top-down 2D game)), the "character" seems to be in the wrong plane (stuck in the x-z plane for some reason).

avatar image
1

Answer by MILLIMEDIA · Nov 01, 2019 at 06:41 AM

Possibly use this?

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 BenZed · Dec 17, 2014 at 09:51 AM

 using UnityEngine;
 using System.Collections;
 
 public class LookTowardMouse : MonoBehaviour {
     
     void Update () 
     {
         //Mouse Position in the world. It's important to give it some distance from the camera. 
         //If the screen point is calculated right from the exact position of the camera, then it will
         //just return the exact same position as the camera, which is no good.
         Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);
         
         //Angle between mouse and this object
         float angle = AngleBetweenPoints(transform.position, mouseWorldPosition);
         
         //Ta daa
         transform.rotation =  Quaternion.Euler (new Vector3(0f,0f,angle));
     }
     
     float AngleBetweenPoints(Vector2 a, Vector2 b) {
         return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
     }
     
 }

My answers keep disappearing. Anyway, this is a correction from the last one.

Comment
Add comment · Show 6 · 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 KrisIsHidden · Dec 17, 2014 at 11:23 AM 0
Share

I just attempted what you had said BenZed but my player model spins around vertically not on a horizontal plane, How would i switch it from a vertical rotation to horizontal?

avatar image starrtennis · Nov 01, 2019 at 03:36 AM 0
Share

It doesn't seem that AngleBetweenPoints is a unity API function. Where is it defined?

avatar image sebastialonso starrtennis · Dec 23, 2019 at 11:46 PM 0
Share

Line 20. He defined it himself.

avatar image sebastialonso · Dec 23, 2019 at 11:47 PM 0
Share

I just used this script and it works...backwards. Does precisely the opposite of what OP asked for. $$anonymous$$oving the mouse to the left, rotates the player to the right.

avatar image Abz101 sebastialonso · Jun 02, 2020 at 02:10 PM 0
Share

To move horizontally, on line 17 change the y to 'angle' and the z to '0f'.

(0f, angle, 0f)

To have the player actually rotate towards the mouse then change the code within the brackets in line 21 to (b.x - a.x, b.y - a.y) #when you said the code did exactly the opposite of what was required I flipped the values and thankfully it works :)

avatar image raider13209 · Jan 13, 2021 at 12:21 PM 0
Share

this is broken but it can be fixed by changing the transform.rotate to (0f, -angle, 0f) the mius is because it rotates the wrong way this means it rotates opposite

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

20 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

Related Questions

Rotation of Object on single axis in direction of the mouse position 0 Answers

rotation based on mouse position 2 Answers

Why my bullet does not rotate via Z axis? 1 Answer

How to make the player rotate to where an object is being thrown? 1 Answer

Slerp/Rotational Problem Query 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