Checking for clipping when spawning an actor at a mouse cursor location

Hey there, new hobby game dev here,

I am making a game in my free time to keep me busy and without going into to much detail, part of it involves placing an object in the scene wherever the mouse cursor is on the floor.

Currently I have a system in place where a line trace is projected from the camera to the mouse in world space, and then attempts to line trace again straight down to make sure that no objects are levitating. It will then do a check to make sure that the object is in view if placed in the hit location but that isn’t too relevant for the problem I’m trying to solve.

This system works pretty well except for one thing… objects can clip into walls, which I don’t want. I use a lerp and timeline to smoothly move the object to wherever the mouse moves but the object isn’t stopped once collision is made. I use the sweep option on Set Actor Location but that didn’t do anything, assuming it is because I don’t simulate physics in the object.

To try and fix this, I turned on simulate physics, but the actor didn’t even move to the line traces hit location, probably because I initially spawned it at the world origin and it was getting blocked by walls when trying to move to the mouse location. To combat this, I set the initial spawn location to be at the mouse location, but the actor still refuses to move to the line trace after this. However when I click to spawn the actor regardless, it does spawn at the mouse location but does something weird. It just starts floating in a random direction across the floor and spinning in a circle for some reason…

My question is, does anyone have an idea on how I prevent clipping WITHOUT simulating physics, or if that’s not possible (which wouldn’t surprise me), how could I get around this weird issue of it just levitating away, and the preview not properly updating?

Thanks for help in advance!

EDIT: I have done more research and found that Simulate Physics shouldn’t be required to check collision, so I have turned that off again for now. I checked the sweep option properly and it says that only the root component is checked for collision, which I guess means that I would need to have the mesh as the root component. This is an issue though as the objects that can be spawned are all child classes so the root component is unchangeable… any ideas on how I can get around this? Or is there any better way of going about preventing the clipping? Now that I think about it, the objects could get stuck really easily during movement if it was stopped by sweeps…

Best bet would probably be to take the location of the mouse trace and do a box or sphere trace from it, then spawn the actor if the box or sphere doesn’ hit anything. Could also try spawning an invisible dummy actor at the mouse trace location,setting its spawn conditions to try and adjust and have that spawn the desired actor in its begin play.

2 Likes

In C++ you can use GetWorld()->FindTeleportSpot to find a non colliding location for the actor

In BP you can try moving the actor with “Teleport” node instead of setting location directly

1 Like

It did cross my mind to use box or sphere collisions to prevent the clipping but the same spawn function can spawn a LOT of different actors of shapes and sizes so it just wouldn’t be the right size for different actors unfortunately.

I could do the adjustments in the begin play too but I also imagine that it would be quite complex to do that given the nature of my game and the world having lots of thin walls where the actors could move in the wrong direction to get out of the collision, so thats a last resort at the moment I would say. Thanks for the suggestion though!

I had no idea that teleport also moved the object in the case of a collision, I will give it a a go now!

EDIT: Teleport didn’t fix the issue, which makes me think that it could be a general collision issue but I doubt it as all actors and meshes have their collisions enabled to block eachother…

Regarding the actors having different sizes you could also use the size as extents or radius on the shape trace. Or… Conan Exiles uses a trick similar to my dummy actor suggestion.They have a master placeable class which initially doesn’t collide, but has an overlap checking box collision which is moved around with a line trace. When the place key is pressed it checks for overlaps, and enables collision and such if the check succeeds. The children of the base class have the overlap box tweaked to fit the mesh.Is a bit fiddly if you have to edit a lot of child classes, but it works rather well once they’re all set up.

1 Like

Oh my all this time I’ve been trying to automatically fix any clipping actors when I could’ve just made it unplaceable if its in an invalid location… That’s probably a lot easier to accomplish so I’ll try that next time I’m working on it!