How to make the characters rotation to face the wall(Raycast)

  1. What do you want to achieve?
    How can i move the player towards the “Climb” Part (is there a way to convert the raycast’s position to CFrame because i want to move the player closer to the wall using tweenservice ) and reset its rotation to face the wall that i found with raycast.

  2. What is the issue?
    just dont know how to change the position to cframe and reset the rotation of the player to face the wall.

  3. What solutions have you tried so far?
    i tried to use the rayresult’s position but then the character’s humanoid root part was going to the place without the rest of the body and ive been trying to find a solution for so long.

Here’s my code (btw only using a local script for now )
LocalScript:

--services
local CAS = game:GetService("ContextActionService")
local UIS=game:GetService("UserInputService")
local players=game:GetService("Players")
local TweenService=game:GetService("TweenService")
--end of services



--useful variables for later
local debounce = false
local playerfound = false
local hum = script.Parent.Humanoid 
local FREEZE = "freezeMovement"
local val=nil
local char = script.Parent
local pp =false
local dd = false
local deb = true
--end of useful variables for later

-- animation 
local humanoid = char:WaitForChild("Humanoid")
local vaultanim = script.Parent.vault
local vaultanimtrack = humanoid.Animator:LoadAnimation(vaultanim)
local vaultanim2 = script.Parent.vault2
local vaultanimtrack2 = humanoid.Animator:LoadAnimation(vaultanim2)
local vaultanim3 = script.Parent.vault3
local vaultanimtrack3 = humanoid.Animator:LoadAnimation(vaultanim3)
local Animate = script.Parent:WaitForChild("Animate")
local walkanim = Animate.walk.WalkAnim
local walkanimtrack = humanoid.Animator:LoadAnimation(walkanim)
--end of animation






function ended(input)
	if input.KeyCode==Enum.KeyCode.Space and dd == true and deb == true then
		dd = false
		deb = false
		
		
		

		deb = true
	end
end

UIS.InputEnded:Connect(ended)






local raycastDistance = 4
while wait() do
	
	
	local raycastParams = RaycastParams.new()
	raycastParams.IgnoreWater = true
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local rayResult = workspace:Raycast(char.HumanoidRootPart.Position, char.HumanoidRootPart.CFrame.LookVector * 3000, raycastParams)
	
	if rayResult then
		
		local position = rayResult.Position
		local distance = (char.HumanoidRootPart.Position - position).Magnitude
		if distance < raycastDistance then
			
		if rayResult.Instance.Name == "Climb"  then
			
			val = rayResult.Instance
			dd = true
			
				
			end
			end
	else
		game.Players.LocalPlayer.PlayerGui.found.Value = false
		dd = false
		print("notfound")
	
		end
		



	
end

(EDIT)


how to fix the rotation so it always faces the part

here’s my code




--services
local CAS = game:GetService("ContextActionService")
local UIS=game:GetService("UserInputService")
local players=game:GetService("Players")
local TweenService=game:GetService("TweenService")
--end of services



--useful variables for later
local debounce = false
local playerfound = false
local hum = script.Parent.Humanoid 
local FREEZE = "freezeMovement"
local val=nil
local pos=nil
local char = script.Parent
local pp =false
local dd = false
local deb = true
--end of useful variables for later

-- animation 
local humanoid = char:WaitForChild("Humanoid")
local vaultanim = script.Parent.vault
local vaultanimtrack = humanoid.Animator:LoadAnimation(vaultanim)
local vaultanim2 = script.Parent.vault2
local vaultanimtrack2 = humanoid.Animator:LoadAnimation(vaultanim2)
local vaultanim3 = script.Parent.vault3
local vaultanimtrack3 = humanoid.Animator:LoadAnimation(vaultanim3)
local Animate = script.Parent:WaitForChild("Animate")
local walkanim = Animate.walk.WalkAnim
local walkanimtrack = humanoid.Animator:LoadAnimation(walkanim)
--end of animation






function ended(input)
	if input.KeyCode==Enum.KeyCode.Space and dd == true and deb == true then
		dd = false
		deb = false
		
		local GoalCF = pos * CFrame.Angles(0,math.rad(180),0)
	
		local humrroot = char.PrimaryPart
		local huminf = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In)
		local humtop1 = TweenService:Create(humrroot, huminf, {
			CFrame = GoalCF * CFrame.new(-2,5,0)
		})
		char.HumanoidRootPart.Anchored = true
		humtop1:Play()
		
		wait(1)
		char.HumanoidRootPart.Anchored = false
		
		
		
		deb = true
	end
