- Home /
How to use OnTriggerEnter? (JS)
Ahoy,
This is a problem I have been dealing with for a while but never knew how to fix.
How do I get OnTriggerEnter to work?
In this script the player object has a rigidbody attached, so it should work, but absolutely no dice. I know it's kind of a noob question, but it's really bothering me since I need to finish this game for the GGJ 2015. The game is 2D, if that helps.
var facingRight : boolean = true;
var facingLeft : boolean = false;
var jumped : boolean = false;
var debug : boolean = false;
var ground : Collider2D;
function Update () {
if (Input.GetKey(KeyCode.D)) {
if (facingRight) {
transform.Translate (0.1, 0, 0);
facingRight = true;
facingLeft = false;
}
if (facingLeft) {
transform.Rotate (0, 180, 0);
facingRight = true;
}
}
if (Input.GetKey(KeyCode.A)) {
if (facingLeft) {
transform.Translate (0.1, 0, 0);
facingLeft = true;
facingRight = false;
}
if (facingRight) {
transform.Rotate (0, 180, 0);
facingLeft = true;
}
}
if (Input.GetKeyDown(KeyCode.Space)) {
if (jumped == false) {
rigidbody2D.velocity = Vector2(0,6);
jumped = true;
}
}
}
function OnTriggerStay (ground) {
jumped = false;
debug = true;
}
Thanks.
Answer by persijn · Jan 25, 2015 at 02:09 AM
Question seem a bit large. http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
It's triggered when you hit something with an other ridged body. Don't forget to added the type of variable to the triggered argument. Like 'ground : Collider' Dont declare a collider beforehand i'ts sendt with the event. So remove your declaration of ground. It's not needed.