How to change the direction of the RayCast?

Hello, I’m making the 2D laser.

And It’s working fine, but I need to rotate him, so its not creating the laser on up direction.

So I have this part of the code:

RaycastHit2D hit = Physics2D.Raycast (transform.position, transform.up);

So how can I change the code that the ray will be rotating with the parent object? And not will cast to up direction?

Maybe something with Translate?

Please help if you have any things in mind about this, thanks! :slight_smile:

Well, let’s say, you want to raycast in the direction the object is facing. So, you could just pass transform.forward instead of transform.up.
There are many other ways specify the direction of the ray but those depend on the actual situation.

Hi @Rembo4Fight

You need to use transform.right.
The end point of the laser is retuning (0,0,0) because your raycast is probably not hitting anything.
Use the code snippet below.

    RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right);
    if (hit.transform != null)
        lineRenderer.enabled = true;
        laserHit.position = hit.point;
        lineRenderer.SetPosition(0, transform.position);
        lineRenderer.SetPosition(1, laserHit.position);
        lineRenderer.enabled = false;