Need help using MoveDirection to cast a ray

I’m trying to convert a script that I made that moves the player X studs in the direction chosen by HRP.LookVector to Humanoid.MoveDirection (because lookvector is very limiting on controls) , however I’ve hit a stump. I must admit I’m very inexpirenced with rays and cframes.

The issue is I’ve no idea where to go from where I am for the ray calculation. I can use MoveDirection calculate where to move the player based on CFrames alone, however I need to cast a ray from the player in thier MoveDirection so that I can know if there’s an obstacle in the way.

My current solution is faulty as hit detection is still calculated from HRP’s LookVector CFrame. I’ll include both the base script that uses CFrames alone and note my patchwork solution. The script is probably very problematic otherwise as well, this is the first of the sort I’ve written. Stole the ray function from DevWiki.

Old Script Below. (Does not use MoveDirection; uses LookVector. is also completely functional.)

local function doWeHit(range)
	local rayOrigin = root.Position
	local rayDestination = (root.CFrame *, 0, -range-ofb)).p
	local rayDirection = rayDestination - rayOrigin
	local raycastParams =
	raycastParams.FilterDescendantsInstances = {root.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	if raycastResult then
		local HitSpot = raycastResult.Position
		local distToObj = (HitSpot - rayOrigin).Magnitude
		return distToObj - ofb
		return range

local function DodgeRoll()
	local rollRange = 20
	local gohere = root.CFrame + (root.CFrame.LookVector *,0,doWeHit(rollRange))) -- Replacing root.CFrame.LookVector here with Humanoid.MovePosition puts me in the correct spot.
		local TweenRoll = TweenService:Create(root,*0.7, Enum.EasingStyle.Linear), {CFrame = gohere})

You will need to get the move direction and multiply it with root part’s vectors such as look vector or right vector;

root.CFrame.LookVector* (Humanoid.Move.Direction.Z) +root.CFrame.RightVector* (Humanoid.Move.Direction.X) 
1 Like

I appreciate the feedback, and the explaination helped me figure out the code for the hitdetection working properly. I made the replacement noted in my original script and rayDestination with (rayOrigin + (Humanoid.MoveDirection*30)) for 30 studs

However when trying to make the character face the direction they’re moving to I’m running into issues.

I have a very poor understanding of CFrames and rayCasting at the moment so you may have to overexplain it to get your point across. I’ve been expirementing with what you’ve given me however I’ve made no tangible progress.


The best way you can do it is to multiply it by the move direction such as;

(root.CFrame *  ).LokkVector -- you may need to switch it to position and not use lookvector

RayCasting uses two parameters to cast origin point and direction(root.CFrame.LookVector or ( (root.CFrame*,0,-4)) ).Position-root.CFrame.p).Unit ) You can cover position to direction by using .Unit such as position - direction,

1 Like

I have the rayCasting section completed, it now successfully returns collision detection now as per my previous post, however now I’ve the issue of rotating the character such that they face the direction of the position they’re moving to in the DodgeRoll() function.

local function DodgeRoll()
	local rollRange = 20
	local gohere = root.CFrame + (Humanoid.MoveDirection *,0,doWeHit(rollRange))) --MoveDirection* goes in front
		local TweenRoll = TweenService:Create(root,*0.7, Enum.EasingStyle.Linear), {CFrame = gohere})

Hit detection and the spot the character moves to are both correct, but the character continues to face the direction they were at the beginning of the animation.

I suspect I’d have to change the root.CFrame (in the gohere variable) to something else , though I may just be grossly misunderstanding the concept.

Edit: I can get the character to rotate, however it causes everything else to go incorrectly and it does not face me in the correct direction, so the suspicion was correct

Best way you can rotate the player and not affect anything is to use align orientation.(With a bodyMover)

But since you are also moving the root part when framing just use CFrame.lookAt() or local gohere = root.CFrame * (Humanoid.MoveDirection *,0,doWeHit(rollRange)))
You do not add a cframe with a cframe you are suppose to multiply it.

when I make the change from addition to multiplication, the error TweenService:Create property named 'CFrame' cannot be tweened due to type mismatch (property is a 'CoordinateFrame', but given type is 'Vector3') throws, meaning I’d have to change the Vector3 into a coordinate frame.

I’m currently expirementing with CFrame.lookAt, I may be able to fix it with that.

Just covert the other part to a cframe Humanoid.MoveDirection *,0,doWeHit(rollRange)) )

That conversion breaks the system such that the character is moved to the wrong location, and also doesn’t rotate during the animation. I’m still trying for alternative solutions regarding changing the root.CFrame keeping the + as that’s the only rotation I’ve gotten so far

By inserting:

root.CFrame =,,root.Position.Y,gohere.Position.Z))

after the tween:Play it causes the character to face the right direction. Albeit, they slowly face back towards their original facing angle, but it’s not terribly noticable.

Did you use a body mover and align orientation or the cframe.lookAt?

CFrame.lookAt. Took me a moment to get the hang of but using the piece of code shown in my previous post I was able to force HRP to look at the destination, however tweenservice draws from where youre facing at the time it’s called so there’s a little movement where you slowly rotate back to your original position, working on forcing that out with a counterbalance tween/better placement

If you want it to work well with physics you should try body-movers or the replacements for them.

For sure, I’ve been told of and witness to their superiority over the overglamoured teleport that my script is, but I’ve had so many frusterations working with them. I have no understanding of the proper use of them.

The biggest issue with them I found is the difference in friction between your character and the ground, and your character and the air. This caused my previous efforts to be launched extremely far when using the force to walk off of a ledge.

You can use bodyVelocity to have it work smoothly without being affected by friction.

I’ll begin work on it immidiately, haven’t touched/been recommended bodyvelocity yet but if it works then I may have a lot of rewriting to do – so far I’ve been using teleporting and tweening for rolling, attacking, dying, staggering, and climbing. youll have my eternal gratitude as well haha

Looks like it’s still being affected by friction ): this is my code so far, works perfect in the air but with next to no movement on the ground.

local function DodgeRoll()
	local rollRange = 20
	local gohere = root.CFrame + (Humanoid.MoveDirection *,0,doWeHit(rollRange))) --MoveDirection* goes in front
	root.CFrame =,,root.Position.Y,gohere.Position.Z))
	local moveme ="BodyVelocity") -- creates
	moveme.MaxForce =,4000,4000) -- default values
	moveme.P = 4000 -- default values
	moveme.Velocity = Humanoid.MoveDirection*25 -- gets a velocity = imput*25
	moveme.Parent = root --HRP
	Debris:AddItem(moveme,1) --Destroys after animation's time

You just got to change the Max Force or P(ower). Either higher or lower I suggest increasing the max force.