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. Dismiss Notice

So how do I make an enemy ai to stop shooting at the player when a wall is blocking it's view?

Discussion in 'Scripting' started by the_Bad_Brad, Sep 19, 2016.

  1. the_Bad_Brad

    the_Bad_Brad

    Joined:
    Nov 2, 2014
    Posts:
    278
    Hello, lovely people, can you help me with this enemy ai JavaScript?

    Here is a video of the problem:





    Here is a code I found on the internet:

    I can adjust the detection distance but it won't be realistic on open ground.

    I tried Raycast detection method but how do I make the ai to shoot only when it hits the player collider?

    Code (JavaScript):
    1. var Player : Transform;
    2. var enemy : Transform;
    3. var IsAttacking : boolean = false;
    4. var Bullet : Rigidbody;
    5. var SpawnPoint : Transform;
    6. var Distance : Vector3;
    7. var DistanceFrom : float;
    8. var fireRate = 0.5;
    9. var nextFire = 0;
    10. function Update(){
    11. Attacking();
    12. // Calculate the distance between the player  the enemy
    13. Distance = (enemy.position - Player.position);
    14. Distance.y = 0;
    15. DistanceFrom = Distance.magnitude;
    16. Distance/=DistanceFrom;
    17. var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
    18.      
    19. // If the player is 20m away from the enemy, ATTACK!
    20. if(DistanceFrom<30 && (Physics.Raycast(transform.position, fwd, 100))){
    21.  
    22.      
    23.          
    24.          
    25.              
    26.                  // Make a path
    27.          
    28. IsAttacking = true;
    29. }
    30. else{
    31. IsAttacking = false;
    32. }
    33.  
    34. }
    35.  
    36.  
    37. function Attacking(){
    38. if(IsAttacking){
    39. // The enemy isn't blind so it should face the player
    40. enemy.LookAt(Player);
    41. //Shoot
    42. if(Time.time > nextFire){
    43. nextFire = Time.time + fireRate;
    44. var Shoot = Instantiate(Bullet,SpawnPoint.position,SpawnPoint.rotation);
    45. Shoot.AddForce(SpawnPoint.forward*1000);
    46. Destroy (Shoot,1f);
    47. }
    48. }
    49. }
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I'm not sure what script this code is attached to.
    It seems to only check one enemy versus the player but this should do the trick. Note: You need to put box colliders around all your walls. (or anything else that could block a bullet)
    This is C# code so it might be slightly off syntax wise from java
    Code (CSharp):
    1. if (DistanceFrom < 30)
    2. {
    3.         RaycastHit hitInfo;
    4.         Vector3 directionToPlayer = player.position-enemy.position;
    5.         if (Physics.Raycast(enemy.position,directionToPlayer,hitInfo,100))
    6.         {
    7.                    if (hitInfo.gameObject == player.gameObject)
    8.                                isAttacking = true;
    9.          }
    10. }
     
    the_Bad_Brad likes this.
  3. the_Bad_Brad

    the_Bad_Brad

    Joined:
    Nov 2, 2014
    Posts:
    278
    It was attacahed to enemy objects
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    So why this line:
    Code (CSharp):
    1. var enemy : Transform;
    Wouldn't you just be using this scripts transform:
    Code (CSharp):
    1. transform.position   where had enemy.position
    What variable is being dragged into enemy?
     
  5. the_Bad_Brad

    the_Bad_Brad

    Joined:
    Nov 2, 2014
    Posts:
    278
    It asks for a semicolon but there was already a semicolon lmao
     

    Attached Files:

  6. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Post the modified Update function and point out what line has the semicolon error if you can't figure it out. I bet its something above it thats wrong so the syntax part of the compiler is expecting a semi-colon in a place you woudln't expect one to be.
     
  7. the_Bad_Brad

    the_Bad_Brad

    Joined:
    Nov 2, 2014
    Posts:
    278
    Code (JavaScript):
    1. var Player : Transform;
    2. var enemy : Transform;
    3. var IsAttacking : boolean = false;
    4. var Bullet : Rigidbody;
    5. var SpawnPoint : Transform;
    6. var Distance : Vector3;
    7. var DistanceFrom : float;
    8. var fireRate = 0.5;
    9. var nextFire = 0;
    10. var quak : GameObject;
    11. function Update(){
    12.    
    13. Attacking();
    14. // Calculate the distance between the player  the enemy
    15. Distance = (enemy.position - Player.position);
    16. Distance.y = 0;
    17. DistanceFrom = Distance.magnitude;
    18. Distance/=DistanceFrom;
    19. // If the player is 20m away from the enemy, ATTACK!
    20. //PROBLEM
    21. if (DistanceFrom < 30)
    22. {
    23.         RaycastHit hitInfo;
    24.         Vector3 directionToPlayer = player.position-enemy.position;
    25.         if (Physics.Raycast(enemy.position,directionToPlayer,hitInfo,100))
    26.         {
    27.                    if (hitInfo.gameObject == player.gameObject)
    28.                                isAttacking = true;
    29.          }
    30.           //PROBLEM
    31. }
    32.  
    33. else{
    34. IsAttacking = false;
    35. }
    36. }
    37.  
    38.  
    39.  
    40.  
    41.  
    42.  
    43.  
    44.  
    45.  
    46. function Attacking(){
    47. if(IsAttacking){
    48. // The enemy isn't blind so it should face the player
    49. enemy.LookAt(Player);
    50. //Shoot
    51. if(Time.time > nextFire){
    52. nextFire = Time.time + fireRate;
    53. var muzz = Instantiate(quak,SpawnPoint.position,SpawnPoint.rotation);
    54. Destroy(muzz, 1f);
    55. var Shoot = Instantiate(Bullet,SpawnPoint.position,SpawnPoint.rotation);
    56. Shoot.AddForce(SpawnPoint.forward*1000);
    57. Destroy (Shoot,1f);
    58. }
    59. }
    60. }
     
  8. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Are these declarations ok?
    Code (CSharp):
    1. RaycastHit hitInfo;
    2. Vector3 directionToPlayer = player.position-enemy.position;
    all the variables up top are declared like this:
    Code (CSharp):
    1. var hitInfo : RaycastHit;
    2. var directionToPlayer : Vector3;
    3. directionToPlayer = player.position-enemy.position;