Raycast Aiming Off To The Side Of Players Gun Position

Hey So Im trying to use to raycast for the player to follow the mouse position, but the player is facing over to the left while the mouse curser is over on the right. I can’t find a solution! How can I offset this or tell it where the front of the player is?

Here is my script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerMovement : MonoBehaviour
{
    public CharacterController controller;
    public float speed = 6f;
    Vector3 lookPos;
    public Vector3 moveDirection;


    private void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit, 100))
        {
            lookPos = hit.point;
        }
      
        Vector3 lookDir = lookPos - transform.position;
        lookDir.y = 0;

        transform.LookAt(transform.position + lookDir, Vector3.up);

      }

    private void FixedUpdate()
    {
            float horizontal = Input.GetAxisRaw("Horizontal");
            float vertical = Input.GetAxisRaw("Vertical");
            Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
            controller.Move(direction * speed * Time.deltaTime);
            moveDirection = new Vector3(horizontal, 0, vertical);
      
    }

Code looks functional, maybe your model isn’t correctly aligned?

Does the player turn at all when you move the mouse, or does it just look to the left of it?

Hey, yes player rotates with no issues, it’s just not centred in front of player

Well to me a few things are off.

You get a raycast position from the ground.
Then you have a look direction which is the raycast position - the position of the player.
Set the Y to 0 (Which your ground probably already is)

But then you do a transform.LookAt where you re-add the lookDir to the player position. Which you’ve just subtracted, so it would be the same value as lookPos again.
Vector3 lookDir = lookPos - transform.position;
transform.position + lookDir

just to virtualize values for example:
100 = 110 - 10
10 + 100 = 110

If you do lookDir.y = 0, you’ll look at the ground. But your ground point is probably already Y = 0.
Doing a LookAt at a Y axis other than its own, it’ll rotate forward or backward depending whether the point is lower or higher than the character itself.

You’re also polling user input in the FixedUpdate loop. That should be done in Update instead.
So poll the user input in Update, store the values and use them in FixedUpdate.
(though in my example below, I don’t use the mouse input.)

Instead of calculating the direction the player should move to, you could probably just use transform.forward.
Rotation is done by using the LookAt and the forward direction is where it should move to.

I changed the script up a bit as I was debugging the values.
You could try this on a capsule.

using System;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    // Other Components
    public CharacterController Controller;
    private Camera mainCamera;

    // Public Modifiers
    public float MoveSpeed = 6f;

    // Private variables
    private Vector3 lookPos;

    private void Start()
    {
        mainCamera = Camera.main;
    }

    // Update is called once per frame
    private void Update()
    {
        // Raycast from the screen point to the world with a distance of max 100 meters.
        var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out var hit, 100))
        {
            // Look position is the point where the raycast hit something.
            lookPos = hit.point;
            // Keep the height of where to look at, at the same height of the character.
            lookPos.y = transform.position.y;
        }

        transform.LookAt(lookPos, Vector3.up);
    }

    // FixedUpdate is called at a fixed interval, which can be multiple times a frame or even skipping one.
    private void FixedUpdate()
    {
        // Likely I'm wrong to use Time.deltaTime here as well.
        Controller.Move(transform.forward * MoveSpeed * Time.deltaTime);
    }

#if UNITY_EDITOR

    private void OnDrawGizmosSelected()
    {
        Debug.DrawLine(transform.position, lookPos, Color.red);
    }

    private void OnValidate()
    {
        if (Controller == null)
        {
            Controller = GetComponent<CharacterController>();
            UnityEditor.EditorUtility.SetDirty(this);
        }
    }

#endif
}

While it’s not the source of the offset you see (I don’t think it is anyway), using FixedUpdate() is incorrect here.

Two good discussions on Update() vs FixedUpdate() timing:

https://jacksondunstan.com/articles/4824

https://johnaustin.io/articles/2019/fix-your-unity-timestep

Thanks for those, I will check them out. My coding experience is from years back and I will be going back to it at some point to relearn everything, but it in the mean time it will be messy

could animation rigging cause my problem? I’m probably going to rewrite the entire look code or use a new model and see if anything changes

Hey that created very random problems with the player flying all over the place

So I fixed the issue be reseting everything. But it’s still central to the character opposed to the gun position. How can I change this?
I made a firepoint for the end of the gun but not sure how to implement this?
Or could i offset the players rotation in script to account for this?

I was wondering if to change this line of code? So it’s not from camera position. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);