- Home /
Trigger gravity by external collider
I'm using the following script to enable gravity when I walk into a certain gameobject with my player (CardboardMain). With this script gravity is enabled when I walk into the object itself, but I want gravity to be triggered by an external collider. How do I do this?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TriggerGravity : MonoBehaviour {
// Use this for initialization
void OnTriggerEnter(Collider other)
{
if (other.tag == "CardboardMain")
{
GetComponent<Rigidbody>().useGravity = true;
GetComponent<Rigidbody>().isKinematic = false;
}
}
}
Answer by MrMatthias · Mar 16, 2018 at 10:51 AM
Add a field and assign the rigidbody that should get gravity
[SerializeField] new Rigidbody rigidbody;
void OnTriggerEnter(Collider other)
{
if (other.tag == "CardboardMain")
{
rigidbody.useGravity = true;
rigidbody.isKinematic = false;
}
}
Answer by geerttimmerman1 · Mar 17, 2018 at 12:12 PM
It still doesn't work. I attached the script to a collider and I selected IsTrigger. I attached a mesh with a RigidBody to the script, but when I walk into the collider, the RigidBody doesn't enables gravity. Anyone any idea why it doesn't work?
Your answer
Follow this Question
Related Questions
Trigger with Gravity + Collision 2 Answers
How do I get my water to work?,How do I slow my descent in water? 0 Answers
Collision detection for low relative velocity, high world velocity rigidbodies 2 Answers
How to call OnTriggerEnter once 5 Answers
Stacking mechanic (Parenting Prefab to another prefab on collision) 2 Answers