Raycast Lag When Moving

I have searched the internet far and wide, I have read everything about raycasting, interpolation, fixed timestep, update and fixed update… And so much more but I am so stuck on this issue. And I really hope someone can save me.

My code works great, except for 1 problem, which is when I move my character, the raycast lags behind.

I want it to be exact.

I have tried Update & Fixed-update, tried all different ways to program it. Lets just say Ive tried a lot.

I know the ray its self is lagging behind because I can see it in debug.drawray, my linerenderers also follow the same pattern. I am really stuck on this one guys. If you need anymore info please ask and I will provide. Here is my code currently.

    private void openMenuInRightHand()
    {
        rightHandRay = true; //For inspector viualisation only 
        rayLine.enabled = true;

        //Set ray from left hand
        ray = new Ray(leftHand.position, leftHand.forward);
        //Set ray line renderer from left hand
        rayLine.transform.SetParent(leftHand.transform);

        Debug.DrawRay(ray.origin, ray.direction);


        if (Physics.Raycast(ray, out hitInfo, 100, testMask))
        {
            rayLine.SetPosition(0, ray.origin);
            rayLine.SetPosition(1, hitInfo.point);

            castAll();
        }
        else
        {
            rayLine.SetPosition(0, ray.origin);
            rayLine.SetPosition(1, ray.origin + ray.direction);
        }

    }

   

    private void FixedUpdate()
    {
        if (enableRaycasting) {

            if (MiniMenu.menuOpenInLeftHand)
            {
                openMenuInLeftHand();
            }
            else if (MiniMenu.menuOpenInRightHand)
            {
                openMenuInRightHand();
            }
            else {
                rayLine.enabled = false;
            }


        }


    }

If really there was nothing on the internet for this then here it goes,
It looks like the problem is that you are calling the openMenuInRightHand() function in the FixedUpdate() method, which is called at a fixed interval determined by the “Fixed Timestep” value in the Time settings of your project. This means that the raycast will be performed at regular intervals, rather than every frame.

One way to fix this problem is to move the code from the openMenuInRightHand() method into the Update() method, which is called once per frame. This will ensure that the raycast is performed every frame, and the raycast will be more accurate because it will be performed at the exact moment that the player moves.

Another option is to keep the code in the FixedUpdate() method, but to use the Time.deltaTime value to interpolate the position of the raycast based on the time that has elapsed since the last fixed update. This will allow you to use the fixed timestep for other parts of your game, while still ensuring that the raycast is performed accurately and without lag.

I am sure there is something out on the internet somewhere, but I have searched far and wide and came up empty.

The first part I understand. I have tried using Update as well as fixed update. Im not sure why I need to move the openMenuRightHand method into update since its already called using update, so im a bit confused by that. And for the last part you mentioned, ive read a lot about all this stuff but Is it possible you could give me an example? ive read a lot about interpolation and i tried to find examples in code but couldent find anything that related to my issue.

Another thing to mention is I am working on a VR project. And most of the movement code is handled by Oculus and there are things out of my control. The ray is supposed to go from the hand to the hitinfo point or ray.direction in the case it doesnt hit anything.

No idea what to do, this “Bug” is on the top of my priority list.

Also thanks for taking your time to answer me, I hope I can figure this out soon.