- Home /
Apply gravity to parents
Hello, in the platform game I'm making I want to apply gravity to the platform if the player stands on it. At this moment I have 2 gameobjects. A and B. A is the visible platform and B is an invible trigger to check if the player hits the platform. B is also parent of A. I also added a rigidbody to object A and B and unchecked use gravity.
But when the player now hits the platform gameObject B falls down. This makes sence because the code is attached to B. I'd like to know how I can apply gravity to A wich is a child of B.
This is the code I attached to gameObject B :
function OnTriggerEnter (hit : Collider)
{
if(hit.gameObject.tag == "Player")
{
var MyObject = gameObject.GetComponent(Rigidbody);
MyObject.rigidbody.useGravity = true;
}
}
Answer by Eli-Davis · Jun 01, 2011 at 11:26 PM
var platformA:Transform;
function OnTriggerEnter (hit : Collider){
if(hit.gameObject.tag == "Player"){
var MyObject = gameObject.GetComponent(Rigidbody);
platformA.MyObject.rigidbody.usGravity = true;
}
}
It's untested. But I think it will work. Just drag the platform A object into the transform variable.
With a couple of adjustments this code works :D thank you very much
Your answer
![](https://koobas.hobune.stream/wayback/20220612093629im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to disable player gravity 2 Answers
Have child ignore parent's tag? 1 Answer
Walking on walls JS 2 Answers
When collider enters trigger, colliders parent = triggers parent 1 Answer
Manipulate gravity in an area? 1 Answer