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 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 level. I will also use this for other objects, with aim of creating a unique level every time you play (aside from Landscape itself.
This would currently work as it is if Landscape was flat, but of course it isn’t so what I need is ability for each mesh to do a Line Trace By Channel to check Landscape’s floor height and snap to that when spawning individual meshes. I have created a custom Channel called Landscape and attempted to do some of blueprint myself but I’m struggling with getting hit location and using that to generate Z value and turn that into height information for 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 meshes and you could tell me where I’m going wrong.
If you create a custom Trace Channel called Landscape, with default set to Ignore, and then adjust collision on your landscape to block Landscape Trace Channel, you can set up something like this in your Actor BPs:
What this does is get Actor’s location and difference between that and a point straight down. I have default for variable TraceLength set at 0,0,1000, but you may need a longer trace depending on how high above landscape you’re spawning your Actor. Trace Channel is set to Landscape, so it will only detect Actors with Landscape set to Block.
Then it takes Out Hit and gets location of that hit, and sets Actor Location to that point. If you’re rotating your Actor randomly, you may need to add a Z offset to this depending on where pivot point is in relation to mesh.
Well that helped me a hell of a lot and was very clearly explained. It ALMOST worked out of 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 others are still floating too high.
here is what I have at moment. I have tried hooking Line trace in at various points in bp, but I get similar behaviour every time. It may need me to edit way I have done some of 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!
If 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 rest, or as in once config I tried, it worked on them all but they were all on top of each other!!!
It looks like you’re only doing Line Trace once, after Loop completes. This means you’ll only set World Location for one component rather than all of them. You should probably run this after Add Local Rotation node instead, essentially adding component, setting a random location and rotation, then dropping it to landscape below for EACH component made. Right now, you’re only doing it for last component saved in ProcStore variable.
A couple things to consider:
You’re setting a Transform for mesh component. Transforms include a Location, Rotation, and Scale vector. If you wanted to, you could calculate random Location and Rotation values and create a Transform, then feed that into Add StaticMesh node. That way you don’t need to add a Local Rotation after creating component.
Your Line Trace is using Actor Location for Start value, but this is taking World Location of BP that contains all of this. Instead, if you’re running this all from a single BP, you would want to use location of target component as Start. Try using ProcStore > Get World Location instead.
You’ll need to make sure that location you’re giving mesh is above landscape, or it won’t drop and could end up colliding with terrain. For rocks, this might be okay depending on what you’re looking for, but you should be aware of possibility. You can solve this by clamping random vector you’re using for location, if you want.
Happy to help! Let me know if you have more questions.
Hey ,
Thanks so much. This now has desired effect of randomly spreading my meshes around 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 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 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 exact same coords but with different rotations.
2> As a byproduct of first question, how would I ensure during 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 transform again if it hits another mesh? Could you give me a clue how I could incorporate such a check into blueprint please?
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 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 Learn tab in Launcher, there’s a downloadable project called Blueprint Office. 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 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 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.
Since I can’t post my own question I’ll post here, since it is somewhat similar. I need to get 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 same direction and start location as reflected line trace. Any thoughts?
It’s not really clear what you’re attempting to achieve. Can you be more clear about 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 Blueprint Scripting section. If you’re able to now, I would recommend doing so with information requested above. Thanks!