- Home /
How can i make character head to rotate according to the mouse cursor position ?
I want when i move the mouse around the game view when the game is running then the character head will rotate according to the mouse cursor position.
First i tried LookAt but it's not working the character head is all the time fall down. Second not sure if it should be just Input.Mousepositon or somehow to make it in world space position ?
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour
{
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = 3.0f;//-20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public Transform boneToRotate;
private Rigidbody rigidbody;
float x = 0.0f;
float y = 0.0f;
public float rotationSpeed = 5f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate()
{
//boneToRotate.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Input.mousePosition), Time.deltaTime);
boneToRotate.LookAt(Input.mousePosition);
Debug.Log("Mouse Freedom");
//}
if (target && Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed);
if (target != null)
{
target.rotation = Quaternion.RotateTowards(transform.rotation, rotation, rotationSpeed);
}
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
In the Update function this is the two lines i tried:
//boneToRotate.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Input.mousePosition), Time.deltaTime); boneToRotate.LookAt(Input.mousePosition);
In the variable boneToRotate i dragged in the Inspector the EthanHead (Transform)
And the script is attached to the Main Camera.
The goal is to make the head look at where the mouse cursor position is at. If the mouse cursor is now on a mountain look to that far mountain if it's no something closer so look at it. But the idea is to rotate the head according to the mouse cursor movement.