Help with raycasting

ask you to help me, I need to make this script work with tweenservice but I don’t know how to properly combine raycast with tweenservice

need more info, what do you want to tween?

i need a bullet tweenservice, you look Raycast script I need to make this bullet to fly with the help of the tween service and do it through raycasting so that it flies to the position of the mouse(because if i just do a raycast, it will be buggy)

What you mean?

If you want to use a tween for that you just need to place in the Goal argument of the Tween a CFrame or Position coordinates of where the Raycast did hit. And play the tween with that data.

Thanks, I didn’t understand how to do it logically.

1 Like

Check the Raycast documentation, it will show you what stuff is inside the Result variable gotten from the Raycast:

local rayOrigin = Vector3.new(0, 0, 0)
local rayDirection = Vector3.new(0, -100, 0)

local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

if raycastResult then
	print("Instance:", raycastResult.Instance)
	print("Position:", raycastResult.Position) -- Here you have the coordinates of where the ray did hit
	print("Distance:", raycastResult.Distance)
	print("Material:", raycastResult.Material)
	print("Normal:", raycastResult.Normal)
else
	warn("No raycast result!")
end

Then on you tween use that coordinate

local TS = game:GetService("TweenService")
TS:Create(bullet, Info, {Position = raycastResult.Position}):Play()

Im not saying using tweens for this is the best approach, you could try other methods too if you are not sure about the results

1 Like

I think combining raycast and tweenservice is the best option for my framework, anyway thank you very much!

1 Like

image
i have a error(its dont read the result position)

my script:

image
No comments…

Read the Raycast documentation, the output says it all “index nil with Position”, the result is nil(empty), its logical if the ray doesnt hit anything then theres no data in the result variable. You should handle that, if theres no hit then dont try to use coodinates that doesnt exist for the tween.
Instead maybe you will need to get the look vector of the shoot and extend it, and use that coordinate for your tween, or extend the length of the ray to make sure it collides something like a wall

1 Like

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