- Home /
How do i make that the character head to rotate to where the mouse cursor is ?
What i want to do is to rotate the character head only according to where the mouse cursor is at now. So if when the game is running i move the mouse around the game window it will rotate the player head so it will looks like he looking to where i point him to look with the mouse.
And only when moving the mouse cursor around if i don't move the mouse don't rotate the head around.
And i would like first making it for the head only and then also for the whole body to turn to where the mouse cursor is positioning/pointing. But first only for the head part.
I have a ThirdPersonController with MainCamera under it in the Hierarchy. And also attached this script to the ThirdPersonControlle:
using UnityEngine;
using System.Collections;
public class CharacterMoveMouse : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
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.forward);
}
}
But this script is not moving the head and when running the game the i see sharp moves and i can't move the character to control it as before nothing is working. Big mess.
Your answer
![](https://koobas.hobune.stream/wayback/20220612095025im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I animate linerenderer lines over time ? 1 Answer
How do i use a public sealed class to create objects and destroy them ? 0 Answers
How can I call the Load method and/or the ShootingSettings method also only once in the Update ? 1 Answer
How can i check and fire an event when the user look at specific object ? 0 Answers