Player facing mouse with orthographic isometric camera
I am current working on a project with a 3D isometric camera. the player typically moves on both the x and z axis with y being the height for jumping, I say typically because if your moving left, right, up, and down its on both axis simultaneously where if the player is moving diagonally there actually only moving along one axis. This is not standard obviously but is done this way because of the camera.
Now my issue comes in with making the player face the mouse at all times. im currently using the transform.lookat feature with a raycast from the camera to the mouse. the issue I have is the the player looks all over and the model leans on every axis. Ive tried to constrain it only to the y axis but no matter what i do i always get funky results.
Im wondering if the movement is affecting the axis or if im using the wrong command (lookat) and should be using something else. Ive taken out what im fairly certain to be unnecessary code for this post like jumping but if its all needed feel free to request it.
void Awake()
{
playerBody = GetComponent<Rigidbody>();
}
void Update()
{
Movecheck();
}
void FixedUpdate()
{
Facing();
if (moveRequest)
{
Move();
moveRequest = false;
}
}
void Movecheck()
{
if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
{
Findforces();
moveRequest = true;
} else {moveRequest = false;}
}
void Findforces()
{
verticalForce = Input.GetAxis("Vertical") * (Vector3.forward + Vector3.left);
horizontalForce = Input.GetAxis("Horizontal") * (Vector3.forward + Vector3.right);
moveForce = (horizontalForce + verticalForce);
}
void Move()
{
playerBody.velocity = new Vector3(moveForce.x * moveSpeed, playerBody.velocity.y, moveForce.z * moveSpeed);
}
void Facing()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength = 100f; /////////////////////////////// prob to big but leaving for now
if (groundPlane.Raycast(ray, out rayLength))
{
pointToLook = ray.GetPoint(rayLength);
}
Vector3 lookTowards = new Vector3(pointToLook.x, 180, pointToLook.z); //culprit line, y is at 180 or model is upside down.
transform.LookAt(lookTowards, Vector3.forward);
}