Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How To Move A Part Smoothly At A Constant Speed?

I've been trying to make a simple projectile shot with a tool recently. I've tried using so many methods (although I don't think I fully understood any of them) and all of them somewhat worked, but never how I wanted it to. I want to move a part that I created with Instance.new("Part") to the Player Mouse.Hit.p vector3 position. I've tried bodyposition, bodyvelocity, rocketpropulsion, tweening, and lerping. All of these seem to not work the way I want them to. I want the object to move at a constant speed (regardless of distance) to the mouse position, then disappear. I've tried using the lookVector of the mouse.Hit but it seems to always move the part to a certain distance away from your player to the area you clicked, not to the object you clicked on, if that makes sense. I feel like I'm over complicating this, please tell me if I am. Any help is appreciated.

4 comments

0

As I didn't quite understand your explanation, I have a few questions before I post an answer. First, are you trying to make a projectile go towards a part and stop or do you want it to move infinitely towards where the player clicked? Second, can you provide an example of how you could utilize this? (What exactly is the purpose of your projectile?)

0

I'd like it to move infinitely and be removed after a set distance/time. I'm trying to make a projectile that can damage other players in a PVP setting, but I think I know how to do most of the damaging other players part. I just can't seem to get it to go forward correctly ;-;

0

raycasting

0

If you want it to move at a constant speed then why not just change the position and then make it get deleted when it hits something. If you want bullet drop you could make the y value slow get subtracted.

1 answer

If you want the movement to be perfectly smooth then you need to connect to RenderStepped, which fires every render frame and includes the time elapsed. Assuming filtering is enabled, a signal will need to be sent to the server to have it handle collisions and effects and replication, but rendering is handled by the client. Using RenderStepped to interpolate the part position is the smoothest way to move it.

local function Interpolate(part, destination, speed)
	local origin = part.CFrame
	local duration = (destination - origin.p).magnitude/speed
	local velocity = (destination - origin.p)/duration
	local elapsed = 0
	repeat
		part.CFrame = origin + velocity*elapsed
		elapsed = elapsed + game:GetService("RunService").RenderStepped:Wait()
	until elapsed > duration
end

This discussion is preserved as it was. New answers, comments, and votes are closed.