Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Ian Spangler · Feb 09, 2010 at 11:36 PM · rigidbodypositionrespawn

setting position of two rigidbodies connected by a Fixed Joint

Hello, I am relatively new to Unity, and I am trying to set the position of two rigidbodies that are linked by a Fixed Joint.

Code: rigidbody1.position = spawnPoint.position; rigidbody2.position = spawnPoint2.position;

(spawnPoint and spawnPoint2 are empty game objects set at particular locations)

The physics/animation goes haywire from this unless I remove the Fixed Joint. Even when I just set the position of only one of the rigidbodies, it still does not work. And I can't set the position of a container object for the rigidbodies because the rigidbody children move away from their parent's coordinates during the course of the game I'm making and I need to relocate them to their starting point.

What would be the correct approach here? I appreciate any answers.

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Ashkan_gc · Feb 10, 2010 at 04:07 AM

when you use fixed joints it means that your gameObject should not be able to move so you should remove the joint and move your rigidbodies and then connect the joint again.

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 RC-1290 · Jul 31, 2017 at 07:51 AM 0
Share

It is worth noting that using Destroy to remove the joint, and re-creating the joint in the same frame can result in unexpected behavior (because the destroy doesn't really take effect until the end of the frame).

Luckily setting connectedBody to null, changing the position, and the setting connectedBody back to the original value, does work in one frame.

avatar image
2

Answer by duck · Feb 10, 2010 at 10:24 AM

In general you shouldn't adjust the position of physics objects yourself. You should apply forces and allow the physics engine to handle the movement and collisions.

The specific problem you're having is that as far as I know, a "fixed joint" is actually simulated as an extremely rigid and stiff spring.

By moving just one of the objects, you're forcing that spring to be stretched much further than it should be, so during the next physics update the spring exerts a massive force on the two objects resulting in the "haywire" behaviour that you're seeing!

There is a function called "MovePosition" which is designed to allow you to move a rigidbody to a specified position, however it's generally intended for use with kinematic rigidbodies only. You might find that if your objects don't collide with anything during this move, it might work okay, but you'll have to make sure that you move both rigidbodies so that after the move they are still the same fixed distance apart - and so, you won't have introduced a massive force in the fixed joint.

Alternatively you could try disconnecting the joint (set the joint's .connectedBody to null) temporarily before you move the object, then re-attach the joint after the move.

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 Ian Spangler 1 · Feb 10, 2010 at 08:14 PM

Thanks, this makes sense. I was aware of the fact that repositioning rigidbodies manually is discouraged, but in this case I actually do need to relocate my character (composed of 2 rigidbodies) to a specified starting point upon a death trigger and doing it on the container object doesn't work because the container object's coordinates don't move along with the rigidbodies' coordinates.

I like the idea of disconnecting the joint dynamically with code. I wasn't sure if that was actually possible, but now that i see it and it looks quite simple, I will give that a try before anything else.

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 Lipis · Feb 24, 2010 at 08:34 PM 0
Share

@Ian Please use the add comment if you want to reply to an answer, rather than posting another answer.

avatar image
0

Answer by Santa · May 27, 2011 at 12:39 PM

It is also possible to set gameObject.active = false. Then move objects. And then activate them back.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can I arbitrarily move a dynamic Rigidbody 2D? 1 Answer

Explain why the y-axis of the GameObject's position is not exactly zero when rigidbody is added to it. 1 Answer

Destroy object on collision or check position 2 Answers

Is there a way to set velocity while setting the position? 1 Answer

Network or camera desync or something else? 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