• News

• Industries

• Learning & Support

• Community

• Marketplace

# How to create a laser that bounces on the walls and hurts players?

Hi, I’m currently prototyping a simple multiplayer game (or so I thought) where players are able to shoot lasers and the laser bounces around in the level.

However I’m not sure on how I should create the laser, but right now I spawn sphere projectiles that I’ve set bounce to true on.

What I have in mind:

Currently:

Is it possible to create what I’m trying to make? I could have a game with balls but lasers are much cooler.

Entire project is available on GitHub: https://github.com/Saucy/ue4-laserz

There are probably at least 3-4 ways you could do what you need. Just thinking off the top of my head here…

Make your current ball invisible and increase the speed to the point it would seem instant to travel it’s maximum distance. Then each time it collides store the vector of the collision point. Then when it has reached it’s limit, spawn a spline using the vectors of each collision.

An idea how to do this is all I need at this point. Now I know where to start, thanks!

Making fast moving objects in unreal has one big problem. When you get to 2000-3000 speeds they sometimes go trough walls. I tried this fast moving projectile thing for weapons, and it does not work well.

Instead i used line trace. You can trace line in direction you are aiming. You get location and normal vector in hit spot.
So you have direction of incoming lazer, normal at hit point, you need to calculate bouncing vector.
Easiest way is to normalize incoming vector, negate it then rotate 180deg around normal vector. There are probably faster methods (you should google this), but that rotating is first that came to my mind.

Co now you should have impact point and calculated outgoing vector. Shoot new lazor.

This also works great with beam particles, you can spawn new beam particle for each line trace.

have a look at MirrorByVector in the FVector class

I found this simple line trace tutorial on the wiki: A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums

But I can’t get it to work, he’s using First Person Template, I’m doing a platformer.

Anyway here’s how it looks:

The blueprint:

I don’t understand how I can make something simple as draw a line from my character, to the mouse cursor and continue drawing the line 5000 units further. It’s impossible.

Every time I find a tutorial or something it’s completely different from what I’m doing.
I’ve tried it once before a TopDown prototype where I wanted to draw (yet another) laser, Magicka style.

I just don’t understand.

you need debug line or gameplay one
for gameplay look for beam emitter

you can specify the source, target and tangents if you want the line to have some fancy form

I have a debug line, but its angle is off

I see 2 line traces in your BP but there are actually 3 line traces shown in your image so it is a bit hard to narrow down without more information.

In your image of the game, there is a line trace that appears to be going from the character to the correct mouse location. What part of your script is generating that trace? You need to isolate to that part and make sure that part is being used for the reflection.

I use only line trace to show where I am aiming (dunno how to make a drawn dotted line from player to mouse cursor).

The other lines are where I attack.

When the player attacks the laser should continue until it hits something and then bounce until it has reached max # bounces.
That’s why I need to the laser to continue past the mouse cursor, otherwise it stops at it.
Like shooting a projectile that bounces endlessly until it hits something and bounces until it has reached max # bounces.

Aiming only:

When I press attack, the angle is way off:

Here’s the blueprint:

edit, previous suggestion was not correct

Looks like your 1st line trace is trying to ‘project’ the line by multiplying by 5000, but that is an actual world space location you are tracing to. You do not need to multiply that by 5000 there.

Also you may need to convert the position into a vector before doing “Mirror vector by normal”. That vector can just be Normalize(Start - End). Then you mirror that, and then multiply the result of the mirrored vector by 5000 (or whatever your max length it)

I don’t understand well what are you trying to achieve , here is my comprehension :
You want to fire a laser in a direction and it will bounce from wall to wall ?
It will bounce continuously until it hits a player or the laser has a certain lifetime?

The first attack linetrace expects a start and an end:
Start - where the player is
End - where the mouse cursor is

But if I’m not aiming at a wall (as you can see in the example images in my previous post) the linetrace just stops, i.e: nothing happens, it doesn’t bounce anywhere.
That’s why I thought I’d increase the End point by multiplying by 5000 would help the linetrace hit something on the way to the end point.

I don’t quite follow what you mean, all positions are already vectors?

Players can shoot a laser.
The laser bounces on the walls, and hurts players if they are hit by the laser.

It doesn’t matter for how long the laser bounces or not, it’s just an idea of mine to have a powerup that allows the player to have their laser bounce more times before being destroyed (because it didn’t hit any players)…

If there is no hit, you need to handle that case by using the “Return Value” of the line trace. If there is no hit, then you need to set the hit point manually to be the end point. Use a “branch” node connected to the return value for that.

In terms of “vector vs direction”, what I meant is that you cannot multiply a position by 5000 in order to ‘cast’ it out. That will simply scale the position from the origin (0,0,0). To scale a position relative to another position, you need to first subtract the position you wish to scale from. Then you could multiply it by some number and then re-add it to the start position. I usually normalize to have exact control of the casting distance, since if you don’t, the scaling will be relative to how long the initial trace vector was which could cause issues in some cases. But yes, technically all positions are vectors, they just aren’t relative to your ray so they weren’t really being used as directional vectors as it appeared they were meant to be.

As I said before, you need to trace to the mouse cursor as the end point and handle the ‘no hit’ case. If you need the end point to fire offscreen, then you need to convert your position into a normalized direction vector and then you can multiply it by a large number and add it to the starting position to cast it out further. That was the Normalize(start - end) bit from my last post. Hopefully I made things less confusing not more confusing this time.