- Home /
Player mouse rotation
I'm new to programming with C# and I need help with rotating the 3D character to look at the mouse. Is there a way of modifying this code so it only rotates the character of the y axis, as at the moment it is rotating on the z axis?
void playerRotate()
{
transform.rotation = Quaternion.LookRotation(Input.mousePosition);
}
Answer by b1gry4n · Apr 12, 2016 at 05:03 PM
You will need to cast a ray from the mouse position converted to world space. The end of your ray (if you provide a length) whether it hit or didnt, needs to be the "target" your player looks at. A good example can be found in the Angry bots demo
Answer by M-Hanssen · Apr 12, 2016 at 07:42 PM
If you want to let your 3D character look at the mouse (flat on the screen). Then this is what you want to do:
protected void Update()
{
Vector3 targetPostition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPostition = new Vector3(targetPostition.x, transform.position.y, targetPostition.z);
transform.LookAt(targetPostition);
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Keep objects current rotation, with Input Mouse X 0 Answers
3D Turret Rotation 0 Answers
Using quaternion for mouse movement? 3 Answers
How can I add a rotation to my bullets when they are shot? 1 Answer