Get an entity to move towards a player

I am stumped at the present moment. Perhaps some of you lua gods can help.

I am trying to get an entity (which is a SENT) to jump towards the player and keep jumping at the player.

I have the basic code to work out where the player is (Or at least I hope) and the radius of which the entity should be looking for a player, I can’t however get the prop to move towards the player.

I have tried ApplyForceOffset and SetVelocity and both have failed me. I get prop entitys flying all over the shop but never at players.



for k, v in pairs(player.GetHumans()) do
		if v:Alive() then
			local selfEntPos = phys:GetPos()
			phys:SetMass(50)
			local playerPos = v:GetPos()
			local entVector =  phys:GetPos() - v:GetPos()
			if selfEntPos:Distance( playerPos ) < 5000 then
				phys:ApplyForceOffset( Vector(0,0,100) * math.random(0,50), entVector  )   -- Apply a physics impulse to the physics object.
			end
		end
		
	end


Any help or pointers in the right direction would be greatly appreciated!

entVector is the direction to your entity. First you (can) normalize it which brings the x,y,z numbers down to below 1.0 , then you can multiply it by however much force you want.


phys:ApplyForceOffset( entVector:GetNormalized() * 5000 * phys:GetMass(), Im_Not_Sure_What_this_argument_is)

You multiply it by the entities mass so it should apply the same force to any sized entity. I’m not sure what the second argument of ApplyForceOffset is, I would try setting it to the entities current position.

I have fixed it!!!

I will leave this code here for anyone else who may find it useful!



for k, v in pairs(player.GetHumans()) do
        if v:Alive() then
            local selfEntPos = phys:GetPos() -- This is the entitiy
            phys:SetMass(50) --Give it a low mass so it can jump higher
            local playerPos = v:GetPos() -- get the players position
  --entVector is the position the entity will travel (at the player) the - 0,0,150 makes the prop move with a jump
            local entVector =  (phys:GetPos()- Vector(0,0,150)) - v:GetPos() 
            if selfEntPos:Distance( playerPos ) < 5000 then --If the player is less than 5000 units form the prop
--Force the prop to move at the player
            phys:ApplyForceCenter( playerPos - entVector * math.random(0,100) )
            
            end
        end
        
    end