Search Unity

Resolved How to hold Rigidbody items

Discussion in 'Physics' started by Deleted User, Dec 31, 2020.

  1. Deleted User

    Deleted User

    Guest

    I have a Rigidbody item system in my game that makes items kinematic when you pick them up. Collisions and movement work fine on other items, but they go right through the walls! When I drop them and turn isKinematic off, it works fine again. Any ideas on why this is happening?
     
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    For kinematic rigidbodies, you control their position, not the physics engine. That also means that when you set their position inside a wall, the physics engine can't push them out.
     
  3. Deleted User

    Deleted User

    Guest

    If that is the case, then how can I make my player hold an item and perform collisions on it without the item moving around?
     
  4. ShokTheIV

    ShokTheIV

    Joined:
    Nov 19, 2020
    Posts:
    17
    set the player's rigidbody to dynamic, and the item's rigidbody constrains to freeze all
     
  5. Deleted User

    Deleted User

    Guest

    My player is a CharacterController, not a rigidbody. Setting the item's constraints doesn't change anything; the items still go through the walls. Also, my items become children of my Camera node when you hold them, so that might mess things up.
     
  6. ShokTheIV

    ShokTheIV

    Joined:
    Nov 19, 2020
    Posts:
    17
    oh the gun will still go thru walls no matter what u do, try to bump up the width of the fps controller collider so it will give an illusion that the gun isnt going thru walls but its just the player
     
  7. Deleted User

    Deleted User

    Guest

    Afraid that won't work. It is an FPP game, and there are several items of varying size. e.g. a key would seem to collide with the wall in the same way as a box. Maybe I could somehow add the item's collider to the PlayerController? That way it would use the Move() method, right?
     
  8. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Keep the object dynamic (don't set it to kinematic), find some position you want it to hover around (pro tip - let the player change distance with scroll wheel) and hold it there with dynamic forces, get the direction from the object to the hover position and add force in that direction (use the velocity change one that doesn't care about the mass if you want all objects to behave the same)
     
  9. Deleted User

    Deleted User

    Guest

    Like I said before
    If I use your method, then my items will move around when they hit the walls no matter how much force I apply. That's not really what I want. I may make my player a Rigidbody because of no compound colliders :(, so I guess I'll just try a joint Component or something.
     
  10. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    Are you asking for a system where the player holding an object is not allowed to rotate/move in a way that would make the position of the held object illegal? If that is the case and you absolutely know what you want, then you can poll the legality of the position of the held object with, for example, Physics.CheckBox. The idea here being that you check the position of your kinematic rb held in hands using that method and if it returns true, you have violated the physics world and you must revert the position and rotation of your character controller back to its' previous values.

    On a side note, however, this sounds like a bad user experience solution to me, as it will frustrate users not being able to move where they want to. Maybe you can reevaluate your goals and go for the half-life 2 approach where you pick up a prop and it remains perfectly aligned with your view, but when you push said prop against geometry it offsets and eventually falls out of hands? It seems like just a matter of programmatically applying a joint between your character and the object without making it kinematic https://docs.unity3d.com/Manual/Joints.html
     
  11. Deleted User

    Deleted User

    Guest

    Yes. I just want the player-Rigidbody or not-to stop moving when the held item collides with a wall. I think Hello Neighbor 2 uses a method like this. I believe that the dynamic approach used in half-life 2 and various other games would be a worse user experience because it would feel like you have strechable arms. However, it might feel more realistic if the item could move a little bit. I will experiment with the Physics.CheckBox method, and possibly SpringJoint.
     
  12. Deleted User

    Deleted User

    Guest

    Turns out it was a lot simpler than I expected. Since Rigidbody players support compound colliders, all I had to do was make my items children of the player and remove their Rigidbodys while they were being held. Now It works great :)!