How would I turn these vectors into a raycast direction relative to the player

What I am trying to do is teleport the player 8 directionally based on which direction they’re moving using their keys pressed.

local W = UIS:IsKeyDown(Enum.KeyCode.W) 
local A = UIS:IsKeyDown(Enum.KeyCode.A) 
local S = UIS:IsKeyDown(Enum.KeyCode.S) 
local D = UIS:IsKeyDown(Enum.KeyCode.D) 
	
local direction = Vector3.new(0,0,0)
if W then direction += Vector3.new(0,0,-1) end	
if A then direction += Vector3.new(1,0,0) end
if S then direction += Vector3.new(0,0,1) end
if D then direction += Vector3.new(-1,0,0) end
	
direction = direction.Unit
	
local params = RaycastParams.new()
params.FilterDescendantsInstances = {char}
params.FilterType = Enum.RaycastFilterType.Exclude
	
local raycast = workspace:Spherecast((humrp.CFrame * CFrame.new(0,0,2)).Position, 2, direction *20, params)

The direction variable does work properly (for example holding W and A does give (1,0,-1) which is the correct direction), but I want to convert it into a raycast that will start from the player’s cframe to whatever the direction is, and I’m not sure how to go about doing that.

local W = UIS:IsKeyDown(Enum.KeyCode.W) 
local A = UIS:IsKeyDown(Enum.KeyCode.A) 
local S = UIS:IsKeyDown(Enum.KeyCode.S) 
local D = UIS:IsKeyDown(Enum.KeyCode.D) 
	
local direction = Vector3.new(0,0,0)
if W then direction = Vector3.new(0,0,-1) end	
if A then direction = Vector3.new(1,0,0) end
if S then direction = Vector3.new(0,0,1) end
if D then direction = Vector3.new(-1,0,0) end

local directionpos = humrp.CFrame * CFrame.new(direction)
local newdir = CFrame.new(humrp.Position,directionpos.Position).LookVector
	
local params = RaycastParams.new()
params.FilterDescendantsInstances = {char}
params.FilterType = Enum.RaycastFilterType.Exclude
	
local raycast = workspace:Spherecast((humrp.CFrame * CFrame.new(0,0,2)).Position, 2, newdir * 20, params)

maybe this’ll work

1 Like

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