- Home /
Movement along X and Z axis...
I want to rotate my character to face the mouse cursor. The script here does not do that, it rotates across X and Y. I want it to rotate across X and Z only. Can anyone help me?
The script is here:
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
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 = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
Thanks in advance! :D
I'm slightly confused, do you want your character to spin as if it were turning to face your cursor? If so, that means that you want it to rotate only on the "Y" axis. I might be able to help, but I need more information.
Wow I've not explained it properly :P
Yeah is pretty much what you've said, I want one of its sides to always face the cursor, hence using only the Y axis. If you can help me with that, it'll be great :D
If you just replace the (lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg;
With (lookPos.z, lookPos.x) * $$anonymous$$athf.Rad2Deg;
Then it should rotate along the x and z and not the y.
So something like 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);
}
}
Let me know if that works:D
@Destran's answer is close, you also have to replace the Vector3.forward with 'Vector3.up' in the last line.
transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
It's closer than the others so far... However I still need it to face the cursor whilst it moves in 360 degrees. With this script + robertbu's contribution, it only rotates half-way, and mirrors that for the other half.
Answer by CodeElemental · Mar 03, 2014 at 08:26 AM
void Update () {
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
transform.LookAt(lookPos);
}
This should work.
I was actually about to post (almost) exactly that! Well hope it works, Dubstep.
It works... kinda.
It ins$$anonymous$$d keeps one face looking at the cursor... However, I want it just to rotate around the Y-axis, similar to the AngryBots demo, but only in rotation. I think your script works the right way, however I need it to only rotate around the Y. I tried freezing the rotation with a RigidBody, however it does not work :'(
Can you help me with this please? :D
If you're using some form of isometric perspective, it's possible that you need to swap the Y and Z coordinates.
Can you elaborate a bit more on what behaviur are you getting , i can't understand the one-face looking part?
I want the character to always be facing the cursor, via rotation. This is as in the character is standing. The script you gave me makes him face the air when the cursor is nearby. As I already suggested about twice, I want it similar to the AngryBots demo.
Thanks in advance! :D
Answer by wibble82 · Mar 03, 2014 at 09:06 AM
The simplest solution to your problem has already been posted above - you take the mousePos, ask the camera for the position that corresponds to world space, then set the transform to look at that point. This however will cause your character to look directly at the mouse, meaning they may end up at an odd angle (for example, if the new position is high above the charachter, it'll end up 'lieing down'. A small modification to the earlier code might be:
void Update ()
{
//get the mouse position and convert it to world space
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
Vector3 lookPos = Camera.main.ScreenToWorldPoint(mousePos);
// clamp the y coordinate to the same height as the player so the character doesn't face upwards
lookPos.y = transform.position.y;
//force the character to look at the player (specify the up vector as well to get best results!)
transform.LookAt(lookPos,Vector3.up);
}
Hope that helps!
Your answer
Follow this Question
Related Questions
unity2D: make object face mouse 2 Answers
Look at mouse 1 Answer
Locking movement to one axis only (3D game) 1 Answer
Look around when holding down mousebutton 1 Answer
Look at the mouse? 2 Answers