Facepunch / garrysmod-issues

Garry's Mod issue tracker
144 stars 56 forks source link

entity:SetVelocity() #5155

Open Mat40 opened 2 years ago

Mat40 commented 2 years ago

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
SnisnotAl commented 2 years ago

Have you tried SetVelocityInstantaneous?

Mat40 commented 2 years ago

yes

SnisnotAl commented 2 years ago

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

Mat40 commented 2 years ago

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

SnisnotAl commented 2 years ago

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)

Mat40 commented 2 years ago

okk thx i will try this

ZenkakuHiragana commented 2 years ago

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.