Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How To Move A Part Smoothly At A Constant Speed?

Asked by 6 years ago
Edited 6 years ago

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.

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?) ThatPreston 354 — 6y
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 ;-; Hamrhed2 4 — 6y
0
raycasting hellmatic 1523 — 6y
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. xXGokyXx 46 — 6y

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
6 years ago

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
Ad

Answer this question