- Home /
changing gravity OnTriggerEnter
so basically all i want to do is when a rigidbody enters a triggercollider with the correct tag the gravity will change to how i want to change it. then once it exits the collider gravity will return to normal.
Answer by NEGATIVERAGDOLL · Apr 24, 2018 at 12:50 AM
Hello, are you wanting the gravity bool to be set on and off or are you wanting the mass to be changed? I will attach a script as an example in a minute.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExampleGravity : $$anonymous$$onoBehaviour {
public Rigidbody Rigid;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
Rigid.mass = 4; // This can be changed to whatever you want, this is if you want the mass changed;
Rigid.useGravity = false; // This is if you want to turn the gravity off;
}
void OnTriggerExit(Collider other)
{
Rigid.mass = 1; // Back to normal here using mass;
Rigid.useGravity = true; // This is if you want the gravity to be turned back on;
}
}
hey thanks man ya the reason i wanted to change gravity is cus ihave an up force script and when it was pulling objects up they were vibrating, so i was hoping that no gravity within the collider would prevent it from vibrating, and i think its worked! thanks man
hey man would you $$anonymous$$d hleping me answer another question? so if you go to my questions look at the one that says "pulling objects accross collider". i have a collider script that pulls objects accross it, depending on the axis i want it to pull objects across when it enters the triggercollider. but i want it so that wherever an object enters, it will just be pulled strait across rather than along a specific axis. im not sure how hard that is to figure out, but i couldent figure it out so. thanks again man!