Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

C# error

Discussion in 'Scripting' started by Custardcs, Mar 2, 2016.

  1. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    Hey i am having a bit of an issue with my code :|
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy_AI : MonoBehaviour
    5. {
    6.     public float enemyLookDistance;
    7.     public float attackDistance;
    8.     public float enemyMoveSpeed;
    9.     public float enemyRotSpeed;
    10.  
    11.     //zombie health
    12.     public float ZomMaxHealth = 100;
    13.     public float ZomCurHealth = 100;
    14.  
    15.  
    16.     //brainPrefab.
    17.     public GameObject zombieBrain;
    18.  
    19.  
    20.     private Transform target;
    21.     private Renderer myRender;
    22.     private Animator anim;
    23.  
    24.  
    25.     void Awake()
    26.     {
    27.         anim = GetComponent<Animator> ();
    28.         myRender = GetComponent<Renderer> ();
    29.         ZomCurHealth = ZomMaxHealth;
    30.     }
    31.  
    32.     public void Damage(float amount)
    33.     {
    34.         ZomCurHealth -= amount;
    35.  
    36.         if (ZomCurHealth <= 0)
    37.         {
    38.             GameObject brains = GameObject.Instantiate<GameObject> (zombieBrain);
    39.             brains.transform.position = transform.position;
    40.             brains.transform.rotation = Quaternion.identity;
    41.             GameObject.Destroy(brains,20);
    42.  
    43.  
    44.             Destroy (gameObject);
    45.         }
    46.     }
    47.  
    48.     // Update is called once per frame
    49.     void Update ()
    50.     {
    51.         //make sure the player exists
    52.         if (target == null)
    53.         {
    54.             target = Player.GetTransform();
    55.  
    56.             if (target == null)
    57.             {
    58.                 //default to wander aimlessly if there is no player
    59.                 Wander();
    60.                 return;
    61.             }
    62.         }
    63.  
    64.         float fpsTargetDistance = Vector3.Distance (target.position, transform.position);
    65.  
    66.         if (fpsTargetDistance < enemyLookDistance)
    67.         {
    68.             Alerted();
    69.             return;
    70.         }
    71.  
    72.  
    73.         if (fpsTargetDistance < attackDistance)
    74.         {
    75.             ChasePlayer ();
    76.             return;
    77.         }
    78.  
    79.  
    80.         //default to Wander mode
    81.         Wander ();
    82.     }
    83.  
    84.     void Alerted()
    85.     {
    86.         anim.Play ("Walk1", 0);
    87.         myRender.material.color = Color.yellow;
    88.         Quaternion rotation = Quaternion.LookRotation (target.position - transform.position);
    89.         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * enemyRotSpeed);
    90.  
    91.     }
    92.     //chase the player
    93.     void ChasePlayer ()
    94.     {
    95.         anim.Play ("Run2", 0);
    96.         myRender.material.color = Color.red;
    97.         transform.position += transform.forward * enemyMoveSpeed * Time.deltaTime;
    98.  
    99.     }
    100.  
    101.     //wandering
    102.     void Wander ()
    103.     {
    104.         anim.Play ("Idle", 0);
    105.         myRender.material.color = Color.blue;
    106.         //Play The Animation
    107.     }
    108. }
    basically it does find me when in range it looks at me.. when i move out of range it wanders but when i get close it does not chase me or move towards me..
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If attackDistance is less than enemyLookDistance then your Alerted condition will always be true and return before ever checking the ChasePlayer condition. Easiest way to fix that would be to check attackDistance before enemyLookDistance.
     
  3. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    thanks, i swopped the to around and it works now :| lol