Tweening to the end of a raycast/where it hits?

Basically, I want to get the position/CFrame of where the raycast ends even if its not hitting anything/where it hits. I’m using this to basically tween the humanoidrootpart as a dash ability to move the player forward til it stops at the wall instead of clipping right through.

Though of course I’ve searched and found nothing of this sort, just people saying you could tween bullets though I don’t know how that’ll transfer :thinking:

This is currently what I’m doing which is of course failing:

TS:Create(HRP, TweenInfo.new(0.3), {CFrame = CFrame.new(HRP.CFrame. HRP.CFrame.LookVector * DIST)}):Play()

Though of course if there’s a better way to make the player dash instead of tweening/sometimes consistent distances with changing the velocity on the humanoidrootpart then I’ll take up on the idea, thank you :+1:

1 Like

Firstly, you’ll need to create the ray - this’ll go in the direction of the HRP’s LookVector:

local MAX_DISTANCE = 300 --how far the tween will go
local ray = Ray.new(HRP.Position, HRP.CFrame.LookVector * MAX_DISTANCE)

Secondly, use FindPartOnRay to see where it ended - the second returned is the position of where it ended:

local part, position = workspace:FindPartOnRay(ray, HRP.Parent) --ignore the HRP's Parent's descendants

Finally, you can tween to this given position:

local tween = TS:Create(HRP, TweenInfo.new(0.3), {CFrame = CFrame.new(position)})

You may want to add some offset so they don’t go into the wall.

2 Likes

How would you keep the player’s orientation?

1 Like

You might be able to do something like: HRP.CFrame.Angles * CFrame.new(position)

Add the new CFrame to their current, then minus their Position:

local tween = TS:Create(HRP, TweenInfo.new(0.3), {CFrame = HRP.CFrame * CFrame.new(position) - HRP.Position})
1 Like