- Home /
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?
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
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.
Figured it out, now just to make the player shoot towards the cursor.
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.
Thanks, this works perfectly. guys just add the script to the game object & it should work perfectly.
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);
is it possible to change the rotation position thing (it's on z but i want it on y)
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
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).
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.
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?
It doesn't seem that AngleBetweenPoints is a unity API function. Where is it defined?
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.
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 :)
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