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

Problem with Enemy detecting Player

Discussion in 'Scripting' started by Nizzo87, Jul 5, 2021.

  1. Nizzo87

    Nizzo87

    Joined:
    Dec 28, 2019
    Posts:
    7
    Hi all,

    I have a problem with my Enemies. They are still patrolling correctly as far as I can see. However, they have recently started shooting at the zero point of the environment. They also do this when I move my entire game, for example, on the X-axis for testing and - as seen in the picture - put an enemy there once. The enemy shoots into the empty space. I absolutely cannot explain this. At first I thought it was due to the Cinemachine, but that is ruled out. I put in some code too of the enemy script. you see my game on the right which I moved just to see where it comes from. enemydes.PNG Maybe you can help me.

    Thanks a lot and greetings, Nico.

    Code (CSharp):
    1. private void Start()
    2.     {
    3.         waitTime = starWaitTime;
    4.         player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    5.         randomSpot = Random.Range(0, moveSpots.Length);
    6.     }
    7.  
    8.     private void Update()
    9.     {
    10.         EnemyDrawRay();
    11.  
    12.         PatrolRouteEnemy();
    13.  
    14.         if (Vector2.Distance(transform.position, player.position) < shootingRange)
    15.         {
    16.             EnemyShootAtPlayer();
    17.             Debug.Log("Player: Enter Enter Collision - Shoot!");
    18.         }
    19.         if (Vector2.Distance(transform.position, player.position) >= shootingRange)
    20.         {
    21.             Debug.Log("Player out of range");
    22.         }
    23.  
    24.     }
    25.  
    26.     // ##### Draw Ray from Enemy to Player #####
    27.     void EnemyDrawRay()
    28.     {
    29.         Vector2 down = transform.TransformDirection(Vector2.down) * drawnRayLine;
    30.         Debug.DrawRay(transform.position, down, Color.green);
    31.     }
    32.  
    33.     // ##### Check if Player Enters or Exit the Enemies Trigger
    34.     private void OnTriggerEnter2D(Collider2D collision)
    35.     {
    36.         if (collision.gameObject.CompareTag("Player"))
    37.         {
    38.             if (enemyLookAtPlayer < 1.5f)
    39.             {
    40.                 Vector2 dir = this.transform.position - player.transform.position;
    41.                 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    42.                 this.transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
    43.                 Debug.Log("YES - Player Entered in Enemies Trigger!");
    44.             }
    45.         }
    46.     }
    47.  
    48.     private void OnTriggerExit2D(Collider2D collision)
    49.     {
    50.         if (collision.gameObject.CompareTag("Player"))
    51.         {
    52.             Vector2 dir = this.transform.position - moveSpots[randomSpot].position;
    53.             float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    54.             this.transform.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
    55.             Debug.Log("Player out of range");
    56.         }
    57.     }
    58.  
    59.  
    60.     // ##### Reduce Enemy health by one with each hit #####
    61.  
    62.     private void OnCollisionEnter2D(Collision2D collision)
    63.     {
    64.         if (collision.gameObject.CompareTag("Bullet"))
    65.         {
    66.             EnemyLives -= 1;
    67.             Debug.Log("Critical hit");
    68.         }
    69.     }
    70.  
    71.  
    72.     // ##### Enemy Patrol for x point (choose in Inspector) and defines the wait time between two points #####
    73.  
    74.     void PatrolRouteEnemy()
    75.     {
    76.         transform.position = Vector2.MoveTowards(this.transform.position, moveSpots[randomSpot].position, speed * Time.deltaTime);
    77.  
    78.         if (Vector2.Distance(this.transform.position, moveSpots[randomSpot].position) < 0.2f)
    79.         {
    80.             if (waitTime <= 0)
    81.             {
    82.                 randomSpot = Random.Range(0, moveSpots.Length);
    83.                 waitTime = starWaitTime;
    84.             }
    85.             else
    86.             {
    87.                 waitTime -= Time.deltaTime;
    88.             }
    89.         }
    90.     }
    91.  
    92.     // ##### Enemy attacks player. Enemy keeps distance when player comes closer. #####
    93.  
    94.     public void EnemyShootAtPlayer()
    95.     {
    96.         if (Vector2.Distance(transform.position, player.position) > stoppigDistance)
    97.         {
    98.             transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    99.         }
    100.         else if (Vector2.Distance(transform.position, player.position) < stoppigDistance && Vector2.Distance(transform.position, player.position) > retreatDistance)
    101.         {
    102.             transform.position = this.transform.position;
    103.         }
    104.         else if (Vector2.Distance(transform.position, player.position) < retreatDistance)
    105.         {
    106.             transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
    107.         }
    108.  
    109.         if (timeBtwShots <= 0)
    110.         {
    111.             Instantiate(projectile, transform.position, Quaternion.identity);
    112.             timeBtwShots = startTimeBtwShots;
    113.         }
    114.         else
    115.         {
    116.             timeBtwShots -= Time.deltaTime;
    117.         }
    118.     }
     
  2. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    192
    I am just looking at the EnemyShootAtPlayer() method right now.
    Confusing Code breads errors which you dont understand.

    1) Buffer Vector2.Distance(transform.position, player.position) into a variable.
    2) Avoid the stacked if else cases
    3) transform.position = this.transform.position; <== This doesnt do anything
    4) I think your Projectile needs a a direction then you Instantiate it.
    You only give it the position where it starts but no rotation.
    Quaternion.identity means its not rotated at all.
    5) BTW the methods is doing 2 things also a no no.
    One is moving the AiCharacter and the other is shooting.
    The shooting Part should probably be in its on MonoBehaviour,
    that way you can add this ability whatever you want.
     
    Last edited: Jul 5, 2021
  3. Nizzo87

    Nizzo87

    Joined:
    Dec 28, 2019
    Posts:
    7
    Hi TheFunnySide,
    Thank you for your feedback.
    There is a lot I can optimize yes. I would also like to do this when I have found the cause. I have not been doing this for so long. I think the shooting is actually not the problem here but has only made me aware of it. The enemies normally point to me when I enter the OnTriggerEnter2D area. But now they are pointing at the zero point. So that means they are already oriented wrong in general. I have also already deleted the cameras but it didnt help.

    Greets, Nico