- Home /
How can I access a collider's parent's rigidbody?
I have a vehicle and I'm attempting to implement a speed boost box collider. It seems like I'm accessing the parent correctly, but it's not working.
I have a game object with a rigidbody attached and the collider is a child game object with a box collider attached. I don't get any error saying it can't access the rigidbody, but it's not doing anything.
Here's what I have:
using UnityEngine;
using System.Collections;
public class SpeedBoost : MonoBehaviour {
public float boostForce = 10.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay(Collider other) {
other.transform.parent.rigidbody.AddForce(other.transform.parent.rigidbody.transform.forward * boostForce, ForceMode.Acceleration);
}
}
This works as a script attached to the vehicle using the line:
base.rigidbody.AddForce(base.rigidbody.transform.forward * NitroForce, ForceMode.Acceleration);
What am I missing here?
Answer by Linus · Jul 26, 2013 at 01:47 AM
I think you are doing it right. But the AddForce needs to be applied over time, usually in FixedUpdate. Or if you really just want it applied once, you need more force than 10.0 Experiment with different values for boostForce
See http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
It doesn't seem to work, I tried
other.transform.parent.rigidbody.AddForce(Vector3.up * 100, Force$$anonymous$$ode.Acceleration);
to see if it would send the vehicle upwards, but to no avail.
Over time would be great, as the vehicle will be in the box collider for a few seconds. With OnTriggerStay it is running the code about 60-80 times. I put a Debug.log in there to see that it was actually running the code.
As it turns out, I just made a simple error because I'm not used to Unity yet. I was changing boostForce in the script forgetting that it was exposed to the Inspector, and there it was set to 3. 25 provides a good boost.
Your answer
Follow this Question
Related Questions
Trigger BoxCollider Fails to Move With GameObject After Resize 0 Answers
Getting the Rigidbody of a Collider? 1 Answer
Rigidbody.SweepTest Hit Trigger Colliders 0 Answers
OnTriggerEnter not being called with multiple colliders 1 Answer
Checking gameobject in Collider range only on method call 1 Answer