Ray not detecting player

Hello all

So I have run into a strange issue here. Basically I have a moving block casting three rays in the direction it is currently moving. I have a top, middle, and bottom ray. For some reason, only the top ray is detecting collisions, can anybody see why this would be? I have been staring at

    void Update () {
        Vector3 position = transform.position;
        Vector2 temp = new Vector2(((speed * dir) * Time.deltaTime), 0.0f);

        for(int i = 0; i < 3; i++){
            float x = position.x + center.x + size.x/2 * dir;
            float y = (position.y + center.y - size.y/2) + size.y/2 * i; 
           
            ray = new Ray(new Vector2(x, y), new Vector2(dir, 0));
            Debug.DrawRay (ray.origin, ray.direction);
            if(Physics.Raycast (ray, out hit, Mathf.Abs (speed) + skin, playerMask)){
                Debug.Log ("HIT MOTHER FUCKER");
                float dst = Vector3.Distance (ray.origin, hit.point);
            }
        }
        transform.Translate(temp);
    }

Pretty hard to diagnose without seeing the scene. One thing I will say with Raycasts is that it’s trivial to put in a Debug.ShowRay() to be able to see the cast in the scene view and see if anything’s going wrong that way.

How is drawing the ray to make sure it 1) exists, and 2) is going in the correct direction trivial?

Does anyone have anything helpful they could suggest?

As far as I can tell, only the top ray (when i ==2) is detecting collisions. Even without seeing the scene, it should be something within the block of code that I posted, I am just not sure what, as I am fairly new to Raycasting.

I think it was just a suggestion in case it’s not something you’d checked.

Have you tried moving the Physics.Raycast call outside the for loop to see if that helps? It looks like the RaycastHit variable is being used by all three rays, so you’ll only get info on the last ray to be cast.

That’s assuming the Debug.Log call is showing as you expect in the console, though.

Regardless of the Physics.Raycast call though, it should still print to the debug log if there is a hit right? Cause I create a new ray in each iteration, check it, if hit execute the code, then move on to the next

I noticed that if I scale up my object it doesn’t seem to have as hard of a time detecting these collisions. I have the moving block of size (1,1,1) and the player block it is trying to detect is (0.5, 0.5, 0.5). Would I need to cast more rays? I can see that the ray is passing right through the object when it is scaled down, but is not reporting any hits

Would there be a reason within my code for this?

This is just very strange, because the algorithm I am using is identical to what I am using for the player, but I am not having any issues with the player object. I have ensured that the playerMask is correctly setup in the editor and everything.

Pretty sure Physics.Raycast will return on a physics update? So casting a ray in a loop doesn’t make sense to me. How will it have time to calculate the ray?

Maybe this is your problem?

Notes: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour. If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it’s data structures, before a Raycast will hit the collider at it’s new position.

I’m not sure what you’re saying here.

It doesn’t matter where the code is in Update(), it’s still going to be calculated every frame.