AddForce and AI

Hi! I have some class, AIControl. Inside this class in Update method NPC calls diffirent methods depending on the environmental situation. For example, if distance between NPC and some target more than 10, NPC calls GoTo() method. And in GoTo() method some code that applies AddForce to NPC and rotates NPC to target. And, as you see, it’s called from Update method. So, i need to apply AddForce from FixedUpdate() and keep “call method” principle. But i don’t know how… of course, I can use boolean vars, but then i’d have also to turn off AddForce. For example, i write somehere mForward = true, then, somewhere mForward = false…

Example:

Update()
{
if(dist > 10f)   
GoTo(target);
else    
Attack(target);    
}    
GoTo(...) 
{    
NPC.AddForce(...);  
NPC.MoveRotation(...);  
}

Thanx!

Where is that?

1 Answer

1

So, looks like the only way is to use boolean vars…
Something like this:

Update()
    {
    if(...)
    {
    mForward = true;
    }
    }
    
    FixedUpdate()
    {
    if(mForward)
    {
    AddForce(...);
    mForward = false;
    }
    }

Must've been done on your behalf this time. Accepting / upvoting helps others find information; we're encouraged to encourage newcomers to pick up the habit.