Problem with using raycasting to teleport

I have this tp move in my game where it teleports you in a random direction by roughly 10 studs.
however to avoid the player getting teleported inside / through walls I raycast in the direction before teleportation. If theres no raycastresult then I proceed with the teleport. However if there is a result then I will tp them to raycastresult.Position to place them infront of the wall instead of directly inside of it / through it.

However instead of teleporting them on the wall it teleports them in the opposite direction a lot farther then the tp is supposed to allow them to travel.

https://gyazo.com/cc2c1fab8fa542c65313fc9ce4e4a5ce
here is a gif of it. The first tp is going into the wall but decided to send them 4x the distance backwards.
the 2 tps after are examples of how far it SHOULD be going.

Any and all help is greatly appreciated. Thx in advance!

What’s your code? If I had to guess what your problem is maybe it’s that you’re treating the position as a direction and adding it onto the players position or something but I dunno

1 Like
local rando = math.random(1,4)
			local direction
			if rando == 1 then
				direction = char.HumanoidRootPart.CFrame.lookVector*15
			elseif rando == 2 then
				direction = char.HumanoidRootPart.CFrame.lookVector*-15
			elseif rando == 3 then
				direction = char.HumanoidRootPart.CFrame.rightVector*15
			else
				direction = char.HumanoidRootPart.CFrame.rightVector*-15
			end

			local rc = raycast(char,char.HumanoidRootPart.Position,direction)

			if rc == nil then
				char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(direction)
			else
				char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(rc)
			end

the raycast function i have is basically the same as a raycast except it just makes sure it doesnt detect hitboxes and stuff. The arguments are (blacklist,origin,direction)

Try just doing

char.HumanoidRootPart.CFrame = (char.HumanoidRootPart.CFrame-char.HumanoidRootPart.Position) * CFrame.new(rc)

If you want to preserve the rotation

1 Like

Nope I get the same problem unfortunately.

Oh I forgot, just switch the position of the two

char.HumanoidRootPart.CFrame = CFrame.new(rc) * (char.HumanoidRootPart.CFrame-char.HumanoidRootPart.Position)

That should work nicely

1 Like

It can still send u to random places / inside of walls. I will probably just make it not move you at all if you cannot travel the distance

If you wanted to try one last thing you could also just get the distance from the ray hit point and multiply the respective direction by that distance
Thatd be a simpler approach more similar to your current one

Also btw I just realized youre prob just casting in the wrong direction
Youre multiplying the world space lookvector by the cframe of the hrp which causes it to be applied locally to the hrp resulting in a totally different direction and therefore wall-age

Id fix this issue by simply adding on the vector3 direction to the hrp cframe, not multiplying it

1 Like

doing char.HumanoidRootPart.CFrame + direction or char.HumanoidRootPart.CFrame + rc actually stops me from being able to go inside of walls. However it still teleports me far away I just cant actually get trapped inside the wall now which is an improvement I suppose.

The slight edit has to be kept for rc since its a position instead of a direction
Its very slightly simpler now though cause you dont have to worry about a bunch of crazy direction stuff

I believe changing it to:

char.HumanoidRootPart.CFrame - char.HumanoidRootPart.Position + rc

Would do the trick

4 Likes

works perfectly! Thank you so much for helping. Greatly appreciated!

1 Like