Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Fraggle222 · May 09, 2012 at 01:28 AM · rigidbodycolliderbugfly

Rigidbody behaves differently when I add a collider

I have a spaceship/airplane that is a rigidbody and I have a custom script to control it (see below), but it goes berserk if I add a box collider.

The thing flys ok if I don't have any collider enabled, but if I add/enable one the thing get's all funky (doesn't fly at all like it did before, spins out of control, etc). Can you explain why?

I thought colliders wouldn't effect anything unless I collide with something. But maybe that's all wrong, because I'm definitely not colliding and yet the motion get's all messed up.

Script:

 #pragma strict
 
  var AmbientSpeed:float = 3;
 
     var RollSpeed:float= 2;
     var PitchSpeed:float=.5;
     var rotFactor:float=0.025;
     var angularDrag:float=2;
     
 
 function Start () {
     rigidbody.angularDrag=angularDrag;
 }
 
 function Update () { 
 }

 function FixedUpdate()
     {
         var AddRot = Quaternion.identity;
         var roll:float = 0;
         var pitch:float = 0;
         var yaw:float = 0;  //not used
         
         roll = Input.GetAxis("Horizontal") * RollSpeed;
         pitch = Input.GetAxis("Vertical") * PitchSpeed;
         
         rigidbody.rotation=rigidbody.rotation*Quaternion.Euler(-pitch,0,-roll);
         
         var rotz=transform.eulerAngles.z;
         if(rotz>180) {
             rotz=rotz-360;
         }
         
         rotz=-rotz*rotFactor;
         
            rigidbody.AddTorque(Vector3(0,rotz,0));
         rigidbody.velocity=(transform.TransformDirection(Vector3.forward)*AmbientSpeed);
         
         print("roll="+roll+" rotz="+rotz);
     }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fraggle222 · May 09, 2012 at 02:57 AM 0
Share

To clarify, if I have a box collider attached but not enabled (unchecked in inspector) to the spaceship, and run the scene, then everything is fine. If I enable the box collider either before or during play (using checkbox in inspector) then the spaceship goes out of control, wacky rotations, etc.

avatar image perchik · May 09, 2012 at 05:28 AM 0
Share

Forgive me if this is obvious, but is there anything it might be colliding with?

avatar image AlucardJay · May 09, 2012 at 07:09 AM 0
Share

anything that is part of the gameObject with this script (including children) if they have any rigidbody's or colliders.

avatar image Fraggle222 · May 09, 2012 at 10:01 AM 1
Share

@perchick/alucardj: No it's not colliding with anything. I've reproduced the same behavior by creating a new game object (cube) and attaching this script to it. Attach a box collider and it goes haywire, but without the collider it's fine.

Try it.

5 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Richard-Buck · Nov 15, 2013 at 07:42 PM

I also found a sudden change in behavior when I added a collider. I was applying a torque to my Rigidbody to rotate it, but after adding the collider the effect of the torque was massively reduced.

I realised that the reason for this is that before the collider was added, the object effectively had a point mass (i.e. zero volume). The collider defines the volume of the object's mass, so after adding it the object's mass was no longer a single point, meaning that a larger force is required to rotate it at the same rate, exactly like the real world (think about pushing someone on a roundabout with them at the center, then at the edge).

Consider using ForceMode.Acceleration to manipulate objects without having to worry about their mass distribution.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wolf123450 · Feb 21, 2016 at 02:53 AM 0
Share

Thanks, Force$$anonymous$$ode.Acceleration helped my problem with jittery colliders.

avatar image
4

Answer by BadRAM · Oct 29, 2016 at 05:41 AM

I found a fairly safe workaround. you can manually set rigidbody.inertiaTensor to the default (1, 1, 1) and it will behave as if nothing was changed.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CornOnTheCob3000 · Jul 11, 2017 at 05:40 PM 1
Share

I know this is an old comment, but your response here saved me from a problem that I have had many a scream over. Thank you very much!

avatar image PlayCreatively · Apr 23, 2019 at 06:43 AM 0
Share

This works beautifully

avatar image brian1gramm1 · Oct 18, 2020 at 10:05 PM 0
Share

Thanks so much. This fixed my problem.

avatar image
2
Wiki

Answer by whydoidoit · May 13, 2012 at 05:03 PM

The centre of mass of a rigidbody is calculated based on its colliders, the combined set of colliders on an object and its children describe its physical dimensions. You would expect the physics of the object to change if you add a collider.

You also appear to be both adding torque and modifying the rotation directly - that's definitely going to cause some problems. Either you should be doing physics and applying forces or you should be modifying the rotations. You could make a kinematic physics object (to which you directly apply your transform and rotation changes and NOT forces), but that probably won't be what you want if you need good physical interactions between both participants in a collision - if that's the case you need to apply forces relevant to your input conditions.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fraggle222 · May 13, 2012 at 06:49 PM 0
Share

$$anonymous$$ike, thanks, this is probably the answer (center of mass issue). I've seen nothing in the docs on this, but then again I haven't read all the docs. Seems like a super important point. I don't know about your last sentence though. You can apply forces to a rigidbody that does not have a collider, it still has shape, and plenty will happen, but it won't collide.

avatar image whydoidoit · May 13, 2012 at 07:46 PM 0
Share

Ah yeah you are right, they do don't they... Sorry, been working too long with multiple colliders! The car tutorial gives details of the effect of having multiple colliders without having a specific centre of mass by the way.

You definitely want to watch trying to set a rotation and apply physics at the same time - unless you stop the physics affecting the rotation the simulation will be messed up.

avatar image Fraggle222 · May 14, 2012 at 02:37 AM 0
Share

The problem is that I can't seem to get the motion to work correctly if I use just forces and torques. Also, I don't think the center of mass thing is the issue. I tried setting rigidbody.centerOf$$anonymous$$ass=Vector3.zero. It didn't help.

avatar image FlightOfOne · May 29, 2017 at 02:04 AM 0
Share

I ran into the same challenge (5.6). I ended up doing what you mentioned, using forces (AddForceAtPosition. off set from center) to cause the rigidbody to rotate, ins$$anonymous$$d of using AddTorque. I tried doing what Richard Buck suggested(using Force$$anonymous$$ode.Acceleration) and it works too, however, I got much better, and predictable results this way.

Also, I should note that when you add a forceAtPosition, to also add a counter force in the opposite side to get a torque result so that the net forces will be equal to zero. Otherwise, you will be added extra forces to a certain direction. In many cases this will give you unrealistic behavior.

avatar image
1

Answer by ArenMook · Jan 22, 2016 at 09:27 AM

Ran into this oddity myself. For future reference this bug happens when you update the rigidbody's mass at run-time. Apparently Unity seems to cache the mass somewhere, and never updates it unless you add a new collider to the rigidbody. The only work-around I've found was to forcefully DestroyImmediate() the rigidbody and re-make it with the updated mass value.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by 4-bit · Feb 19, 2013 at 04:18 AM

Has anyone found a solution for this yet?

I'm running into exactly the same thing in my game right now, where I'm trying to get my asteroids spinning as they wake up.

The following code is being fired in the OnStart part of the script attached to the object.

 rigidbody.AddTorque (Random.Range(-100,100),Random.Range(-100,100),Random.Range(-100,100));
 rigidbody.AddForce (Random.Range(-250,250), Random.Range(-1500,1500),0);

Add Force fires just fine, but the AddTorque does nothing.

What is really frying my brain is I would swear AddTorque worked on the object before I added addforce, and it was addforce that got me messing with things. Now even when I switch things back AddTorque just isn't working.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zinger · Feb 19, 2013 at 02:27 PM 0
Share

Is this the same issue that was posted? The original question had to due with differing behavior before and after a collider was added to a rigidbody.

avatar image 4-bit · Feb 19, 2013 at 05:44 PM 0
Share

Actually, yes it was. The torque and force worked fine until I added a collider sphere. Then torque stopped. If I shot the object it would spin, but not from code.

If I turned off the collider it would work as normal.

What I found this morning is if I put Force$$anonymous$$ode.Impulse into the torque it begins working. So, problem solved, but not sure what difference the collider was making.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

No bounce after rigidbody collision 2 Answers

Collider Mask Bug 0 Answers

How to Stop a Game Object from Passing through a Collider while it's being Dragged? 3 Answers

Is it possible to block a collider with a trigger from hitting and object behind it? 1 Answer

How to limit a ray to go downwards to infinity, how can I make it down only 1 float. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges