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);
}
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
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);
}