- Home /
make enemy only follow along the X and Z axis
currently I have this script which makes my enemy move towards me and attack me when he in a surtain distance away from the player but when He gets that close first he stards to rotate like he is looking up to me and when I jump he even rotates like he is lying on the ground just to follow the player and I dont want that I only want the enemy to follow the player and rotate towards his X and Z axis
 var Target : Transform;
 var Speed : float;
 var Hit = 1;
 var HitDelay = 1;
 
 function Update () {
     var distance = Vector3.Distance(gameObject.transform.position, Target.transform.position);
     if(distance > 2) {
         GetComponent.<Animation>().Play("walk");    
         var Step = Speed * Time.deltaTime;
         var relativePos = Target.position - transform.position;
         var rotation = Quaternion.LookRotation(relativePos);
         transform.rotation = rotation;
         transform.position = Vector3.MoveTowards(transform.position, Target.position, Step);
         }
     else if(distance < 2) {
         Attack();
 
     }
 }
 
 function Attack() {
         GetComponent.<Animation>().Play("attack");
         yield WaitForSeconds(HitDelay);
         Target.transform.SendMessage("PlayerHitted", Hit, SendMessageOptions.DontRequireReceiver);
 
 }
I know that there is nothing like something.position.x/y/z when using vector3.MoveTowards and the rotation thing I used doesn't work either because it's the MoveTowards that makes him rotate the strange way it does I hope someone can help me thanks
Answer by Arshia001 · Jul 24, 2016 at 09:12 AM
Do it like this:
  function Update () {
      var distance = Vector3.Distance(gameObject.transform.position, Target.transform.position);
      if(distance > 2) {
          GetComponent.<Animation>().Play("walk");    
          var Step = Speed * Time.deltaTime;
          var TargetPos = Target.Position;
          TargetPos.y = transform.position.y;
          var relativePos = TargetPos - transform.position;
          var rotation = Quaternion.LookRotation(relativePos);
          transform.rotation = rotation;
          transform.position = Vector3.MoveTowards(transform.position, TargetPos, Step);
          }
      else if(distance < 2) {
          Attack();
  
      }
  }
 
yupp just found that out too thanks for answering though but I was just going to say that I fixed it lol
for anyone who wants to know what the good code is i'll mark this ome as best answer
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                