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
0
Question by Pikmet · Apr 18, 2013 at 06:12 PM · movementphysicsvelocityrigidbodies

How can you carry rigidbodies without them appearing jumpy?

I'm writing a script that would allow a player to "carry" an object similar to how objects are carried with the gravity gun in Half Life 2. Since these objects are quite big, I have to worry about them not clipping through the floor, so I can't just change the position. I have tried many approaches such as addForce and parenting, but the best way I have so far is changing the velocity on each step. But this has it's own problems as the box appears jumpy when walking, and collisions with walls makes the box go crazy. Here is what I have so far:

The carry loop (in C#):

     IEnumerator carry(Transform obj) {
         obj.rigidbody.useGravity = false;
         while (carrying) {
                 Vector3 center = transform.position + (transform.forward* 4);
                 Vector3 objPos = obj.transform.position;
                 
                 if (Vector3.Distance(objPos, center) > 1.5F) {
                     break;
                 } else if (Vector3.Distance(objPos, center) > .1F) {
                     Vector3 Dir = center - objPos;
                     obj.rigidbody.velocity = Dir * 50;
                 } else {    
                     obj.rigidbody.velocity = Vector3.zero;
                 }
             yield return null;
         }
         obj.rigidbody.velocity = obj.rigidbody.velocity * .5f;
         obj.rigidbody.useGravity = true;
     }

How it is called:

     public void Update() {
         //If pressed E and not carrying
         if (Input.GetKeyDown(KeyCode.E) && !carrying) {
             RaycastHit hit;
             if (Physics.Raycast(transform.position, transform.forward, out hit, 8)) {
                 if (hit.rigidbody && !hit.rigidbody.isKinematic) {
                     carrying = true;
                     StartCoroutine(carry(hit.transform));
                 }
             }
         } else if (Input.GetKeyDown(KeyCode.E) && carrying) {
             carrying = false;
         }    
     }

Any ideas on how to make this more smooth? Or is there a better way to do it rather than changing the velocity each call (which I know messes with the physics)

Comment
Add comment · Show 2
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 MrSteve1 · Apr 18, 2013 at 06:26 PM 0
Share

I am using a similar concept in my game, only it uses the mouse, but you might find it useful and implement it some way.

 function On$$anonymous$$ouseDown(){
     gameObject.Find("ObjectName").AddComponent(DragRigidbody);
 }
 function On$$anonymous$$ouseUp(){
     Destroy(gameObject.Find("ObjectName").GetComponent(DragRigidbody));
 }

You will need to import the DragRigidBody script first, Unity comes with this.

avatar image Pikmet · Apr 18, 2013 at 08:12 PM 0
Share

$$anonymous$$y experience with this script was that the boxes are to slow to react when you use a spring joint, like the DragRigidbody script did (unless you give it a high spring value which creates it's own problems). Perhaps I was configuring it wrong, but no matter what variables I gave it, I couldn't get it to work decently.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Owen-Reynolds · Apr 18, 2013 at 08:15 PM

The dragObject script connects a spring between the thing you are dragging and an invisible "drag point" (which would probably be a child in front of your hands.) Getting springs set correctly is a pain, would need to be sure to damp lots, but that could work.

For the setting velocity solution, I've gotten better results by not "snapping" to a target speed. Instead of vel=???, use vel*=0.9f; vel+=???. The 1st part is fake drag (and could maybe just set the rigidbody drag very high.) When you have to change directions, thqt turns a "snap" into a few frames of slowing to reverse.

Getting the correct values is ugly. For example, with a drag of 10% (speed times 0.9) adding a speed of 5 will reach speed 50 (at speed 50 the 10% drag and the +5 cancel.) Values like times 0.75 (and more speed) will make it stiffer.

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 Pikmet · Apr 25, 2013 at 03:59 PM 0
Share

Unfortunately, I couldn't find a value for the fake drag that stopped the erratic behavior of the box. Low values of drag gave the same jumpy result, while high values created too much velocity.

avatar image Owen-Reynolds · Apr 25, 2013 at 08:57 PM 0
Share

While walking? Does moving forwards give sort a delayed jerk to the object? $$anonymous$$aybe also add all (or some) or the player's movement.

Code drags from 0.99 to 0.9 are interesting. Anything lower (more than 10%/frame) is basically just a $$anonymous$$oveTowards.

One more trick that works in some situations is to lower the push (or increase the drag) when close to the target area, and stop movement when "close enough."

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

13 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

Related Questions

move 2d character affected by physics with velocity and/or add force 2 Answers

Physics: Auto-stopping a spaceship with reverse thrusters 1 Answer

Player slows down when jumping/velocity changes 1 Answer

How to move a gameObj by cicking/ touching another gameobj? 0 Answers

Constraining maximum movement speed on a rigidbody on certain axes. 1 Answer


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