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

How to make Vehicle Turning correctly?

Asked by 5 years ago

I'm trying to make scripted car but I have trouble scripting Vehicle Turning. For example this is how I handle Turning Right

local UIS = game:GetService("UserInputService")
local part = script.Parent
--This are Three things I use For moving and Turning
local thruster = part.BodyThrust
local gyro = part.BodyGyro
local rotate = part.BodyAngularVelocity
-------
local on_move = part.OnMove

local R = 60



game:GetService("RunService").Stepped:Connect(function()

    local x = part.Orientation.X 
    local y = part.Orientation.Y 

    local z = part.Orientation.Z
    --Resistance and Deaccelaration  
    part.Velocity = part.Velocity - part.Velocity/R



    local function onKeyPress(inputObject, gameProcessedEvent)


        --Forward
        if inputObject.KeyCode == Enum.KeyCode.T then
            on_move.Value = true
            direction = part.CFrame.LookVector


            gyro.CFrame = CFrame.Angles(direction.X/2,direction.Y/2,direction.Z/2)
        end
        --Backward
        if inputObject.KeyCode == Enum.keyCode.G then
            on_move.Value = true
            direction = part.CFrame.LookVector


            gyro.CFrame = CFrame.Angles(-(direction.X)/2,-(direction.Y)/2,-(direction.Z)/2)
        end
        --Turning Right
        if (inputObject.KeyCode == Enum.KeyCode.T and UIS:IsKeyDown(Enum.KeyCode.H)) or (inputObject.KeyCode == Enum.KeyCode.H and UIS:IsKeyDown(Enum.KeyCode.T)) then
            on_move.Value = true
            direction = part.CFrame.LookVector

            rotate.AngularVelocity = Vector3.new(0,-2,0)
            gyro.CFrame = CFrame.Angles(direction.X/2,direction.Y/2,direction.Z/2)

        end
        --Left
        if i(inputObject.KeyCode == Enum.KeyCode.T and UIS:IsKeyDown(Enum.KeyCode.F)) or (inputObject.KeyCode == Enum.KeyCode.F and UIS:IsKeyDown(Enum.KeyCode.T)) then
            on_move.Value = true
            direction = part.CFrame.LookVector
            rotate.AngularVelocity = Vector3.new(0,2,0)
            gyro.CFrame = CFrame.Angles(direction.X/2,direction.Y/2,direction.Z/2)

        end
    --




    end
-------Stop the  Car
    local function onKeyLetgo(inputObject, gameProcessedEvent)
        gyro.CFrame = CFrame.Angles(0,0,0)
        rotate.AngularVelocity = Vector3.new(0,0,0)
    end




    UIS.InputBegan:connect(onKeyPress)

    UIS.InputEnded:connect(onKeyLetgo)




end)

But it seems that Variable "Direction", Which is lookVector, isn't updated when BodyAngularVelocity Rotates The part. It updates when you Re-press keys.

0
Why are you declaring a local variable each time the Stepped event is fired? Also why are you creating a new RbxScriptConnection each time as well? You should move them outside of the event. It won't fix the problem but it doesn't make sense. User#19524 175 — 5y
0
If you mean X,Y,Z variables I just forgot to delete them, I don't use them anymore. I'm just experimenting with different ways to fix my problem. About RbxScriptConnections, It won't work otherwise, since I'm usig RunService, I the problem is that you can't access anything inside Runservice GooierApollo664 183 — 5y

Answer this question