When aiming with raycast can't sync mouse position correctly

As you see in the video if i move the mouse fast its aiming correctly but if i move mouse slowly its not aiming.
How can i fix that?

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

public class Aim : MonoBehaviour
{
    StarterAssetsInputs starterAssetsInputs;
    [SerializeField] private LayerMask layerMask;

    [SerializeField] private GameObject debugObject;

    private void Start()
    {
        starterAssetsInputs = GetComponent<StarterAssetsInputs>();
    }
    private void Update()
    {
        Vector3 mouseWorldPosition = Vector3.zero;

        Vector2 screenCenterPoint = new Vector2(Screen.width / 2, Screen.height / 2);
        Ray ray = Camera.main.ScreenPointToRay(screenCenterPoint);
        if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, layerMask))
        {
            debugObject.transform.position = raycastHit.point;
            mouseWorldPosition = raycastHit.point;
        }

        Vector3 worldAimTarget = mouseWorldPosition;
        worldAimTarget.y = transform.position.y;
        Vector3 aimDirection = (worldAimTarget - transform.position).normalized;

        transform.forward = Vector3.Lerp(transform.forward, aimDirection, Time.deltaTime * 40);
    }
}

Is there a Rigidbody on this guy? If so you’re bypassing the rotation mechanism by directly manipulating the transform rotation (line 33)… that will trigger physics to reconsider it as a glitch and try to do various “soothings.”

With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

1 Like

No there is no rigidbody in the object

also there is no collider in debug object

Any idea how can i fix that?