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.
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)
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.
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.