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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

EnemyAI not shooting (using Raycast)

Discussion in 'Scripting' started by Hidrem, Apr 17, 2018.

  1. Hidrem

    Hidrem

    Joined:
    Mar 12, 2017
    Posts:
    22
    Hello everyone,

    I would gladly use help from the forums at this point as I can't get this issue fixed by myself.

    My enemy AI is programmed to chase, patrol and shoot depending on if they see the player. If they don't they patrol between waypoints if they do they chase him AND this is the part I can't fix If they are in the range of the player they should theoretically shoot him. (shoot meaning using Raycast)

    Now Patrol and Chase work 100% but this doesn't... The only debug that shows up when in game is the first one ("DA ENEMY SHOT BREH BREH") which is outside of the if statement. Any help appreciated!

    Cheers!

    Here is the piece of script that is related to the shooting:
    Code (CSharp):
    1.  void Shoot()
    2.     {
    3.  
    4.         RaycastHit hit;
    5.         Debug.Log("DA ENEMY SHOT BREH BREH");
    6.         if (Physics.Raycast(transform.position, transform.forward, out hit, range))
    7.         {
    8.  
    9.             Debug.Log("something happened ");
    10.             Health target = hit.transform.GetComponent<Health>();
    11.             if (target != null)
    12.             {
    13.                 Debug.Log("player hit");
    14.                 target.TakeDamage(damage);
    15.             }
    16.             if (hit.rigidbody != null)
    17.             {
    18.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
    19.             }
    20.  
    21.             GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
    22.             Destroy(impactGO, 1f);
    23.  
    24.         }
    25.     }
    26.  
    27.         void ShootUpdate()
    28.     {
    29.         shotTimer -= Time.deltaTime;
    30.  
    31.         if (shotTimer <=0)
    32.         {
    33.             Shoot();
    34.             shotTimer = shotDelay;
    35.         }
    36.         if (!SeekPlayer() || Vector3.Distance(transform.position, agent.destination) > shootDistance)
    37.         {
    38.             enemyState = EnemyState.Chase;
    39.         }
    40.     }
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    A handy lil thing when you're raycasting or checking orientations is Debug.DrawRay. Used like this:

    Code (CSharp):
    1. Debug.DrawRay(transform.position, transform.forward * range)
    You can put that before the if statement to see in the editor where it's raycasting up to exactly. If it's missing you know it's facing the wrong way, if it's hitting and still not calling you need to check the layers that the objects are on, or make sure there's nothing invisible in between them.
     
    Hidrem likes this.
  3. Hidrem

    Hidrem

    Joined:
    Mar 12, 2017
    Posts:
    22
    Ok sweet It did help me and noticed that the raycast shoots from below the plane... So I added this little beauty
    Code (CSharp):
    1.  Debug.DrawRay(transform.position, transform.forward * range);
    2.         if (Physics.Raycast(transform.position + shootPointOffset, transform.forward, out hit, range))
    and raised the Y offset. Although the enemy does now hit the player and kills him. The raycast still shows coming from his feet or below his feet (hard to tell). Is that normal?
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You haven't moved the Debug drawn version up by the shootPointOffset like the actual raycast.