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!
Answer by Nemonymous · Jul 16, 2016 at 07:22 AM
So, looks like the only way is to use boolean vars... Something like this:
Update()
{
if(...)
{
mForward = true;
}
}
FixedUpdate()
{
if(mForward)
{
AddForce(...);
mForward = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612084837im_/https://answers.unity.com/themes/thub/images/avi.jpg)