Parent/Children with AddForce (16729)

I guess this is a pretty simple question, but I just can't find the answer.

I'll use this example for a base, say we're flying a rocket. Now the rocket's power doesn't come from the rocket itself does it? It comes from the rockets engines. So what I am trying to do is re-create that. I first create the engine object, and add force to it, making sure I lock all rotations and what not. I then attach the engine object to the main rocket, this is when I run into problems. Instead of the engine sticking to the rocket, the engine just flies off itself.

What do I need to change here?

~ Alex

2 Answers

2

just as you said, the rocket's movement is based on the engine's movement, not the other way around. so the engine should be a parent of the rocket, and the rocket will be the child. The child will move/do whatever the parent does. so if the engine moves up, the rocket will move up.

what do you mean 2 rockets? just be a little more specific of what you're trying to do and i'll try to help you out.

in that case, you will not be able to do this parent/child method. you will instead "pretend" that the rocket has an engine, and you will just apply different forces for each engine directly to the rocket. If that confuses you, i'm sorry.

You could apply your forces to the rigidbody on the engine's parent if you're set on having the engine be a child to the rocket fuselage. You can use AddForceAtPosition to fake the relative position.

try:

if(transform.parent != null && transform.parent.gameObject.rigidbody != null){
    transform.parent.gameObject.rigidbody.AddForceAtPosition(yourForce, transform.position);
}

http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForceAtPosition.html

Hmm. Wouldn't expect this to be at all a massive performance drain :o If you wanted to speed it up, you could cache off a reference to the rigidbody earlier, and thus just turn it into: mainRigidbody.AddForceAtPosition(yourForce, transform.position); but that wouldn't make a noticeable difference unless you have hundreds/thousands of these guys?