Line Trace by Channel help please (blueprint)?

Here is my dilemma.
I have created a blueprint which at each game start, takes a defined number of meshes of a certain type and applies a random (within defined limits) Transform and Rotation to the object. In this case it is a rock. I intend to use this to apply to my Landscape to have a random scattering of rocks over the level. I will also use this for other objects, with the aim of creating a unique level every time you play (aside from the Landscape itself.

This would currently work as it is if the Landscape was flat, but of course it isn’t so what I need is the ability for each mesh to do a Line Trace By Channel to check the Landscape’s floor height and snap to that when spawning the individual meshes. I have created a custom Channel called Landscape and attempted to do some of the blueprint myself but I’m struggling with getting the hit location and using that to generate the Z value and turn that into the height information for the individual meshes.

If someone could show me a simple, complete usage of this it will hopefully give me enough clues to integrate something similar into my current blueprint. or if more info is needed I could share what I currently have in my Construction Blueprint for the meshes and you could tell me where I’m going wrong.

Thanks in advance, this’ll help me TONS.


Hey Daz,

If you create a custom Trace Channel called Landscape, with the default set to Ignore, and then adjust the collision on your landscape to block the Landscape Trace Channel, you can set up something like this in your Actor BPs:

What this does is get the Actor’s location and the difference between that and a point straight down. I have the default for the variable TraceLength set at 0,0,1000, but you may need a longer trace depending on how high above the landscape you’re spawning your Actor. The Trace Channel is set to Landscape, so it will only detect Actors with Landscape set to Block.

Then it takes the Out Hit and gets the location of that hit, and sets the Actor Location to that point. If you’re rotating your Actor randomly, you may need to add a Z offset to this depending on the where the pivot point is in relation to the mesh.

Hope that helps!

Hey Ben,

Well that helped me a hell of a lot and was very clearly explained. It ALMOST worked out of the box for me, but I think I am complicating it by already having a blueprint doing some other location and rotation stuff.
Basically, it works for one of my Rocks, but the others are still floating too high.

here is what I have at the moment. I have tried hooking the Line trace in at various points in the bp, but I get similar behaviour every time. It may need me to edit the way I have done some of the other stuff in there before this works, but hopefully you can set me straight!? Sorry, bit of a newbie here to UE4. Your time is very much appreciated!

P.S - Ignore the errors. It runs fine, I had just not compiled it at that point and moved some stuff around…

If Ben is not around to follow up on this, can anyone else help please?
I feel so close to doing it seeing as it works on one mesh but not the rest, or as in once config I tried, it worked on them all but they were all on top of each other!!!

Come on you geniuses, help out a thicko newb

It looks like you’re only doing the Line Trace once, after the Loop completes. This means you’ll only set the World Location for one component rather than all of them. You should probably run this after the Add Local Rotation node instead, essentially adding the component, setting a random location and rotation, then dropping it to the landscape below for EACH component made. Right now, you’re only doing it for the last component saved in the ProcStore variable.

A couple things to consider:

  • You’re setting a Transform for the mesh component. Transforms include a Location, Rotation, and Scale vector. If you wanted to, you could calculate the random Location and Rotation values and create a Transform, then feed that into the Add StaticMesh node. That way you don’t need to add a Local Rotation after creating the component.
  • Your Line Trace is using the Actor Location for the Start value, but this is taking the World Location of the BP that contains all of this. Instead, if you’re running this all from a single BP, you would want to use the location of the target component as the Start. Try using ProcStore > Get World Location instead.
  • You’ll need to make sure that the location you’re giving the mesh is above the landscape, or it won’t drop and could end up colliding with the terrain. For rocks, this might be okay depending on what you’re looking for, but you should be aware of the possibility. You can solve this by clamping the random vector you’re using for location, if you want.

Happy to help! Let me know if you have more questions.

Hey Ben,
Thanks so much. This now has the desired effect of randomly spreading my meshes around the map!

2 Questions though, please:
1> When i set my ProcAmount to something like 500, which should take 500 meshes and spread them randomly, I’ve noticed that there is a nice spread but many of the meshes are on top of each other (with random rotations). I cannot be very precise and I don’t think it’s every instance of a mesh that has multiple meshes on it, but flying to a few at random there will be at least one of the locations that has multiple instances on top of each other. WHen I say they are on top of each other, I don’t just mean they are overlapping, I mean they are at the exact same coords but with different rotations.
2> As a byproduct of the first question, how would I ensure during the spawn checks that two meshes do not overlap? I guess I need a secondary check using a sphere trace or something that checks for overlap and randomises the transform again if it hits another mesh? Could you give me a clue how I could incorporate such a check into the blueprint please?

Thanks again!

Yep, that’s a result of random number generation. If you read up a bit on that, you’ll discover that computers can’t actually generate random numbers, but fake it in a number of ways that makes it harder to tell. With a large enough number of “random” vectors, you’ll see a fair amount of overlap. They might not be at exactly the same location, but close enough that they might as well be.

As you suggested, doing a check to make sure it’s not touching anything when it’s spawned is how you could get around this. Since it sounds like you’re going for something a little more complex, I would recommend checking out these resources:

  • This link is some of our documentation on random seeds. There’s also a neat little one-shot tutorial that you might find interesting here that goes is similar to what you’re looking to do.
  • In the Learn tab in the Launcher, there’s a downloadable project called Blueprint Office. The most recent version is 4.5, but it converts to 4.6 with no problems. In it, you can find a pretty awesome blueprint called BP_Random_Foliage that spawns a bunch of plants from an array of static meshes that each align to the surface within a specified area. It’s essentially an extended version of what you and I have been discussing, using Instanced Static Meshes. There’s some documentation that helps explain how it works here.

Taking a look at that project and the BP_Random_Foliage blueprint inside it would probably help your project quite a bit if you’re looking to randomly place a lot of different meshes in your levels.

Hope that helps!

Since I can’t post my own question I’ll post here, since it is somewhat similar. I need to get the hit results from a line trace and use that to set actor location and rotation. I have a mirror actor with a spot light and texture that I need to face in the same direction and start location as the reflected line trace. Any thoughts?

Hi rysmith25,

It’s not really clear what you’re attempting to achieve. Can you be more clear about the desired result, what’s actually happening, and your current setup?

Why can’t you open a new post? This would be best addressed in another post in the Blueprint Scripting section. If you’re able to now, I would recommend doing so with the information requested above. Thanks!