Raycast Blink(teleport) problems

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    The code fires a ray from the players humanoidrootpart to the players lookvector and if it hits something then it will update players HRP cframe to that result position else if it doesn’t hit anything then it will update HRP cframe to the full length
module.Blink = function(player) 
	local HRP = player.Character:FindFirstChild("HumanoidRootPart")
	local BlinkDebounce = player:FindFirstChild("Debounce")
	local AbilityCD = 5
	local BlinkDistance = 25
	
	if not BlinkDebounce then
		local Charges = Instance.new("BoolValue")
		Charges.Name = "Debounce"
		Charges.Parent = player
		
		local Rayorigin = HRP.Position
		local Raydirection = HRP.CFrame.LookVector * BlinkDistance
		local Raycastparams = RaycastParams.new()
		Raycastparams.FilterDescendantsInstances = {player.Character}
		Raycastparams.FilterType = Enum.RaycastFilterType.Exclude
		
		local Raycastresult = workspace:Raycast(Rayorigin, Raydirection, Raycastparams) 
		
		if not Raycastresult then
			HRP.CFrame = HRP.CFrame + HRP.CFrame.LookVector * 25
		else
			local raycasthitposition = Raycastresult.Position
			HRP.CFrame = CFrame.new(raycasthitposition - HRP.CFrame.LookVector * 2)
		end
	else
		task.delay(AbilityCD, function()
			BlinkDebounce:Destroy()
		end)
	end
end
  • How (specifically) do you want to improve the code?
    Currently the way it updates players cframe causes some issues with what direction the player is facing. I would have time when it turns the player 180* if the ray hits a wall or the player turning sideways without me moving the camera at all.

The way your else branch works when you do have a RayCastResult, doesn’t specify an angle for the new CFrame. You are only supplying a position. I would suggest that you use the CFrame constructor that accepts a “lookAt” position. new(position: Vector3, lookAt: Vector3)

You should try changing the following line like below:

HRP.CFrame = CFrame.new(raycasthitposition - HRP.CFrame.LookVector * 2, raycasthitposition)

I tried this really quick in studio and it seems to stop turning the character incorrectly.

1 Like

Just a quick tip I think this fits #help-and-feedback:scripting-support more, just so that your thingy doesn’t get taken down.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.