• Entity movement is simply too fast
    1 replies, posted
I have coded an entity to move towards a target. The problem is that the entity moves at an extremely fast pace unless its mass is set to a really high number such as 5000. Here is the code I am working with, what is causing it or what could I do to limit how fast it moves? [CODE]function ENT:MoveToTargetRed(target) local t = ents.FindByClass("melon_blue") if t[1] != nil then local selfEntPos = self:GetPos() local entVector = (self:GetPos()- Vector(1,1,1)) - t[1]:GetPos() if selfEntPos:Distance( t[1]:GetPos() ) < 2000 then self:GetPhysicsObject( ):ApplyForceCenter( t[1]:GetPos() - entVector * math.random(0,100)) --Tried modifying the math.random but that only skews where the entity will move to. end end end[/CODE]
Not sure if you're trying to accomplish anything specific with your vectors, but you could simply just subtract entity's position from the target position to get your direction, multiply the direction by desired move speed, then plug it into ApplyForceCenter [code] local entVector = t[1]:GetPos() - self:GetPos(); entVector = entVector * moveSpeed; self:GetPhysicsObject():ApplyForceCenter(entVector); [/code] Play with moveSpeed to get your desired speed. This is just basic vector usage, I'm not sure how it'll translate into Source Engine, but might be worth a shot. You'll probably need to normalize the vector before multiplying it by move speed edit: [code] local moveSpeed = math.random(0,100); local moveDir = t[1]:GetPos() - self:GetPos(); moveDir:Normalize(); self:GetPhysicsObject():ApplyForceCenter(moveDir * moveSpeed); [/code]
Sorry, you need to Log In to post a reply to this thread.