- Home /
Refer to a parent Rigidbody
Hey
I would like to make a simple reference to my parent gameobject which has a Rigidbody. All time I would like to hadle with it I suck. Does not rly work at all. I hope you can help me there.
using System.Collections;
public class TankMoveBehaviour : MonoBehaviour {
public Rigidbody daVoda;
public string daVodaX;
public Vector3 eulerAngleVelocity = new Vector3(0, 100, 0);
public bool check = false;
public bool forward = false;
public bool backward = false;
public bool rotateleft = false;
public bool rotateright = false;
// Use this for initialization
void Start () {
daVoda = null;
//daVodaX = this.gameObject.transform.parent.name.ToString();
}
// Update is called once per frame
void Update () {
//daVodaX = this.gameObject.transform.parent.name.ToString();
//Debug.Log(this.gameObject.transform.parent.name.ToString());
if(check == true)
{
{
daVoda = this.gameObject.transform.parent.rigidbody;
Debug.Log(daVoda.ToString());
}
if(Input.GetKeyDown(KeyCode.F))
{
forward = true;
}
else
{
forward = false;
}
if(Input.GetKeyDown(KeyCode.C))
{
backward = true;
}
else
{
backward = false;
}
if(Input.GetKeyDown(KeyCode.X))
{
rotateleft = true;
}
else
{
rotateleft = false;
}
if(Input.GetKeyDown(KeyCode.V))
{
rotateright = true;
}
else
{
rotateright = false;
}
if(forward == true)
{
daVoda.AddForce(transform.forward * 200);
}
if(backward == true)
{
daVoda.AddForce(-transform.forward * 200);
}
if(rotateleft == true)
{
Quaternion deltaRotatio = Quaternion.Euler(-eulerAngleVelocity * Time.deltaTime);
daVoda.MoveRotation(rigidbody.rotation * deltaRotatio);
}
if(rotateright == true)
{
Quaternion deltaRotatio = Quaternion.Euler(-eulerAngleVelocity * Time.deltaTime);
daVoda.MoveRotation(rigidbody.rotation * deltaRotatio);
}
}
}
}
That's all. I don't know why nothing happens....
Thank's for each answer ;)
$$anonymous$$ake sure that your object has a parent. If you are 100% sure then there should be no problem. Or if you will be assigning it at runtime, don't forget your trusty null checks!
Answer by M-G-Production · Dec 05, 2014 at 09:52 PM
Hi .black!
Try this! Store the parent in another Transform variable!
public Transform daVodaParent;
public Rigidbody daVoda;
void Start()
{
//You don't need to set daVoda to null!! Already the case.
}
void Update()
{
//ADD THIS
daVodaParent = transform.parent;
daVoda = daVodaParent.rigidbody;
}
This should work!
Bests, Math
Your answer
Follow this Question
Related Questions
Using hinge joints to make a balance board 1 Answer
Object Reference Required to access non-static member 'UnityEngine.RigidBody.AddRelativeTorque' 1 Answer
colliders arenot colliding 1 Answer
NullReferenceException Error, has not been solved yet. 3 Answers
Adding a collider to a rigidbody stops addforce working. 0 Answers