Cant get Player to rotate towards mouse position on specific axis
For the past week I have been trying to get my player to look in the direction of the mouse on the screen and have gone to a million different questions and answers and can't figure out what to do to make this work, I want my player to look at the mouse position on only the x-axis and my current issue is that the actual rotation is extremely slight and only rotates the player by like 0.2 when the mouse is on the other side of the screen.
This is my player code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player : MonoBehaviour {
public float speed;
private Vector3 mousePos;
private void Update()
{
// Movement
transform.position += new Vector3(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0f, 0f);
// Mouse look
Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
mousePos = transform.position - pos;
mousePos.x = -mousePos.x;
mousePos.y = -mousePos.y;
transform.LookAt(new Vector3(mousePos.x, 0, 0));
transform.rotation = Quaternion.Euler(transform.rotation.x, 0, 0);
Debug.DrawLine(Camera.main.transform.position, mousePos);
Debug.Log(mousePos);
// TODO: Clamp position to island model scale
}
}
From experimenting, it seems like the Camera.main.ScreenToWorldPoint() is whats giving me the extremely small numbers as when I Debug.Log them the numbers do not watch the coordinates of where it actually is on screen.
This is what I am trying to accomplish for the player rotation, does anyone know how to fix this?
Your answer
Follow this Question
Related Questions
why is my mouse posision allways 0? 1 Answer
3rd Person Shooter Crosshair system 1 Answer
Rotation starts freaking out when I put my cursor on it. 1 Answer
Screen to World Point on 2.5D game 0 Answers