garrysmod-issues icon indicating copy to clipboard operation
garrysmod-issues copied to clipboard

entity:SetVelocity()

Open Mat40 opened this issue 3 years ago • 7 comments

Hello I noticed that when we setVelocity an entity we can not reset it. Indeed, my print(ent:GetVelocity()) output -1227 in x while I SetVelocity to 0 just before

function dropoffFlight(ent, dropPos)
if not IsValid(ent) then return end -- Prevent if 'ent' is not valid

local dropVehicle = ents.Create("lunasflightschool_atte")
if not IsValid(dropVehicle) then return end -- Prevent if 'dropVehicle' is not valid

local inFlight   = true
local dropped    = false
local timerName  = "inFlightTimer" .. math.random(0, 1000)
local moveVector = Vector(-10, 0, 0)
local stopVector = Vector(0, 0, 0)

originalAngle = dropVehicle:GetAngles()

print(dropPos)

timer.Create(timerName, FrameTime(), 0, function()

    if not IsValid(ent) then
        timer.Remove(timerName)
        return
    end

    if inFlight then

        --print( ent:GetPos():DistToSqr(dropPos) )

        if ent:GetPos():DistToSqr(dropPos) < 10000000 then
            inFlight = false
            dropped    = true
            print("test")
            ent:SetMoveType(MOVETYPE_NONE)
            ent:SetVelocity(stopVector)
            --ent:SetMoveType(MOVETYPE_FLY)
            print(ent:GetVelocity())

            timer.Remove(timerName)

            timer.Simple(5, function()
                if IsValid(ent) then removeEntity(ent) end
            end)
        end
        if not dropped then
            --print("test1")
            ent:SetVelocity(moveVector)
        end
    end
end)
end

Mat40 avatar Feb 02 '22 10:02 Mat40

Have you tried SetVelocityInstantaneous?

SnisnotAl avatar Mar 19 '22 22:03 SnisnotAl

yes

Mat40 avatar Mar 19 '22 23:03 Mat40

try ent:GetPhysicsObject():SetVelocity() and SetAngleVelocity too

SnisnotAl avatar Mar 19 '22 23:03 SnisnotAl

i have already try this but not working i have conclude that when can reset an entity velocity, it’s very strange i have ask on some big discord communautary and nobody understands why it’s not working

Mat40 avatar Mar 20 '22 10:03 Mat40

hi buddy! i think i may of found a fix for you lmk if its works!

ent:SetVelocity(stopVector)

change this line to these three lines, because setting a velocity of 0 apparently just adds 0 to the velocity, causing no change

local stopVector = ent:GetVelocity()
stopVector:Negate()
ent:SetVelocity(stopVector)

(also delete your declaration for stopVector at the top of your code)

SnisnotAl avatar Mar 21 '22 21:03 SnisnotAl

okk thx i will try this

Mat40 avatar Mar 22 '22 11:03 Mat40

Quote from Entity:SetVelocity - Garry's Mod Wiki

WARNING If applied to a player, this will actually ADD velocity, not set it.

I guess this might be related.

ZenkakuHiragana avatar Aug 01 '22 17:08 ZenkakuHiragana