x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Aiming laser that redirects when it hits an object?

Hi guys.

I'm creating an aiming system that displays a laser emitting from the player mesh that shows the projected direction a projectile would take show it hit an object and bounce off it. I have created a beam particle who's target will become any object it hits, however I don't want it to end there and instead reflect off in the direction the projectile would bounce. I believe this could potentially be achieved through debug lines, splines and/or beam particle tangents however I don't have much experience with any of the above. Bare in mind I'm creating a networked multiplayer game so the less taxing it is the better, thanks guys :)

TLDR: Want to create aiming laser that bounces off objects. Think of how grenades are thrown in Gears of War.alt text

This is what I currently have:alt textalt textalt text

Product Version: UE 4.11
Tags:
download.jpg (6.8 kB)
capture.png (371.6 kB)
capture2.png (517.4 kB)
untitled-1.png (778.2 kB)
more ▼

asked Jun 21 '16 at 01:19 PM in Blueprint Scripting

avatar image

DanSaxon
43 8 14 21

avatar image Nevermore Jun 21 '16 at 01:50 PM

I'm not sue if I got this right but can't you just spawn another actor of the beam class where your previous beam hits the wall?.

avatar image DanSaxon Jun 21 '16 at 05:35 PM

Potentially, however my only issue is performance as it's a networked multiplayer game that has quite a few particles and intensive shaders to begin with, so I'm trying to improve performance as much as possible.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

The formula in Scott's answer is a very efficient way to re-direct a projectile hitting a surface in a new direction, that part is true. But there's a lot of other stuff that's got to happen in the "you just need to make another beam" part of his answer that isnt explained and that formula isnt in blueprint format either.

So let's expand a bit. In order to maintain a total combined beam length of 1000 units per your blueprint, you'll need a short list of things to go get. You'll need:

  • A "new start point" and "new end point" to construct beam #2.

  • The "old start point" and the "impact point" of beam #1.

Using his formula, let's turn the velocity on the left side into "new direction" and the velocities on the right side into "old direction". Using the impact point as "new start point", we can use the new direction to figure out the "new end point" if we also can figure out how much beam is left over. [Note: You should probably test that there is beam length left - meaning the impact happens within 1000 units, otherwise skip all this calculation stuff].

alt text

Starting with the old stuff, "old start point" is the tip of your gun. Use a socket on your gun or whatever was fed into the start point for beam #1 here. (Not shown in image). The "old direction" is the direction beam 1 was shot in - again you can use a forward vector on the projectile or gun socket to feed there before the trace. You could also subtract the Trace End from Trace Start too. Just make sure you Normalize that before feeding it to that Dot product node so the pointing direction is a length of 1.

Right, so onto the new stuff. "New start point" is easy - that's the impact point included in the Hit result. "New end point" is a little more complicated. We gotta know which way that beam is going to go, and how much beam is left to extend in that direction, yes?

Let's do the length first because that's based on old things we already know. The Vector Length from your gun to the impact point tells you how much was used in Beam #1. Subtract that from 1000 and you have your "new length" of Beam #2. This is also a handy point to do a branch test because if that length is negative, there's no Beam #2 to be made.

To get "new direction", let's re-write Scott's formula as: NewDir = OldDir - 2 ( OldDir dot ImpactNormal ) ImpactNormal. Note that for a line trace, normal and impact normal give the same thing.

Last step, now you have a unit vector pointing down the path the beam will reflect in, you just need to multiply that by whatever beam is left over and add that result to the impact point in order to determine "new end point". I didn't actually test the BP in the picture so might have some things mixed up there...

more ▼

answered Jun 21 '16 at 11:06 PM

avatar image

1l2Hawk
690 36 35 74

avatar image ScottSpadea Jun 21 '16 at 11:30 PM

yeah, i was gonna expand my answer with a blueprint example, but i got lazy. i have made a similar laser aiming system before, but i just used debug lines, instead of beams, but i don't have that project on my computer anymore, so i couldn't find an image of the finished blueprint.

since his image showed he could make beams, and i have no idea what kind of physics his projectiles will use, i just answered the unsolved part of his question, providing the math for bouncing off walls.

 Velocity = Velocity - 2 * ( Velocity Dot SurfaceNormal ) * SurfaceNormal

i suggest using more than 2 beams, and making an array of locations, where each beam is made out of 2 of those locations. i would calculate this array by looping through some projectile physics math, to calculate a line trace, to detect walls for bouncing. every physics iteration, i would add another point, and every hit detection i would add an extra point, and change the velocity in the physics math using that reflection vector formula.

its not just hitting a wall that might change the direction, gravity on a projectile will make an arc, which should be made out of many points. each point in the list should be the end of one beam and the beginning of another, and this point data should be calculated for the whole laser in a separate step before updating the visuals for the beams.

this is kinda an expensive thing to make in blueprints, if you can calculate the list of points in C++, it will be far more efficient. then in blueprint, you can set dress those points with whatever you want, using beam particles, or scaled instance static meshes, or project the locations to render icons on the hud, etc... but this kind of code really belongs in c++, probably in a custom projectile movement component, or in a C++ blueprint function library.

avatar image DanSaxon Jun 25 '16 at 12:45 PM

Thanks guys, it has nearly worked. It calculates the length to perfection, it however doesn't give the correct rotation and just makes it face upward/forward - depending on perspective, any idea why? Here's a link to what is currently happening:

https://i.gyazo.com/7553cba6a0c9304c864e6550f8ac206a.gif

This is the blueprint: alt text

untitled-1.png (2.5 MB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Cheers guys, I'll test the stuff out as soon as I get to my computer. In regards to physics. They are restricted to movement along the xy plane and are not effected by gravity.

more ▼

answered Jun 22 '16 at 09:35 AM

avatar image

DanSaxon
43 8 14 21

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question