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
0
Question by kubajs · Mar 26, 2020 at 10:34 AM · updatefixedupdaterigidbody physicsjittering

Proper item carry by player respecting physics (collisions)

Hi guys, I'm trying to figure out how to properly carry items by player while respecting collisions between the item and obstacles. Approaches I tried:

  • Update method approach - the movement is pretty smooth, no jittering but of course transform is often out of sync of physics, therefore it's possible to push an object fast throught a wall or worse ground. Tried Physics.SyncTransforms() as well.

  • Fixed update method - the movement jitters considerably (I would need to significantly decrease fixed update time and it won't completely fix the issue).

  • Spring joint or fixed joint method - the same issue as above - physics is automatically updated in fixed interval set in fixed update, so any joint jitters significantly when moved by player. Also I'm afraid fixed update won't help the situation when player carries an item an makes very fast movement (rotation) agains a wall. The collider will go through the wall despite the rigidbody is set to continuous dynamic. Any ideas, tips, how to achieve this? I really cannot figure this out myself. One idea was to force item drop when delta position of the carried item between last 2 frames exceeds some limit. Doesn't work well, too bad experience for player. I can provide more details or test scene or video if needed.

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

2 Replies

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

Answer by Antilight · Mar 26, 2020 at 01:45 PM

I assume you're only needing this for large objects like crates and boxes but not small single handed objects. So what I have done in the past is use an empty child object in front of the character and have held objects chase that empty. If the object fails to remain within a set distance of the empty then it is simply dropped. This can be caused by collisions or the player moving too fast. Hope this helps.

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 kubajs · Mar 26, 2020 at 10:59 PM 0
Share

@Antilight Thank you for your answer. I tried both approaches - used threshold between the parented holder object in fps camera (player's look camera), when the threshold value is exceeded, I let the item drop. The second approach (too fast movement) is quite i big compromise. I tried it before but I'd like to avoid this as items are then dropped automatically too often. I'd really like to know how the authors of Stranded Deep survival game managed to implement it. No jittering, I expect they used configurable joint or spring joint for this without any issues.

avatar image
0

Answer by kubajs · Mar 26, 2020 at 11:55 PM

You are right. I now tuned it in the way of a good compromise. Movement smoothness is acceptable. Sharing the code for those interested / struggling with this as well.

Related video here: https://www.youtube.com/watch?v=F_tmmyTw3cY

 using UnityEngine;
 
 public class ItemCarrying1 : MonoBehaviour
 {
     [SerializeField] private float _grabReach = 5f;
     [SerializeField] private float _followSpeed = 10f;
     [SerializeField] private float _carriedItemAngularDrag = 10f;
     [SerializeField] private Transform _camera;
     [SerializeField] private LayerMask _layerMask;
 
     private bool _carrying;
     private Rigidbody _carriedRigidbody;
     private Transform _grabHolder;
     private RigidbodyValues _originalRigidbodyValues;
 
     private void Awake()
     {
         _grabHolder = new GameObject().transform;
         _grabHolder.SetParent(_camera);
     }
 
     private void Update()
     {
         Physics.SyncTransforms();
         if (Input.GetMouseButtonDown(0) && Physics.Raycast(_camera.position, _camera.forward, out RaycastHit hit, _grabReach, _layerMask))
         {
             _carriedRigidbody = hit.collider.GetComponent<Rigidbody>();
             if (_carriedRigidbody)
             {
                 _grabHolder.position = _carriedRigidbody.transform.position;
                 _carrying = true;
                 StoreOriginalRigidbodyValues();
                 SetCarryRigidbodyValues();
                 //_carriedRigidbody.transform.SetParent(_grabHolder);
             }
         }
         else if (Input.GetMouseButtonUp(0) && _carrying)
         {
             Drop();
         }
 
         if (_carrying)
         {
             _carriedRigidbody.velocity = (_grabHolder.position - _carriedRigidbody.transform.position) * _followSpeed;
             if (PushesItemThroughObstacleTooHard())
             {
                 Drop();
             }
         }
     }
 
     private bool PushesItemThroughObstacleTooHard()
     {
         return Vector3.Distance(_carriedRigidbody.transform.position, _grabHolder.position) > 3f;
     }
 
     private void Drop()
     {
         _carrying = false;
         RestoreOriginalRigidbodyValues();
         _carriedRigidbody.transform.SetParent(null);
         _carriedRigidbody = null;
     }
 
     private void SetCarryRigidbodyValues()
     {
         _carriedRigidbody.angularDrag = _carriedItemAngularDrag;
         _carriedRigidbody.interpolation = RigidbodyInterpolation.Interpolate;
         _carriedRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
         _carriedRigidbody.useGravity = false;
     }
 
     private void StoreOriginalRigidbodyValues()
     {
         _originalRigidbodyValues.angularDrag = _carriedRigidbody.angularDrag;
         _originalRigidbodyValues.interpolation = _carriedRigidbody.interpolation;
         _originalRigidbodyValues.collisionDetectionMode = _carriedRigidbody.collisionDetectionMode;
         _originalRigidbodyValues.useGravity = _carriedRigidbody.useGravity;
     }
 
     private void RestoreOriginalRigidbodyValues()
     {
         _carriedRigidbody.angularDrag = _originalRigidbodyValues.angularDrag;
         _carriedRigidbody.interpolation = _originalRigidbodyValues.interpolation;
         _carriedRigidbody.collisionDetectionMode = _originalRigidbodyValues.collisionDetectionMode;
         _carriedRigidbody.useGravity = _originalRigidbodyValues.useGravity;
     }
 
     private struct RigidbodyValues
     {
         internal RigidbodyInterpolation interpolation;
         internal float angularDrag;
         internal bool useGravity;
         internal CollisionDetectionMode collisionDetectionMode;
     }
 }
 

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 kubajs · Mar 27, 2020 at 12:43 AM 0
Share

Video (still in low quality probably as I uploaded it a few $$anonymous$$utes ago but it's not the important factor): https://www.youtube.com/watch?v=F_tmmyTw3cY

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

127 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with animation and void fixedupdate() 1 Answer

Follow camera: Player jumps on move 1 Answer

score, Update, Fixed Update and screen refresh rate (fps) 2 Answers

How to restrict rotation properly 2 Answers

faster calculation without skipping(projectile movement) 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