Teleportation and Terrain

Hello chiefs,

I created a teleportation system using Raycasting.

I use result.Position to get the position of the hit, and then CFrame.new(pos) to get the CFrame of the position.

I then teleport the player to that position by simply doing HRP.CFrame = pcframe.

It works great when the player teleports to an object that is a part! However if the Raycasting hits Terrain, then the Player seems to teleport inside of the Terrain.

Here is the part:
https://gyazo.com/199838e6677d9c2b0f2b4f2341e31ee9

Here is the terrain:
https://gyazo.com/a4e10496093f08cd6aa05dfac1f79c3f

EDIT: I am jumping off the part to reset, but I am falling off of the Terrain

I am not sure if I cannot teleport using CFrame for Terrain, or if there is an issue with my code, but I pretty much included all of the code included in this function above in the explaination.

If someone can get me on the right track or link me a similiar issue, that’ll be great!

1 Like

Raycasting should detect terrain, can I see more of the raycasting code?

1 Like

I think the “issue” is that it’s teleporting onto the surface, try offsetting it by like 3-5 studs in the Y axis

1 Like

Do just like what @RawEggTheGreatIX said but make a check if it was terrain then only do the offset.

1 Like

Right well I may not understand how Terrain works correctly when saying this;

But from the Terrain GIF, it seems like its teleporting a little bit inside of the Terrain instead of the Surface.

You can see that the characters legs are inside of the wall, and then are pushed out. I think that may be why the player cannot stay on the wall after.

But like I said I may be wrong about that, and either way I could probably fix it my offsetting as suggested.

Raycasting is detecting Terrain as the player wouldn’t teleport without a hit.

I believe that this issue is more the teleporting aspect of the code, as it seems to teleport the player into the Terrain.

My theory is that a terrain’s collision is more complex than parts, and thats probably why. Parts that are hit are usually 1 surface where the player would hit, so the 1 surface would “nudge” the player. But for terrain there’s probably multiple collision surfaces the player is hitting, causing the force to cancel out or something like that, and/or the terrain’s collision is slightly lower than what the terrain visually is (makes sense in that term).

Like you said, offsetting would fix it.

1 Like

I do not believe that I can simply using HRP.CFrame = pcframe + Vector3.new(3,0,0) as this would move the player to a different location depending on where they are and which direction they are teleporting in.

I would have to use CFrame:ToWorldSpace()? Or is there another way that I am not realizing that I can use?

you can get the exact CFrame of the surface by doing this:

local SurfaceCFrame = CFrame.new(Result.Position, Result.Position+Result.Normal)*CFrame.Angles(math.rad(-90),0,0)

local OffsetPosition = SurfaceCFrame:PointToWorldSpace(Vector3.new(0,3,0))

basically you find the CFrame of the exact surface, and get a position Vector3 version of :ToWorldSpace() to offset it by 3 studs. Offsetting by local space is probably better than world space.

1 Like