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

Bug Object script won't update

Discussion in 'Scripting' started by euanross400, Feb 24, 2023.

  1. euanross400

    euanross400

    Joined:
    Jul 19, 2019
    Posts:
    3
    I've commented out some code because it was making my enemy look at the player in 3d instead of 2d but it still does it for some reason. I have no build errors in the script but maybe I missed something.

    Enemy script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     //Variables
    8.     public float speed;
    9.     public Transform target;
    10.     public Rigidbody2D rb;
    11.     public Rigidbody2D playerRb;
    12.     public float minimumDistance;
    13.     public PlayerMovement playerMoveScript;
    14.  
    15.     public float hp = 1;
    16.  
    17.     public Transform firePoint;
    18.     public GameObject bulletPrefab;
    19.  
    20.     public float bulletForce = 20f;
    21.     private bool fireDelay = false;
    22.  
    23.     public void TakeDamage(float damageAmount)
    24.     {
    25.         hp -= damageAmount;
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         //Enemy death
    31.         if (hp <= 0)
    32.         {
    33.             playerMoveScript.score++;
    34.             Destroy(gameObject);
    35.         }
    36.  
    37.         //Look at player
    38.         transform.LookAt(target);
    39.  
    40.         //Get distance from player
    41.         //var distance = Vector2.Distance (transform.position, player.transform.position);
    42.  
    43.         //If distance between enemy and the target is greater than minimumDistance move towards target
    44.         if (Vector2.Distance(transform.position, target.position) > minimumDistance)
    45.         {
    46.             transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    47.         }
    48.         else if(fireDelay == false)
    49.         {
    50.             Shoot();
    51.             fireDelay = true; //Set delay
    52.  
    53.             StartCoroutine(FireDelay()); //Start fire delay
    54.         }
    55.  
    56.         //Attack range
    57.         /*if (distance < 50f && fireDelay == 0)
    58.         {
    59.             Shoot();
    60.             fireDelay = 300; //Reset delay
    61.         }*/
    62.     }
    63.  
    64.     private void FixedUpdate()
    65.     {
    66.         //Look towards player (currently looks at the player in 3d making it disapear).
    67.         /*Vector2 lookDir = playerRb.position - rb.position;
    68.         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
    69.         rb.rotation = angle;*/
    70.     }
    71.  
    72.     void Shoot()
    73.     {
    74.         GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    75.         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    76.         rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    77.     }
    78.  
    79.     public IEnumerator FireDelay()
    80.     {
    81.         yield return new WaitForSeconds(1); //Delay for set wait time
    82.  
    83.         fireDelay = false;
    84.     }
    85. }
     
  2. euanross400

    euanross400

    Joined:
    Jul 19, 2019
    Posts:
    3
    Nm I fixed it by deleting it and adding it back into the scene.