- Home /
OnTriggerStay and OnTriggerEnter won't react when the player enters them.
I have a script which checks if the player has entered the trigger and if it is then ask if he wants to buy ammo, the strange thing is - the trigger simply doesn't work while a similar method works for zombies.
Here is the code:
var Bought : boolean = false;
var LAAS : boolean = false;
function OnTiggerStay (collision : Collider) {
if(collision.tag == "Player") {
LAAS = true;
}
}
function OnTriggerExit (collision : Collider) {
if(collision.tag == "Player") {
LAAS = false;
}
}
function Update () {
if(LAAS == true) {
if(Input.GetKey(KeyCode.E)) {
}
}
}
function OnGUI () {
if (LAAS == true) {
GUI.Label(Rect (10, 50, 300, 20), "Press E to buy 1 mag for 20 points");
}
}
Answer by Karsnen_2 · Jan 24, 2013 at 02:37 PM
You have a trigger Enter & Exit.
Now how big is the trigger?
Now with your code, you have not signified the purpose to happen when the player hits the 'e' button. You might have to look into it. Now - Lets consider a scenario which I believe is more relavent to yours.
I am assuming that when the player is inside the collider (which is a trigger), the player has the ability buy stuff. Hence why not make the code like this.
function OnTriggerStay (collision : Collider) {
if(collision.tag == "Player")
{
if(Input.GetKey(KeyCode.E))]
{
// add your relevant code here.
}
}
}
Let me know, if it works.
Later.
This is a draft code. The trigger is larger than the player, it is huge. There is no action for E yet. I want it to show the player a prompt if he wants to buy a new weapon. If he presses E while the prompt is showing, score will be deducted and he will get a new gun. I know ho to make that - the problem is that it doesn't even detect that the player is there.
What do you mean when you say there is no action for 'E' yet?
To have a pop, what you have to do is create a button pop-up with a boolean value under the hood. You need to use the boolean value to trigger the event. after a collision, you have a pop up. This pop-up will send two events a) true b) false. If it is true the you buy the weapon.
So on a large view, you do not buy the weapon when you press 'e'. The weapon is bought by the event that is sent.
I will make the code, what I am concerned about that I used the exact same system as in the code for my other game and it works fine. I want to know why the trigger doesn't detect that the player is in it.
Coolracer: I think you might have to check this chart.
Check the chart at the bottom of the page.
http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html