Search Unity

Enemy is chasing me sideways?

Discussion in 'Scripting' started by ecloev, Oct 3, 2015.

  1. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    @McMayhem
    Hello,
    Does anyone know why my enemy is chasing me sideways instead of forward he is literally running after me with his eyes to the left? Here's his AI :) Thanks!
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AdvancedAI : MonoBehaviour
    6. {
    7.     float distance;
    8.    
    9.     public float lookAtDistance = 25.0F;
    10.     public float chaseRange = 2050.0F;
    11.     public float attackRange =4.5F ;
    12.     public float moveSpeed = 5.0F;
    13.     public float damping = 6.0F;
    14.     public float gravity =20.0F;
    15.     public float damage = 40.0F;
    16.     public float attackRepeatTime = .8F;
    17.     public Transform target;
    18.     public CharacterController controller;
    19.    
    20.     private Vector3 moveDirection = Vector3.zero;
    21.     private float attackTime;
    22.  
    23.  
    24.  
    25.     //TODO: add in a function to find controller and to locate and assign the player as the target
    26.    
    27.     void Start()
    28.     {
    29.         attackTime = Time.time;
    30.     }
    31.    
    32.     void Update()
    33.     {
    34.         distance = Vector3.Distance(target.position, transform.position);
    35.        
    36.         if(distance < lookAtDistance)
    37.         {
    38.             LookAt();
    39.         }
    40.        
    41.         if(distance > lookAtDistance)
    42.         {
    43.        
    44.         }
    45.        
    46.         if (distance < attackRange)
    47.         {
    48.             AttackPlayer();
    49.         }
    50.        
    51.         else if (distance < chaseRange)
    52.         {
    53.             ChasePlayer();
    54.         }
    55.     }
    56.    
    57.     void LookAt()
    58.     {
    59.         Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
    60.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    61.     }
    62.    
    63.     void ChasePlayer()
    64.     {
    65.         moveDirection = transform.forward;
    66.         moveDirection *= moveSpeed;
    67.        
    68.         moveDirection.y -= gravity * Time.deltaTime; // doesn't seem to work right :(
    69.         controller.Move(moveDirection * Time.deltaTime);
    70.     }
    71.    
    72.     void AttackPlayer()
    73.     {
    74.         //TODO: Need Attack Animations
    75.         if (Time.time > attackTime)
    76.         {
    77.             target.SendMessage("damagePlayer", damage, SendMessageOptions.DontRequireReceiver);
    78.             attackTime = Time.time + attackRepeatTime;
    79.         }
    80.     }
    81.    
    82.     void ApplyDamage()
    83.     {
    84.         chaseRange += 30;
    85.         moveSpeed += 2;
    86.         lookAtDistance += 40;
    87.     }
    88. }
    89.  
    90.  
     
  2. McMayhem

    McMayhem

    Joined:
    Aug 24, 2011
    Posts:
    443
    Is it just his eyes that are off target, or the whole body? I don't see anything that might cause that kind of problem from this script. Though it's a bit difficult to know for sure since I don't know how the AI's GameObject is set up in your scene. Is the transform of your AI character (where the bones and mesh are) facing the same direction as it's parent game object? (I'm assuming here that you've set up a GameObject hierarchy where the actual model of the character is stored in a GameObject that houses all the main AI code.
     
  3. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    How can i tell?
     
  4. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    The code code works fine, like McMayhem says check your character model hierarchy and place the code in current place.
     
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Check the axis your model is aligned to, sounds like it isn't facing the correct way. I think forward is along the z-axis
     
  6. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Position: -0.8, 0, 5.79
    What's wrong with that?
    It's rotation IS 0, 0, 0?
     
  7. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    Your character is facing Z forward?
     
  8. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    If that's what that means then yes haha :p
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You need to look at your model in the editor, look at the axis arrows on the enemy & ensure that it is facing the z axis. It's rotation can be 0,0,0 & it faces the wrong way if it has been built or imported incorrectly.
     
  10. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    It is facing towards the X-axis how can i fix this?
     
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    It depends how you got the model. I've only imported a model once that came in on the wrong axis & the only way I could fix it was to go into blender (where I made the model), rotate it & then reimport it. I kept doing this until I got it into Unity on the correct rotation. Others may know of a better way to do this though.
     
  12. ecloev

    ecloev

    Joined:
    Oct 1, 2015
    Posts:
    101
    Well i have a ton of things attatched, so im not sure what to do? Should i wait to see if anyone else has ideas and then if not pm you?
     
  13. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I can't help beyond that, I just prototype with primitives or 2d sprites without animations or stuff as I'm not skilled enough to be working on anything that needs finished art. It's more important for me at this stage to get my stuff to play well first before I worry about how it looks. Sorry.