Need Help With Hiding Behind Wall Script

I am trying to make a script where when the player hits “H” on their keyboard, it makes them hide behind a wall.

I managed to get a raycast to detect if the player is close to a wall when they hit “H,” but I can’t get the player to automatically move a certain distance closer to the wall.

I tried to separate the space in between the player and the wall into four separate positions (example shown below) to mess around with how far the player should be from the wall, but it didn’t work.

image
(Imagine each of the vertical lines as a position, and the horizontal line as the distance between the player and the wall)

By now, I don’t really want the “four different positions” part, I just need the player to be a set distance from the wall

Here is the part of the code I need help with:

if result then
				local hit = result.Instance
				if hit.ClassName == "Part" or hit.ClassName == "Union" then
					local pos1 = result.Position
					local pos2 = character.HumanoidRootPart.Position
					local humanoid = character:FindFirstChild("Humanoid")

Anything helps, thanks!

3 Likes

maybe use Humanoid.WalkToPart?

2 Likes

Wouldn’t the player just kinda walk into the wall? I mean, it might work.

2 Likes

you could try having the wall as a model, and in that model is a part with transparency set to 1 and cancolide is fasle

2 Likes

I was thinking about doing that, but then I realized that every wall, every tree, every fence would need have a part like that

1 Like

oh yeah, and the hitboxes would be broken to. sorry I can’t really help too much

2 Likes

i don’t get you exactly, you just want a player to hide behind a wall?

1 Like

Yeah, it’s a bit hard to explain, but when the player approaches the wall and hits “H” on their keyboard, I want them to automatically go closer to the wall and turn their back towards it.

1 Like

so he will rotate away from the wall that’s it?

1 Like

That probably would work. I would just need to make it so that they can’t interact with the wall until they are close enough to it.

1 Like

just use a proximity prompt?

nvm this

1 Like

I would need to add a proximity prompt to every single thing that you can hide behind

1 Like

whats wrong here? you can make it using a for loop

1 Like

I think I might just make it so that it only works when you get super close to the wall, and then make the player turn away from it

i’ll make a script without prompts, where do you “store” all these items? or a way to differentiate between dem

You can either make an animation or move the player close to the wall with for ex. a Tween where it moves close behind the wall and if needed rotates to your desired direction?

you could try a script that fires when touching a wall and it plays an animation that turns you around

Rn I’m just prototyping it, so all of my items are currently in the workspace

you need a way to differentiate between these items, like a specific things with them, i’ll make a script if there is a way

I’m assuming you’re trying to make a cover system like Grand Theft Auto.

Here’s a great resource you could use to try and figure out how it’s made to then make your own system.