[SOLVED] Drawing a raycast on the screen

Is there code to draw a line where the raycast had travelled and stop where it hit?

Hi @Gavin_Durbin,

Yes sure, you can use the following method:

https://developer.playcanvas.com/en/api/pc.Application.html#renderLine

Just make sure to use it in your update method, since it draws a line for a single frame only.

1 Like

Does it stay there or disappears?

That’s was my point, if you put it in your script update method so it runs on each frame, it will stay there.

If you don’t, it won’t stay there.

1 Like

Do i just have to copy and paste this into the update when key is pressed?

Well, kind of. You need to pass the start and end positions of your raycast, plus a line color, to the renderLine() method.

How are you using raycasting right now?

I haven’t added it yet, I’m trying to do other stuff first

1 Like

Yes, i do have it, for my guns

Sorry for taking so long lol

@Leonidas so how would I implement this?

@Albertos do you know how to implement a raycast drawn on the screen?

Yes, if you share your raycast code I can give you the code to make the raycast visible with a line.

Yessir




This is all of the important code for the gun.

@Albertos

You can add the code below after line 34 of your script to render a white line.

var color = new pc.Color(1,1,1,1);
this.app.renderLine(from, to, color);
1 Like

Can i use a variable or attribute for the “to” on the raycast drawing?

You already did that for the raycast itself and I use the same variable for drawing the line.

1 Like


It works now but the raycast just points downward lol


See? How would i fix this.

I have literally no idea on how i would change the raycast direction