- Home /
Question by
SC4V4NGER · Jan 03, 2014 at 05:19 PM ·
rotate object
Top Down Character Rotation with mouse
I want to rotate my Player with my mouse and let him look in the direction my cursor is located. Sadly I am pretty New to Unity and have no clue how to even start... I am using C# and no charaktercontroller... I Hope you can help me Thanks Sc4v4nger
Comment
Best Answer
Answer by BigRoy · Jan 04, 2014 at 11:07 AM
Have a look at Quaternion.LookRotation():
Out of the top of my head this should be pretty close to what you need:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public Transform target;
void Update() {
Vector3 mousePosition = Input.mousePosition;
Vector3 targetPosition = Camera.ScreenToWorldPoint(mousePos);
Vector3 relativePos = targetPosition - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos);
transform.rotation = rotation;
}
}
Your answer