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

How might I go about checking if a part is moving?

Asked by 3 years ago

So I am trying to build a racing game and I want players to have a first person perspective when in the car which is defined by a part that I am using as my 'camera'. Here is what I've done so far:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local cam = game.Workspace.CurrentCamera
local camPart = game.Workspace["Lamborghini Aventador-J"].Body.Camera

camPart.Entered.OnClientEvent:Connect(function() --camPart.Entered is a remote event
    repeat wait()
        cam.CameraType = Enum.CameraType.Scriptable
    until cam.CameraType == Enum.CameraType.Scriptable
    cam.CFrame = camPart.CFrame
end)

This does work, but the problem is that when we drive the car, the player's camera perspective is left behind and the car drives off with the camPart. I think a good way to solve this might be to detect when the camPart is in motion with the car and set the CFrame of cam to the camPart whenever the script thinks it's moving. I just don't know how I'd detect the camPart moving.

Any ideas?

0
get velocity sayer80 457 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

I have an alternative suggestion. Set the Camera's CFrame to the part using RunService, so your code will condense to this:

local RunServiceFunction = game:GetService("RunService").RenderStepped:Connect(function()
    Camera.CameraType = Enum.CameraType.Scriptable
    Camera.CFrame = Campart.CFrame
end)

You can do RunServiceFunction:Disconnect() when you want it to stop.

Also, I'm not sure if RenderStepped is working currently because Roblox is replacing it with PreRender (you'll just have to substitute RenderStepped with PreRender).

0
Awesome! Thanks! ABHax101 4 — 3y
Ad
Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
3 years ago

Oh this is easy you can change if you want I used a little example

if script.Parent.Position.Magnitude > 0 then
    print("Changed")
end
0
Thanks for this, I will definitely be using this later on :D ABHax101 4 — 3y
0
np MattVSNNL 620 — 3y

Answer this question