- Home /
Need help with basic AI script.
I have a very basic AI script in which if you were move your character close enough to the enemy, the enemy will follow you. That part works great. The problem comes once the enemy reaches you, he keeps trying to follow you and I want it so that he stops once he reaches you.
 void Update() 
     {    
         if(Vector3.Distance(myTransform.transform.position, target.position) <= 12)
         {
             isMoving = true;
             Quaternion targetRotation = Quaternion.LookRotation(target.position - myTransform.transform.position);
             targetRotation.x = 0;
             targetRotation.z = 0;
             myTransform.transform.rotation = targetRotation;
         
         if(isMoving == true)
         {
         myTransform.rigidbody.AddRelativeForce(Vector3.forward * moveSpeed, ForceMode.VelocityChange);
         }
     }
     else
         isMoving = false;
     
     if(Vector3.Distance(myTransform.transform.position, target.position) <= 1)
         isMoving = false;
 }
Answer by deltamish · Feb 23, 2013 at 07:46 AM
HI,Its a simple fix all you have to do is add this part
 float temomovespeed;
 
 //ommited other part
 void Start(){
 temomovespeed = moveSpeed;
 }
 
 void Update(){
 //ommited other part
 
  if(isMoving == true)
 {
 moveSpeed = temomovespeed;
 myTransform.rigidbody.AddRelativeForce(Vector3.forward * moveSpeed, ForceMode.VelocityChange);
 }
 }
 else
 isMoving = false;
  
 if(Vector3.Distance(myTransform.transform.position, target.position) <= 1)
 isMoving = false;
 
 
 if(!isMoving)
 moveSpeed = 0;
 }
That didn't work but I got it working by just adding
if(is$$anonymous$$oving == false) moveSpeed = 0; else moveSpeed = 1;
at the end of it. Thank you for your help.
Strange is$$anonymous$$oving==false is same as !is$$anonymous$$oving .It should work but why didnt it work .Note i created a new float called tempmovespeed because you could use it to change the speed from walk to run or from crouch to walk(Does the problem lie here).
(-->That didn't work) What problems are you facing ,what isnt working
Answer by spitu · Feb 23, 2013 at 09:08 AM
 void Update() 
 {    
     if(Vector3.Distance(myTransform.transform.position, target.position) <= 12)
     {
         isMoving = true;
         Quaternion targetRotation = Quaternion.LookRotation(target.position - myTransform.transform.position);
         targetRotation.x = 0;
         targetRotation.z = 0;
         myTransform.transform.rotation = targetRotation;
         
         if(isMoving == true)
             myTransform.rigidbody.AddRelativeForce(Vector3.forward * moveSpeed, ForceMode.VelocityChange);
     }
     else
         isMoving = false;
     
     if(Vector3.Distance(myTransform.transform.position, target.position) <= 2)
         isMoving = false;
         
     if(isMoving == false)
         moveSpeed = 0;
     else
         moveSpeed = 1;
 }
Your answer
 
 
             Follow this Question
Related Questions
SphereCastAll sees for miles 2 Answers
Too Close To The Trap and melee attacking enemys. 1 Answer
Adding Distance in javascript 2 Answers
Make Navmesh agent follow closest object with tag 0 Answers
Clicking a sprite to enable script 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                