- Home /
How do I make my model's head and body rotate with the mouse?
How do I make my model's head and body rotate with the mouse?
More specifically, I want the player's head to rotate accordingly to the player mouse. I want the model to looks up when the player looks up, look down when the player looks down, and look left and right when the player does the same.
When looking left or right I want the head to rotate 90 degrees both directions before turning the body and when the player starts walking/running the body with rotate accordingly. Very complex I imagine but please help. THANK YOU!
Use the HeadLookController and/or an I$$anonymous$$ solution.
Answer by TonyLi · Jul 13, 2017 at 07:18 PM
If you're using a Mecanim Humanoid character, write a script with an OnAnimatorIK method. In the method, call Animator.SetLookAtPosition() to set the position. (You can also use SetLookAtWeight to specify how much head and body rotation to use.)
To determine the position, use Camera.ScreenToWorldPoint to convert Input.mousePosition to a world space position.
This will handle looking.
Turning the character's feet to face the mouse cursor is a little more complicated. You'll want to do a little math to determine the angle between the character's forward direction and the direction to the mouse cursor using Vector3.Angle(). If it exceeds a must-turn threshold, start playing a turn-in-place animation until it's below a can-stop threshold.
Would using Input.mouseposition work with Joystick input? If not is there an alternative? Currently I have a gameobject in the scene that is set as the lookat target and I control that with mouse, only problem is I get this weird effect when I control it at the clamped angle
This is a bit late but if your mouse is always in the center of your screen then you can just use new Vector2(0.5f, 0.5f) to replace Input.mousePosition.
Answer by Artieman · Aug 25, 2020 at 12:49 AM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MouseAim : MonoBehaviour {
public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
this is the code i got from a video https://www.youtube.com/watch?v=lYIRm4QEqro to download the code https://www.youtube.com/redirect?q=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D0B__1zp7jwQOKX1Y0amo0dWIxTW8&event=video_description&redir_token=QUFFLUhqbXloUlpzR1Z0eTVXcVlsSy1y$$anonymous$$WZvcFljdS00UXxBQ3Jtc0trOEYzYjgtU1JwZWh4bGFv$$anonymous$$DVX$$anonymous$$nNj$$anonymous$$WV2dGFJUHdHRlNuczJQcWEzZkx4eHZnNFNoT0t6d1Q1TGV0ZndoZ0Fq$$anonymous$$URyV3dURVFHcmNNV3FqRnJfOEg2QWZlRmFEOV9TeDgwazRPSmU5d1hudkp0NHVRVQ%3D%3D&v=lYIRm4QEqro