• PhysObj:SetVelocity and Entities
    1 replies, posted
So far I've been having an issue with applying force to an entity when it finds other entities, at this moment players, in a sphere around itself. My code so far: [CODE]function ENT:Think() local targets = ents.FindInSphere(self.Entity:GetPos(), 150) for _, target in pairs( targets ) do self.Entity:SetVelocity( Vector(1, 0, 0) ) end end[/CODE] I just put in the vector (1, 0, 0) for testing purposes, but I cannot get the entity to move in the direction when I come near, instead I get zero errors as to what I may be doing wrong, and I am most likely getting zero errors due to me missing something important. If someone could help me out that would be great. My purpose for doing this is to eventually create an entity that would act as a creature that will run away, or run towards you depending on it's standing; i.e: How happy/sad it is. [B]edit:[/B] Okay so I figured out what I was doing wrong while trying to apply force to the object, here is my now updated code: [CODE]function ENT:Think() local targets = ents.FindInSphere(self.Entity:GetPos(), 150) if ( targets ) then self.Entity:GetPhysicsObject():ApplyForceCenter( VectorRand() * 100 ) end end[/CODE] My next question is this now; If I wanted to make it so my entity only moves once every 5 seconds in a random direction would I do something like the following? [CODE]local nextmove = CurTime() if ( nextmove > 5 ) then code here for movement end --I've tried this, but it still seems to be moving around, how would I check to see if I had last moved? When to move next? So on and so forth?[/CODE]
[QUOTE=Apple_Bloom;45325076]My next question is this now; If I wanted to make it so my entity only moves once every 5 seconds in a random direction would I do something like the following? [CODE]local nextmove = CurTime() if ( nextmove > 5 ) then code here for movement end --I've tried this, but it still seems to be moving around, how would I check to see if I had last moved? When to move next? So on and so forth?[/CODE][/QUOTE] [lua] --when you create the entity self.NextMoveTime = CurTime()+5 --don't start moving until 5 have seconds have passed (could just use CurTime() to move straight away) --In your entity think if CurTime()>self.NextMoveTime then --it's time to move --move and stuff --you could also add something like self.LastMoveTime = CurTime() here (if you need that information) --reset the time self.NextMoveTime = CurTime()+3 --move again in 3 seconds end [/lua]
Sorry, you need to Log In to post a reply to this thread.