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

why isnt the velocity of this bodyvelocity not changing when the lookvector does?

Asked by 6 years ago

This is supposed to make a boat move in the direction that's facing, even if you turn while you're going forward. However, the lookvector of the boat's primarypart changes, but the velocity of the bodymover does not, despite being set to move based on the part's lookvector.

local re = game.ReplicatedStorage:WaitForChild("re")
local boat = script.Parent
local bounce = false


function goForward(move, speed)
    move.Parent = boat.basepart1
    move.MaxForce = Vector3.new(math.huge,0,math.huge)
    move.Velocity = speed
    boat.basepart1.Velocity = speed
    boat.stats.acceleration.Value = 1
    bounce = true
    while boat.stats.forward.Value == true do
        wait(0.5)
        if boat.stats.acceleration.Value <= 4.6 and boat.basepart1.Velocity.magnitude < 36 then
            move.Velocity = move.Velocity + (Vector3.new(boat.basepart1.CFrame.lookVector.x,boat.basepart1.CFrame.lookVector.y,boat.basepart1.CFrame.lookVector.z))*boat.stats.acceleration.Value
            boat.stats.acceleration.Value = boat.stats.acceleration.Value + 0.3
        end
    end
    bounce = false
    boat.stats.acceleration.Value = 1
    while boat.stats.forward.Value == false and boat.basepart1.Velocity.magnitude > 2.9 do
        wait(0.5)
        print(boat.basepart1.Velocity.magnitude," ",boat.stats.acceleration.Value)
        move.Velocity = move.Velocity - Vector3.new(boat.basepart1.CFrame.lookVector.x, 0, boat.basepart1.CFrame.lookVector.z)*boat.stats.acceleration.Value
        if boat.stats.acceleration.Value <=5 then
            boat.stats.acceleration.Value = boat.stats.acceleration.Value + 1
        end
    end
    if boat.basepart1.Velocity.magnitude < 4.6 then
    move.Velocity = Vector3.new(0,0,0)
    end
end


function turn(move, direction)
    if direction == "a" then
        while direction == "a" and boat.stats.turning.Value == true do
            wait()
            boat:SetPrimaryPartCFrame(boat.PrimaryPart.CFrame * CFrame.Angles(0,0.004,0))
        end
    elseif direction == "d" then
        while direction == "d" and boat.stats.turning.Value == true do
            wait()
            boat:SetPrimaryPartCFrame(boat.PrimaryPart.CFrame * CFrame.Angles(0,-0.004,0))
        end     
    end
end

re.OnServerEvent:connect(function(player, input, stroke)
    if boat.stats.driver.Value == "" then
        boat.stats.driver.Value = tostring(player)
    end
    if (tostring(player)) == boat.stats.driver.Value then
        if input and stroke then
            if input == "w" and stroke == "press" and bounce == false then
                bounce = true
                boat.stats.lastSpeed.Value = boat.basepart1.BodyVelocity.Velocity
                boat.stats.forward.Value = true
                goForward(boat.basepart1.BodyVelocity, boat.basepart1.BodyVelocity.Velocity)
            elseif input == "w" and stroke == "release" then
                boat.stats.lastSpeed.Value = boat.basepart1.BodyVelocity.Velocity
                boat.stats.forward.Value = false
            elseif input == "a" and stroke == "press" then
                boat.stats.turning.Value = true
                turn(boat.basepart1.turn, "a")
            elseif input == "d" and stroke == "press" then
                boat.stats.turning.Value = true
                turn(boat.basepart1.turn, "d")
            elseif input == "a" and stroke == "release" then
                boat.stats.turning.Value = false
                --stopTurn(boat.basepart1.turn)
            elseif input == "d" and stroke == "release" then
                boat.stats.turning.Value = false
                --stopTurn(boat.basepart1.turn)
            end
        end
    end
end)

The part that the problem occurs in is most likely the first while loop, since that's where the acceleration/forward movement of the boat occurs. The second while loop is only there to de-accelerate the boat after you've let go of the W key.

0
What exactly is "move" in the first loop? Bluemonkey132 194 — 6y
0
It's BodyVelocity that's a child of basepart1. vastqud12 20 — 6y
0
Sorry. I see from your original question that is a BodyVelocity. So maybe instead of a BodyVelocity you should use something like a RocketPropulsion. Its just that BodyVelocity is not the right thing to use unless you want to do a bunch of crazy math. Bluemonkey132 194 — 6y
0
Oh it's alright, I've already got it working with BodyVelocity. Now I have another problem where if you turn as you're de-accelerate, it de-accelerates all weird :( vastqud12 20 — 6y

Answer this question