Raycast does not work as intended. is there any alternate approach to this problem?

There are several cubes. a cube rotates around its y axis and has a line renderer component attached to it that projects a line outwards in the forward direction (like the siren on top of an ambulance/police). line renderer is created and destroyed from the code. the goal is when the mouse button is clicked it casts a ray(raycast) and if any cube is hit then to the latter cube is added the script component and line renderer and it starts rotating and the former cube(the ray from which hit the new cube) is removed the script and its linerenderer component. now this works well when the speed of rotation is low (at 1 to 5) but when the speed is 15 and 55 (see images below) it doesnot cast all the time and at 55 it even misses a cube (refer image). how can i make it work ? is there any alternate approach that i can take? THANK YOU :slight_smile:

this is the code :

public class ROTATE : MonoBehaviour {

public float speed = 55f;
private float lineLength = 40f;
private float rayLength = 20f ;
bool check = true;
private string player = "Player";

void Start () {

    
    if (check)//the loop runs the first time script runs and creates line renderer component
    {
        this.gameObject.AddComponent<LineRenderer>();
        //  line.SetPosition(0, Vector3.zero);
        GetComponent<LineRenderer>().SetPosition(1, (Vector3.forward * lineLength));
        GetComponent<LineRenderer>().startWidth = 0.11f;
        GetComponent<LineRenderer>().endWidth = 0.11f;
        GetComponent<LineRenderer>().useWorldSpace = false;
        check = false;
    } 
}

private void FixedUpdate()
{
    LeanTween.rotateAround(this.gameObject, Vector3.up, speed * 5.0f, 1);
    if (true)//(Input.anyKeyDown)here i've put true for debugging so that it casts ray all the time 
    {
        RaycastHit hit;
        if (Physics.Raycast(this.transform.position, this.transform.forward, out hit, rayLength) 
            && hit.transform.gameObject.CompareTag(player))
        {
            Destroy(this.gameObject.GetComponent<LineRenderer>());
            hit.transform.gameObject.AddComponent<ROTATE>();
            this.transform.rotation = Quaternion.identity;
            Destroy(this);

        }
      Debug.DrawRay(this.transform.position, this.transform.forward * rayLength, Color.yellow, 0.5f);
    }
}

This guy does a much better job of explaining FixedUpdate than I ever could.

But basically, it looks like your cube is spinning so quickly that when it’s casting a ray, it misses the cube. As for an alternate approach, you could cast a few rays (say, three) and test if the cube is in any of those.