- Home /
Problem with OnTriggerEnter
I want to make a gravity changer with trigger enter. This is the code;
void OnTriggerEnter (Collider other) {
if (GetComponent<Collider>().tag == "Player") {
gravity.x = 0;
gravity.y = -10;
gravity.z = 0;
its working well but when the player went out of the collider, it continues forever. I want to disable it when the player is out. I tried also this;
void OnTriggerStay (Collider other) {
if (GetComponent<Collider>().tag == "Player") {
gravity.x = -10;
gravity.y = 0;
gravity.z = 0;
}
}
void OntriggerExit(Collider other){
if (GetComponent<Collider> ().tag == "Player") {
gravity.x = 0;
gravity.y = -10;
gravity.z = 0;
but result is same. So can you help me to make what i want to do?
well it seems there's no change because you're assigning values to your gravity vector constantly. Remove your code inside the staytrigger? That code will obviously execute as long as your player is within the colliders bounds, just so you know, in case you didn't. Additionally you are assigning the same values for when you enter the collider and exit it. Perhaps set gravity.y to 0 on exit? Hope that helps some what.
Answer by TestifyNow · Oct 22, 2017 at 09:42 AM
You should set it to the desired value inside 'OnTriggerEnter', and set it back to normal in 'OnTriggerExit'.
Thanks i realised i used 'if' after triggerexit, i dont know why :).
This code works.
if (GetComponent().tag == "Player") {
gravity.x = 10;
gravity.y = 0;
gravity.z = 0;
}
}
void OnTriggerExit (Collider other) {
gravity.x = 0;
gravity.y = -10;
gravity.z = 0;
Your answer
![](https://koobas.hobune.stream/wayback/20220612141548im_/https://answers.unity.com/themes/thub/images/avi.jpg)