Create a bouncing Laser.

Suleiman Abdullah
2 min readMar 1, 2024

--

Objective: Creating bouncing Laser.

In the first step create a scrip called bouncing laser ,create a vector two local variable called origin and assign to object position(transform.position) , create another vector 2 variable and assign it to transform.right.

Create a ray and pass above variable as origin and direction of a ray. Then use Gizmos.DrawLine for visual purpose ,which will draw a ray from the object to direction of a ray.

Use physics dot ray cast to check if we hit objects, then we will draw a small dot to a point where we hit the surface. I add a line from the object to the hit point.

Note: This Gizmos only for visual presentation .

Create a return method called Reflect ,with two vector2 parameter ,one passing ray direction and other for surface normal.

we need to neget vector projection

Go to the physics dot Raycast ,create a vector to variable called reflected assign to the above method then pass ray direction and normal.

To visualize the ray reflection, use gizmos to draw line from hitpoint then the hitpoint plus reflected.

This is the all code in this scrip.

This is how to create a bouncing laser.

See you in the next one.

--

--

Suleiman Abdullah
Suleiman Abdullah

Written by Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development

No responses yet