Reputation: 6800
I've been learning Unity 3D, slowly. I'm trying to make a maze, and need an event to occur when they reach the finish area.
How do I fetch object location and check if it's in the target area? Using Javascript.
Thanks for any help!
Upvotes: 2
Views: 1978
Reputation:
well if you by target area you mean the finish 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 . search for "finish" and select it . you will also need to add a collider to your gameobject.
then create a 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
place this script on your player . this script is probably just 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
}
}
Upvotes: 0
Reputation: 772
As Peter G states on Unity Answers:
If you have a rigidbody, then you can do Rigidbody.IsSleeping() to check if your rigidbody is sleeping
If you are using a Character Controller, then checking to see if CharacterController.velocity == Vector3.zero;
Or, you can manually save a Vector3 every frame that remembers the last position. Something like:
function Update () {
curPos = position;
if(curPos == lastPos) {
print("Not moving");
}
lastPos = curPos;
}
Upvotes: 2
Reputation: 31782
If your end area can be treated as a box, you could add a BoxCollider to your scene around the finish area and set its isTrigger
property to true. Then you'll get a callback to OnTriggerEnter
on your entity when it enters the area, which you can subsequently use to end the level or whatever.
Upvotes: 4