checking when a game object is in an area

hi,i want to check when a player is inside an area. This is my script.Help me!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class deathtime : MonoBehaviour {

    private int health;
    public Text healthText;
    // Use this for initialization
    void Start () {
        health = 1000;
    }
   
    // Update is called once per frame
    void Update () {
        healthText.text = "Health: " + health;
        //if game object is inside the area
            health = health - 1;
    }
}

what is line 17!!

please help

“inside an area”… can you expound on that?

There are several ways:

  • Create a gameObject and assign it a collider. Make that collider a trigger and use “OnTriggerEnter” in your methods.
  • Create a bounding box from script via the bounds class and assign it a size. Check with that bounds object whether a point is inside of that bounding box (Bounds.Contains()).

There are more approaches which might be better suited. That’s hard to say though since you were not really specific on the “area” as LeftyRighty said.

1 Like

Maybe you can check the distance between the vector3 of the object and the vector3 of the area ? and if it’s distance is less than 3 for example then the player is inside the area ?

1 Like

with inside an area i mean that it is between 2 Vector3s for x,2 for z and 2 for y

then try

// at the top of the Script put:
public GameObject PlayerObject;

//now put this function anywhere in the script

bool GetPlayerInsideArea(float Distance)
{
   bool PlayerInArea = false;
   if(!PlayerObject)
   {
    PlayerObject = GameObject.FindGameObjectWithTag("Player");
   }
   if(Vector3.Distance(PlayerObject.transform.position,new Vector3(3,2,2)) < Distance || Vector3.Distance(PlayerObject.transform.position,new Vector3(3,2,2)) == Distance)
  {
   PlayerInArea = true;
  }
  return PlayerInArea;
}

Now you can call it in update like that:

    void Update () {
       if(GetPlayerInsideArea(3f))
      {
        healthText.text = "Health: " + health;
        //if game object is inside the area
            health = health - 1;
      }
    }

you can change 3 to the maximum distance the player allowed to go far…

oh,i allready thought of a script by my self, it works perfectly…

    private int health;
    public Text healthText;
    public Text DangerText;
    private float playerx;
    private float playerz;



    // Use this for initialization
    void Start () {

        health = 1000;
        DangerText.text = "";

    }



    // Update is called once per frame
    void Update () {

        playerx = GameObject.FindGameObjectWithTag ("player").transform.position.x;
        playerz = GameObject.FindGameObjectWithTag ("player").transform.position.z;
        healthText.text = "Health: " + health;
        DangerText.text = "";

        if (playerx <= 75) {

            if (playerx >= -10) {

                if (playerz >= 10) {

                    if (playerz <= 450) {

                        health = health - 1;
                        DangerText.text = "DANGER";

                    }

                }

            }

        }


        if (health <= 0) {

            healthText.text = "DEAD!!!";
            Time.timeScale = 0;

        }


    }



}

Usually I’d consider 4 nested if statements far from perfect. :wink:

i know but at least it works perfectly well

1 Like

Optimizationʕ•́ᴥ•̀ʔっ

public GameObject characterObject;
public GameObject areaObject;
public float areaLength = 0; // distance between characterObject and areaObject

bool GetEnemyInsideArea() 
    {
        if (Vector3.Distance(player.transform.position, areaObject.transform.position) <= areaLength) // if distance between areaObject and character equal or less return true otherwise false 
            return true; 
        return false;
    }
1 Like