Define Raycast 2D Length

Hello,

I’m trying to create a ray going from a sprite into another one.

The actual ray is being rendered fine, but I can’t set the lenght of it… All I need it’s to set a current “size”, like a height for that ray, doing that I’ll be able to ajust the time of the collision between the sprite and ray, and active my animation in the prooper time!

And no, I don’t want to change the ray size just when it’s being debbuged by doing: " ray.direction * (some float value);"

Here’s a pic of what’s being rendered when I debug my ray…

And here’s my code:

void Update () {
        ray = new Ray (rayPosition.position, (GameObject.Find ("Ball").transform.position - transform.position));

        hit2D = Physics2D.Raycast (ray.origin, ray.direction,20.5f);//Shouldn't the 20.5f work for the lenght of the raycast??
        Debug.DrawRay (ray.origin, ray.direction, Color.green);

        if (hit2D.collider != null) {
            if (hit2D.transform.gameObject.tag == "Ball") {
                Debug.Log ("Colidiu com a Bola");
                PhysicsMaterial2D physmat2d = gameObject.GetComponent<PolygonCollider2D> ().sharedMaterial;
                physmat2d.bounciness = 1.25f;
                anim.SetBool ("Activate", true);
            } else {
                PhysicsMaterial2D physmat2d = gameObject.GetComponent<PolygonCollider2D> ().sharedMaterial;
                physmat2d.bounciness = 0.5f;
                anim.SetBool ("Activate", false);
            }
        }
    }

Thanks!!!

 hit2D = Physics2D.Raycast (ray.origin, ray.direction,20.5f);//Shouldn't the 20.5f work for the lenght of the raycast??

^It is, and this is where the actual raycast is happening. Rather, this is the maximum distance you are telling Physics to check for collisions from ray.origin, so anything the ray intersects beyond that length won’t be returned. However, the raycast and actually drawing the ray are independent statements in your script.

As for drawing, you are passing the start and end points of the ray using ray.origin and ray.direction, which means the distance between those two points is going to be the length of the ray. If you want to specify the length with a float, try using

float rayLength;

Debug.DrawRay(ray.origin, ray.direction * rayLength, Color.green);

The direction variable of a Ray (Ray.direction) is normalized, which means it has a magnitude of 1 while retaining its direction data. Since Debug.DrawRay() uses a single Vector3 for direction AND magnitude, just using a Ray.direction will always give you a ray of length 1 in some direction.

1 Like

Thanks for your help.

But I’ve changed the distance of the raycast from 20.5f to 500 and above and the raycast wasn’t able to collide to the ball, just when it gets really close to the start point of the ray…

I guess I misread your question, thinking that it was the draw that wasn’t working instead of the actual raycast.

Have you tried putting any other colliders in the path of the raycast? Are you sure it isn’t hitting some other collider first, like the collider on the source object? I think raycast stops checking and returns after it finds the first intersection.

Hey. Stumbled across this thread whilst looking for the answer to my question, which was as your understanding of the original question. Anyway, your answer helped me loads and i just wanted to say thank you :slight_smile:

2 Likes