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

Enemy responds to player within a certain range

Discussion in '2D' started by JackAshwell, Nov 22, 2019.

  1. JackAshwell

    JackAshwell

    Joined:
    Oct 21, 2019
    Posts:
    24
    So I currently have this script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyMovement : MonoBehaviour {
    6.    
    7.     public Transform player;
    8.  
    9.     private Rigidbody2D rb;
    10.     private Vector2 movement;
    11.  
    12.     public float moveSpeed;
    13.  
    14.     void Start() {
    15.         rb = this.GetComponent<Rigidbody2D>();
    16.     }
    17.    
    18.     void Update() {
    19.         Vector3 direction = player.position - transform.position;
    20.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    21.         rb.rotation = angle;
    22.  
    23.         transform.LookAt(player.position);
    24.         transform.Rotate(new Vector3(0,90,0),Space.Self);
    25.  
    26.  
    27.         direction.Normalize();
    28.         movement = direction;
    29.     }
    30.  
    31.     private void FixedUpdate() {
    32.         moveCharacter(movement);
    33.     }
    34.     void moveCharacter(Vector2 direction) {
    35.         rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    36.     }
    37. }
    But I want it so that if the player goes outside of a certain area say a box on a map, then the enemy stops moving. How could I do this?

    Thanks!
     

    Attached Files:

  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,
    Just one simple way to do this - you could have a trigger collider in your level, which tells the enemy to stop moving when player is not in the trigger.
     
  3. JackAshwell

    JackAshwell

    Joined:
    Oct 21, 2019
    Posts:
    24
    Yeah I have done that, but have come up with an error.

    Player script:
    Code (CSharp):
    1. public class Player : MonoBehaviour {
    2.      public bool isInSection1, isInSection2; //Initialises different sections boolean
    3.    
    4.      private void OnTriggerEnter2D(Collider2D collision) {
    5.          if (collision.gameObject.name == "Section 1") {
    6.              isInSection1 = true;
    7.              isInSection2 = false;
    8.              Debug.Log("section1");
    9.          }
    10.          else if (collision.gameObject.name=="Section 2") {
    11.              isInSection1 = false;
    12.              isInSection2 = true;
    13.              Debug.Log("section2");
    14.          }
    15.      }
    16. }
    EnemyMovement script:
    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class EnemyMovement : MonoBehaviour {
    5.    
    6.      bool Section1, Section2;
    7.      void Start() {
    8.          Section1 = Player.isInSection1;
    9.          Section1 = Player.isInSection2;
    10.      }
    11.        
    12.      void Update() {
    13.          Debug.Log(Section1);
    14.          Debug.Log(Section2);
    15.      }
    16. }
    But I keep getting this error:
    An object reference is required for the non-static field, method, or property 'Player.isInSection1' [Assembly-CSharp]
     
  4. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    You are trying to use Player as a variable, but really you need to make a variable of TYPE Player, and then use that.

    public Player player;

    player.OnTrigger...
     
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Here's an example of how you can achieve this. You want to pay attention to the PlayerDetection portion. You probably wont need the radar but ultimately the linecasting works the same way. If you are doing top down, then the radar may not be a bad way to go for you? It's just something I decided to do for fun. But check it out and hope it helps you :)