end

UIS.InputEnded:Connect(ended)






local raycastDistance = 10
while wait() do
	
	
	local raycastParams = RaycastParams.new()
	raycastParams.IgnoreWater = true
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local rayResult = workspace:Raycast(char.HumanoidRootPart.Position, char.HumanoidRootPart.CFrame.LookVector * 3000, raycastParams)
	
	if rayResult then
		
	--	print(rayResult2.Instance.Name)
		local position = rayResult.Position
		local distance = (char.HumanoidRootPart.Position - position).Magnitude
		if distance < raycastDistance then
			
		if rayResult.Instance.Name == "Climb"  then
				pos = CFrame.new(position,position + CFrame.new(char.HumanoidRootPart.Position, char.HumanoidRootPart.CFrame.LookVector).LookVector)
			val = rayResult.Instance
			dd = true
			
				
			end
			end
	else
		game.Players.LocalPlayer.PlayerGui.found.Value = false
		dd = false
		print("notfound")
	
		end
		



	
end
2 Likes

You can convert Vector3 positions to cframe like this

local position = rayResult.Position
local GoalCF = CFrame.new(position.X,position.Y,position.Z)
3 Likes

This might help out a bit.

3 Likes



--services
local CAS = game:GetService("ContextActionService")
local UIS=game:GetService("UserInputService")
local players=game:GetService("Players")
local TweenService=game:GetService("TweenService")
--end of services



--useful variables for later
local debounce = false
local playerfound = false
local hum = script.Parent.Humanoid 
local FREEZE = "freezeMovement"
local val=nil
local pos=nil
local char = script.Parent
local pp =false
local dd = false
local deb = true
--end of useful variables for later

-- animation 
local humanoid = char:WaitForChild("Humanoid")
local vaultanim = script.Parent.vault
local vaultanimtrack = humanoid.Animator:LoadAnimation(vaultanim)
local vaultanim2 = script.Parent.vault2
local vaultanimtrack2 = humanoid.Animator:LoadAnimation(vaultanim2)
local vaultanim3 = script.Parent.vault3
local vaultanimtrack3 = humanoid.Animator:LoadAnimation(vaultanim3)
local Animate = script.Parent:WaitForChild("Animate")
local walkanim = Animate.walk.WalkAnim
local walkanimtrack = humanoid.Animator:LoadAnimation(walkanim)
--end of animation






function ended(input)
	if input.KeyCode==Enum.KeyCode.Space and dd == true and deb == true then
		dd = false
		deb = false
		
		local GoalCF = CFrame.new(pos.X,pos.Y,pos.Z)
		
		local humrroot = char.PrimaryPart
		local huminf = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In)
		local humtop1 = TweenService:Create(humrroot, huminf, {
			CFrame = GoalCF * CFrame.new(-2,5,0)
		})
		
		humtop1:Play()
		
		
		
		
		
		deb = true
	end
end

UIS.InputEnded:Connect(ended)






local raycastDistance = 10
while wait() do
	
	
	local raycastParams = RaycastParams.new()
	raycastParams.IgnoreWater = true
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local rayResult = workspace:Raycast(char.HumanoidRootPart.Position, char.HumanoidRootPart.CFrame.LookVector * 3000, raycastParams)
	
	if rayResult then
		
	--	print(rayResult2.Instance.Name)
		local position = rayResult.Position
		local distance = (char.HumanoidRootPart.Position - position).Magnitude
		if distance < raycastDistance then
			
		if rayResult.Instance.Name == "Climb"  then
			pos = position
			val = rayResult.Instance
			dd = true
			
				
			end
			end
	else
		game.Players.LocalPlayer.PlayerGui.found.Value = false
		dd = false
		print("notfound")
	
		end
		



	
end

Ok so now how to make the characters rotation to face the wall

2 Likes

Try to use char.PrimaryPart.CFrame.lookVector = add here the part

3 Likes

thank you :smiley: but Ive already found a solution to fix it

1 Like

Alright, Did it take you a long time to fix it?

2 Likes

well, no it was kinda easy but i wasnt just thinking of it :smiley: !!!

1 Like

What was the solution???

1 Like

hello! sorry I have forgotten but I will find it and show you soon!

1 Like