Locating an Object in Unity 3D

I am learning Unity 3D, slowly. I am trying to make a maze and I need an event to happen when they reach the finish area.

How to get the location of an object and check if it is in the target area? Using Javascript.

Thanks for any help!

+2


a source to share


3 answers


If your end area can be thought of as a field, you can add a BoxCollider to your scene around the finish area and set the property isTrigger

to true. You will then receive a callback OnTriggerEnter

on your entity when it enters the area, which you can later use to finish the level or whatever.



+4


a source


As Peter G points to Unity's answers :

If you have a rigid body, you can do Rigidbody.IsSleeping () to check if your rigid person was sleeping.

If you are using a Character Controller, then check that CharacterController.velocity == Vector3.zero;

Or you can manually save the Vector3 in each frame that remembers the last position. Sort of:



function Update () {
 curPos = position;
 if(curPos == lastPos) {
     print("Not moving");
 }
 lastPos = curPos;
}

      

+2


a source


Well, if you mean the finish area by target area, then you can do this in several ways.

you can use collides

in a simple way: (1) create a plane, create a tag named "finish" (2) then select the player, then click the dropdown "tag" in the inspector. find "finish" and select it. you will also need to add a collider to your game object.

then create new JavaScript and add this code to it

    function OnCollisionEnter(collision : Collision){
    if(collision.gameObject.tag == "finish"){ 

//"STOP GAMEOBJECT FROM MOVING"
}

}

      

or use this method

put this script on your player. this script is probably as god as using colliders for what you are doing

 var other : Transform;
    function Update ()
    {

    var dist = Vector3.Distance(other.position, transform.position);

    if (dist < 100)
    {
    //stop player movent here 
// move player to exact finish position over time 

    }

    }

      

0


a source







All Articles