Spawn actor and align above terrain

My actors spawn either below or in the middle of the terrain. How can i make it so that when they spawn they automatically align above the terrain?

Get the terrainā€™s Z value and add your character Z value to that. Then set that is the spawn height.

1 Like

How would i do this?

If you canā€™t actually place the actor above the terrain,

Make an EventBeginPlay node. Connect that to set actor location. Then in the location if the terrain is an actor you can wire the terrainā€™s location into an addition node and add enough to put it right over the terrain.

If the terrain is a BSP, or made with the terrain builder, you should be able to get it Z value by either clicking on the terrain, or just placing a dummy actor on the same level as the terrain and just copy that actorā€™s Z-Value, into your addition node which wires into your set actor location node. You should be adding just enough to place the actor above the terrain. You may need to mess around with this value a bit to get it right.

Yeah, but that will only be good for a flat terrain surface. I Want it to be dynamically. Like when iā€™m about to place a campfire in front of me, a green mesh spawns right in front of me and everywhere i move it moves along the terrain without clipping through, it always sits on top of the terrain and aligns itself perfectly with it so it can be placed there.

If you cant get the terrainā€™s z adis then what youll need to do is add physics to th campfire, and attach it to the player. Use an event tick node to check using an if statement wether the players velocity is not 0. If this is true then just update the fapfire to move with the player. Then give the campfire physics and make it strongly affected by gravity so it falls almost instantly. Another way is adding the campfire asa component to the player.

One solution would be to do a line trace from up above the spawn location, down towards the terrain. Then you can get the location of the hit on the ground, and spawn the actor there, or slightly above that location if the actorā€™s center isnā€™t at ground level.

If you want an actor to align to the angle of the surface, you need to use that hit info to get the surface normal, which is a normalized vector perpendicular to the surface.

Okay, i have created a camera above the player that sends a trace down and spawns the actor above the terrain like i wanted, but iā€™m still a little confused on the ā€œAlignmentā€. I Break the hit result, and i do see a ā€œNormalā€ vector on the node, but what do i do with it?

A camera isnā€™t necessary for the trace, you just need to give it the start/end location vectors. You could have the player perform the trace, or whatever is placing the object would be appropriate.

To get a start location up high, you could use your start location + a new vector with the Y set to like 500. End location could be the same but -500. Draw the line in debug to make sure itā€™s hitting the ground.

EDITED
Removed incorrect information after testing.

I Did that. I Spawn the actor, i use ā€œSet actor rotationā€ and i use the "Rotation from XVector return value as the new rotation. and i seted the ā€œAngle degā€ to 90 and i set the Z to 1.0 and it does rotate, but not the way i want it too. itā€™s all off.

and here is where i get the trace info from

and the result is this

when itā€™s supposed to be like this (i placed them my self)

1 Like

Hey there,

So I finally got some free time, and I set up a test project for this.

I was testing by spawning an actor using the Transform output of the blueprint in the screenshot, but you can just use the rotation if thatā€™s all you need.

This seems to create the desired result. Give it a try!

Whatā€™s happening here is that the ā€œMake Rot from Zā€ node is creating a Rotator with the Z-Axis aligned to the vector input (the surface normal). Then you can apply that Rotator to your actor, and your actorā€™s Z-Axis will be aligned with the surface normal as well.

Let me know how it goes.

2 Likes

Sweet, it looks legit. Iā€™ll try it and let u know if it works! Thank u so much i appreciate it alot man.

It works! haha. I Just have one more question, how can u make it so that the actor wont spawn on walls or super steep hills like this

Like, if you try to spawn on a steep hill like that or a wall, iā€™d like it to print a string saying ā€œYou canā€™t spawn hereā€ Than iā€™ll be pretty much done with object spawning. Iā€™d appreciate if u can help with this!

iā€™d like for it to spawn on flat surfaces and slightly slanted surfaces or whatever seems realistic.

OH and one more thing. How can i add a preview mesh to update with the line traceā€™s location and follow the line trace to preview where i want to spawn the item? and maybe rotate the mesh and when i spawn it, it uses the preview meshs rotation and location? Like this:

Watch this video and youā€™ll know what iā€™m talking about The Forest - Gameplay / Walkthrough / Playthrough - Part 1 - WE MUST SAVE TIMMY! - YouTube

Hey,

I definitely think I can help you with that, but I wonā€™t be able to run any tests until later. Hereā€™s an explanation of how you might be able to set that up.

Essentially what this would do is immediately spawn the object with perhaps a ā€˜preview materialā€™ which is just white color, with some material parameters to change opacity and possibly color. These parameters can be given new values via Blueprint. This object will update itā€™s location based on where youā€™re aiming, and can be rotated with a key binding.

Itā€™s going to involve first finding the angle that the surface normal makes relative to the world Z axis (some vector math). Then you can check if that angle exceeds a float that you designate as the limit. You also may want to do a sphere trace at the objectā€™s location to detect any overlap collisions that would mean it canā€™t fit.

Then based on that you can send new values to the parameters in the material instance to tint it red if it canā€™t spawn, or to make it less translucent once it has been placed (you could also just swap out the material at this point to the real one if it doesnā€™t include additional crafting).

If youā€™ve never worked with instance materials before, theyā€™re not nearly as difficult as they sound, and unreal has some nice short tutorials for how to use them.

But I will try to make some time to test this out later, because Iā€™m also interested in this kind of mechanic. (More inspired by TF2 engineer buildings though).

Okay, sounds pretty good. I Just never worked with dynamic instance materials before. Iā€™ll Try to give it a go and see if i can get it to work, if not, iā€™ll just wait for u to test it out. Thanks for helping me out with this btw i appreciate it alot i wouldā€™ve never figure it out by myself

Hey,

So last night I made a working prototype of this system, and tonight Iā€™m going to try and clean it up and add a bunch of comments.

Just a warning though, it is much more involved than I originally thought and will require many screenshots to show everything that is happening. I broke most of it up into functions so that itā€™s as clear as possible. Iā€™m not sure if answerhub has a limit to what I can upload, but if I canā€™t get everything up Iā€™ll upload everything to a Dropbox album or something.

I just have to add the ability to rotate the actor before placement, and Iā€™ll post it here hopefully tonight.

Okay, iā€™ll be waiting.