- Home /
Character always facing mouse cursor position...
Is it possible to always have the character rotate to face the mouse cursor position? As in similar to the AngryBots demo, except rotating the whole player? I think it is fairly simple, however an hour of research proved it not so. Can anyone help me with this?
Thanks in advance!
Not sure if this is what you needed, but this has worked for me. If you create an empty Gameobject then place that Gameobject in the center of your character(or whatever point you want your character to rotate around) then make your player a child of the Gameobject. And then attach the following script to your Gameobject:
using UnityEngine;
using System.Collections;
public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = $$anonymous$$athf.Atan2(lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
It should rotate your character to always face your cursor.
P.S: sorry for the terrible formatting on the code, I don't know what happened.
Well, it's kinda what I wanted, but...
The character is only rotating around the X and Y axis, which is not what I wanted. I wanted it to rotate around the X and Z. Can you do that please? Thanks so much! :D
Destran's answer helped me with my problem but, making an empty Gameobject and taking your character inside that Gameobject isn't the best solution. What I did was, to put my character inside the script by adding the public Gameobject; And also changing the y in Atan to z, and chaning Vector3.forward to Vector3.down:
using UnityEngine;
using System.Collections;
public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
public Gameobject human;
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);<br>
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - human.transform.position;
float angle = $$anonymous$$athf.Atan2(lookPos.z, lookPos.x) * $$anonymous$$athf.Rad2Deg;
human.transform.rotation = Quaternion.AngleAxis(angle, Vector3.down);
}
}
Answer by TimCoster · May 29, 2016 at 07:28 PM
This works for me:
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray,out hit,100))
{
transform.LookAt(hit.point);
}
}
Or:
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray,out hit,100))
{
transform.LookAt(new Vector3(hit.point.x,transform.position.y,hit.point.z));
}
}
Hey buddy, my character he follows the command correctly. but he has a problem because when he goes to walk he keeps looking at the Y axis, and I wanted him to look only at the X and Z. Your script was as close as possible to making an isometric game. Please help me. void Update() {
RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast(ray,out hit,100))
{
transform.LookAt(new Vector3(hit.point.x,transform.position.y,hit.point.z));
}
}
however this does use alot more resources, and it requires to have something in the background to hit.
Answer by nasoukikos · Mar 02, 2014 at 04:44 PM
try this and call the function when you want it
private Vector3 worldpos;
private float mouseX;
private float mouseY;
private float cameraDif;
public GameObject fpc;
void Start()
{
cameraDif = camera.transform.position.y - fpc.transform.position.y;
}
void LookAtMouse()
{
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
worldpos = camera.ScreenToWorldPoint(new Vector3(mouseX, mouseY, cameraDif));
Vector3 turretLookDirection = new Vector3 (worldpos.x,fpc.transform.position.y, worldpos.z);
fpc.transform.LookAt(turretLookDirection);
}
This script is already functional:
using UnityEngine;
using System.Collections;
public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
float angle = $$anonymous$$athf.Atan2(lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
However it just rotates me the wrong way... I want the rotation to be around the X and Z, not X and Y.
where it says input.mouseposition.y just replace the y with the z or whatever you want
Can you please change it for me? I am kinda new to mouse position and Vector3 positions. Thanks! :D
try this
using UnityEngine; using System.Collections;
public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour {
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 = $$anonymous$$athf.Atan2(lookPos.z, lookPos.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
I was gonna say the same thing... Yeah pretty easy.
@DubstepDragon Just so you know, when it comes to languages like JS and C#, remember that if you see ANY method that returns a value or object followed by a period and the characters X, Y, or Z, then feel free to change it. Here's why:
Usually in those situations, when there's a dot right after "Input.mousePosition" (or anything returning data), then whatever goes after that dot is either another method that does something with that object, or a certain property. In this case, it's a property because it's the X, Y and Z axis, all of which are for measuring position rotation and size.
Sorry for the long explanation, just thought I might explain and try to help you understand in the future, since you said you were new.
@nasoukikos I have some questions about this code:
private Vector3 worldpos;
private float mouseX;
private float mouseY;
private float cameraDif;
public GameObject fpc;
void Start()
{
cameraDif = camera.transform.position.y - fpc.transform.position.y;
}
void LookAt$$anonymous$$ouse()
{
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
worldpos = camera.ScreenToWorldPoint(new Vector3(mouseX, mouseY, cameraDif));
Vector3 turretLookDirection = new Vector3 (worldpos.x,fpc.transform.position.y, worldpos.z);
fpc.transform.LookAt(turretLookDirection);
}
I haven't tested your code in my game yet, but just to clarify: this does work for 2D top-down shooters and platformers, etc. if I understand correctly? Or do I have change it up a little bit to (litterally) get it heading the right direction?
Your answer
Follow this Question
Related Questions
unity2D: make object face mouse 2 Answers
Character - Move In Mouse Direction 1 Answer
Rotate the player to face where the mouse is pointing 3 Answers
Movement along X and Z axis... 2 Answers
Shooting in direction of mouse cursor 2d 5 Answers