x


Dynamic (scripted) vs Static (modeled) parenting - physics behaves differently

Say I created a small plane and wish the physics engine to spin it for me then come to a halt due to angularDrag. This works great.

Now, if I model a cylinder as a child of the rotating plane, it still works great.

But if I add a cylinder to the plane in the plane's Start function, the physics engine appears to get totally confused and will no longer spin the plane correctly.

Here are the steps to demonstrate this problem:

1) New Project

2) Game Object / Create Other / Plane - Position (0,0,0) - keep it selected

3) Component / Physics / Rigidbody - turn off Gravity

4) Project / Create / JavaScript - rename it RotatePlane - make it say this:

  function Start () {
    rigidbody.AddTorque(Vector3(0,100,0));
  }

5) Drag RotatePlane script under Plane object in hierarchy

6) Select Main Camera - set Position (0,15,0), Rotation (90,0,0) so we can see the plane

7) GameObject / Create Other / Spotlight - Position (0,20,0), Rotation (90,0,0) to light the plane

8) Click Play - see rotating plane


Case 1: Modeled Cylinder attached to Plane

1) GameObject / Create Other / Cylinder - set Position to (-2.5,1,0)

2) Drag the Cylinder under the Plane to parent it to the plane

3) Click Play - see rotating plane with cylinder - works great

See: http://www.hometot.com/unity/spin_works.html

See also: http://www.hometot.com/unity/wheel_works.html


Case 2: Dynamically-created Cylinder attached to Plane

1) Edit the RotatePlane script

function Start () {

    var comp = GameObject.Find("Plane");
    var cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
    cylinder.transform.parent = comp.transform;
    cylinder.transform.localPosition = Vector3(-2.5, 1, 0);

    rigidbody.AddTorque(Vector3(0,100,0));
}

2) Now click on Play and let run for a few seconds - the physics engine appears to get confused.

See: http://www.hometot.com/unity/wheel_broken.html

Any ideas what I'm doing wrong?

Thank you!

-- Glenn

more ▼

asked Apr 27 '10 at 05:22 AM

glewis gravatar image

glewis
3 1 1 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Those aren't the same thing...one, the manually created cylinder has a collider and the scripted cylinder doesn't, and they are scaled differently. Also, you shouldn't be adding a rigidbody to a mesh collider like the plane has; change it to a box collider instead.

more ▼

answered Apr 27 '10 at 06:02 AM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

I apologize, but you lost me. Are you saying that the manual version is also incorrect even though it works? What should the dynamic version script look like to make it work like the manual version?

Apr 28 '10 at 06:02 AM glewis

@glewis: When you manually created the cylinder, it has a collider, and the scripted version doesn't. Also the manually created cylinder is scaled to (.1, .5, .1) and the scripted version isn't. This makes them behave differently. Neither one of them is correct or not correct, they are just different. What's incorrect is using a mesh collider and a rigidbody together for the plane. You should get error messages for that. Replace the mesh collider for the plane with a box collider.

Apr 28 '10 at 08:11 AM Eric5h5

OK, sorry about the scale differences... that was a typo on my part. I edited the text above and removed the scaling of the manual cylinder. I've tried out your suggestion, and I'm thinking that I may not have been clear enough in my original description.

I'm trying to spin the plane about its center, and with the mesh collider, this works great. I want to add cylindrical "pegs" to the plane (as child objects) that spin along with the plane as if they are attached to it. With the box collider, the plane now spins around the peg, not what I want. I'll try to put together a video.

Apr 29 '10 at 06:33 AM glewis

I've added 3 live examples to the original. I can put the project source somewhere if that would help.

Apr 29 '10 at 06:55 AM glewis

@glewis: try adding the rigidbody component as the last step, after the parenting is done.

Apr 29 '10 at 04:48 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5058
x2158
x1783
x230

asked: Apr 27 '10 at 05:22 AM

Seen: 1597 times

Last Updated: Apr 29 '10 at 06:54 AM