- Home /
Collision vs Trigger [Solved]
Going back to square one... again.
Scenario : I am trying to create a maze. At the end of the maze I made a cube that has a box collider on it. Mesh renderer is not checked. It is sized to the area I want it.
Goal : I'd like to have my character enter the area for the game to end (Called finalOut in hierarchy). Close out on it's own but before it does the words "Congratulations" appear on the screen.
Problem : I've tried collision detection vs triggers. Created layers named the one Finalout attached it to the gameobject finalOut, one named player (attached to player). The basic FPS controls are being used (CharacterMotor, FPSInputController, ETC.) Still nothing works.
Question : For what I'd like to happen should I be using a trigger or just collision?
EDIT Script : The script named Trigger_Controller
public var gameControl : Game_Controller //script that is attached to my player
function OnTriggerEnter ( Other : Collider )
{
if(other.transform.gameObject.layer==9)
//layer nine is named finalOut and on my finalOut game object
//finalOut
gameControl.finishedTheGame ();
//defined in other script
}
Then this script ^^^ is attached to my GameObject (finalOut)
Thoughts : I'm thinking something to do with the script is wrong. The question is what?
If need be I will post the other one.
Answer by BluEye · Sep 13, 2012 at 02:12 AM
function OnGUI ( )
{
GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity,
new Vector3(Screen.width / 1024f, Screen.height / 768f, 1)); //My screen size
if ( GameEnds )
{
GUI.Label(Rect (350,350,350,350), "CONGRATULATIONS! GAME OVER");//Centered on the screen
}
}
private var GameEnds : boolean;
function OnTriggerEnter (other : Collider) {
if(other.transform.gameObject.layer==8){ //I had to change to the layer 8 = player
GameEnds = true;
yield WaitForSeconds(3);
Application.Quit (); //nothing happens with this (I think it's b.c. I'm testing in Unity)
}
}
//attached this whole thing to my GameObject and it worked
//Just need to increase font size and maybe a colour and I'm good
THANKS EVERYONE FOR YOUR HELP. NOW ON TO THE NEXT PART OF MY GAME
Answer by vagos21 · Sep 12, 2012 at 09:53 PM
Since you want to make the player be able to walk into the invisible box, you'll use it as a trigger.
Then on your box you'd attach a script that includes this (javascript):
function OnTriggerEnter (other : Collider) {
if(other.name == "player"){ //this is actually the name of your player GameObject, you can use a tag or layer instead too
Debug.Log("Congratulations!");//will print it on the console, but you can change it for a screen message
yield WaitForSeconds(3); //wait for 3 seconds
Application.LoadLevel ("Level2"); //load level 2 supposing you just finished level 1
}
}
Answer by PProductions · Sep 12, 2012 at 02:47 PM
I would use OnTriggerEnter as this means that your character wont actually collide with the box, you could use the Collider parameter to check to see if it really is the player which collided. However in this case I don't think there is much difference.
That is what I thought also. Which is why I was trying the OnTriggerEnter. I did an update to my question to include the script I'm using.
Try adding a Debug.Log statement into OnTriggerEnter to see when it is called.
so would it be something like
Debug.Log ("Congratulations", finalOut (or Finalout from the layer ));
Debug.Log ("$$anonymous$$essage", "GameObject Name");
also would the Debug.Log go right before the end of the brackets or does it not matter?
Just use: Debug.Log("Endgame triggered");
and look for the message in the console when you next test the game.
Yes, "Endgame Triggered" is just a message: you could call it "I love ice cream" it makes no difference to the code. it's just a heads up to see which parts of your code are running.
Answer by hoffmanuel · Sep 12, 2012 at 09:53 PM
You could try to use raycasts with a distance. It holds possibilities to compare tags, ...
Answer by Owen-Reynolds · Sep 12, 2012 at 06:03 PM
So, the script is on the exitTrigger and checks to see if it gets hit by another exitTrigger?:
if(other.transform.gameObject.layer==9)
//layer nine is named finalOut and on my finalOut game object
So it isn't actually checking if it was hit by the player?
You could move the `OnTrigger` to the player, and it should work (the player doesn't need a trigger -- OTE also works if you hit a trigger.)
Or, if only the player is moving, you could skip the ifs (if something hits exitTrigger, it must be the player.) More common is to check tags. Tag the player as "Player" and:
Debug.Log("trigger A");
if(other.transform.CompareTag("Player")) {
Debug.Log("Trigger B");
// do end round stuff
}
The actual trigger script function OnTriggerEnter is attached to the GameObject.
Then Game_Controller is another script that is called at the beginning of that one. (Starting to think this wasn't a brilliant idea).
"So it isn't actually checking if it was hit by the player?" That is probably the problem as to why when I hit the GameObject nothing happens. :D
The question is. How do I fix that.
I am going to try combining and reducing the two scripts tonite to just the absolute basic of what I want.
Hit the GameObject. Box comes up, says "Congratulations" then game closes. (Seems simple enough)