- Home /
Raycast/Collider End Level
Hi there, So I am trying to use OnTriggerEnter Collider to end a game. Note that I am using Raycast for all my movements. So most of the end level methods do not work.I know I have to use something to do with Application.LoadLevel, but I am not sure how to write up this code. So how do I write up the code to end a game with raycasting on a tagged object?. This is my movement code so far.
private var insideObject:Transform;
private var okToMove:boolean;
private var showingMission:boolean;
function Start(){
insideObject=transform.Find("Capsule");
okToMove=true;
showingMission=false;
}
function Update () {
if(okToMove){
//Debug.Log(insideObject.rotation.eulerAngles);
var dwn = transform.TransformDirection (Vector3.down);
var fwd = transform.TransformDirection (Vector3.forward);
var bk = transform.TransformDirection (Vector3.back);
var r = transform.TransformDirection (Vector3.right);
var l = transform.TransformDirection (Vector3.left);
var forwardPos:Vector3=transform.position + fwd;
var backwardPos:Vector3=transform.position + bk;
var rightPos:Vector3=transform.position + r;
var leftPos:Vector3=transform.position + l;
var hit:RaycastHit;
Debug.DrawRay(forwardPos,dwn,Color.green,5.0);
if (Input.GetKeyDown(KeyCode.UpArrow)) {
if (Physics.Raycast (transform.position, fwd, hit,1)) {
print ("There is something in front of the object!");
if(hit.collider.gameObject.tag!="Push"){
transform.position += Vector3.up;
}
else{
print ("Push!");
hit.collider.gameObject.transform.position += Vector3.forward;
transform.position += Vector3.forward;
}
}else{
Debug.Log("nothing in front");
if (Physics.Raycast (forwardPos,dwn, 11)) {
transform.position += Vector3.forward;
//did we move to an empty space ?
if (!Physics.Raycast (transform.position,dwn, .5)) {
okToMove=false;
InvokeRepeating("dropDown",1.0,1.0);
}
}
}
insideObject.rotation.eulerAngles.y=0;
}
if (Input.GetKeyDown(KeyCode.DownArrow)) {
if (Physics.Raycast (transform.position, bk, hit,1)) {
print ("There is something in front of the object!");
if(hit.collider.gameObject.tag!="Push"){
transform.position += Vector3.up;
}else{
print ("Push!");
hit.collider.gameObject.transform.position += Vector3.back;
transform.position += Vector3.back;
}
}else{
Debug.Log("nothing in front");
if (Physics.Raycast (backwardPos,dwn, 6)) {
transform.position += Vector3.back;
//did we move to an empty space ?
if (!Physics.Raycast (transform.position,dwn, .5)) {
okToMove=false;
InvokeRepeating("dropDown",1.0,1.0);
}
}
}
insideObject.rotation.eulerAngles.y=180;
}
if (Input.GetKeyDown(KeyCode.RightArrow)) {
if (Physics.Raycast (transform.position, r, hit,1)) {
print ("There is something in front of the object!");
if(hit.collider.gameObject.tag!="Push"){
transform.position += Vector3.up;
}else{
print ("Push!");
hit.collider.gameObject.transform.position += Vector3.right;
transform.position += Vector3.right;
}
}else{
Debug.Log("nothing in front");
if (Physics.Raycast (rightPos,dwn, 6)) {
transform.position += Vector3.right;
//did we move to an empty space ?
if (!Physics.Raycast (transform.position,dwn, .5)) {
okToMove=false;
InvokeRepeating("dropDown",1.0,1.0);
}
}
}
insideObject.rotation.eulerAngles.y=90;
}
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
if (Physics.Raycast (transform.position, l, hit,1)) {
print ("There is something in front of the object!");
if(hit.collider.gameObject.tag!="Push"){
transform.position += Vector3.up;
}else{
print ("Push!");
hit.collider.gameObject.transform.position += Vector3.left;
transform.position += Vector3.left;
}
}else{
Debug.Log("nothing in front");
if (Physics.Raycast (leftPos,dwn, 6)) {
transform.position += Vector3.left;
//did we move to an empty space ?
if (!Physics.Raycast (transform.position,dwn, .5)) {
okToMove=false;
InvokeRepeating("dropDown",1.0,1.0);
}
}
}
insideObject.rotation.eulerAngles.y=-90;
}
}
if (Physics.Raycast (transform.position,dwn,hit, .5)) {
if(hit.collider.gameObject.tag=="HasMission"){
Debug.Log("has mission");
if(!showingMission){
showingMission=true;
var t:Timer=GameObject.Find("Timer").GetComponent("Timer") as Timer;
t.startTimer();
}
}
}
}
function dropDown(){
transform.position += -Vector3.up;
var dwn = transform.TransformDirection (Vector3.down);
if (Physics.Raycast (transform.position,dwn, .5)) {
okToMove=true;
CancelInvoke();
}
}
Answer by aaronov · May 21, 2013 at 03:40 AM
I'm not really sure what you're asking. You seem to know how to use Raycast so why don't you do a Raycast from your object that represents ending the level to see if it hit the player, then simply do something like:
Application.LoadLevel(Application.loadedLevel+1);
Alternatively a better approach would be to set your "EndGame" object to be a trigger and do something like:
void OnTriggerEnter(Collider other) {
// Move to next level if player hit me
if (other.gameObject.tag == "Player")
Application.LoadLevel(Application.loadedLevel+1);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613102629im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I measure how far from the normal a raycast is when hitting a surface? 0 Answers
How can i make the ray cast don't go through the walls 1 Answer
How are raycasts actually implemented? 1 Answer
Want to find the point straight below the controller 1 Answer
enemy Raycast cone detection 0 Answers