Search Unity

Controller with a collider is pushing me around!

Discussion in 'Daydream' started by HeyBishop, Oct 25, 2017.

  1. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    109
    I have a sword gameobject (with a box collider) that I instantiate as a child of GvrControllerPointer which is a child of my player. When I swipe my sword/controller at game objects with rigidbodies, they respond as they should - but so does the player!

    Any ideas of how to allow both the physics of the colliders hitting a rigidbody without the recoil/pushback I'm getting. I almost threw up when I pointed down at the ground, and pushed myself up from the ground's mesh collider!

    Here's a quick clip of my bumpy interaction:




    upload_2017-10-24_23-59-17.png
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,282
    Is your character colliding with the sword? If the rigid bodies are not connected with a joint and don't collider then this shouldn't happen I wouldn't have thought.
     
  3. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    354
    So looking at the doc it sounds like you have made the rigidbody for the sword a child of the rigid body for the player (rather than just having the ojects in the scene graph as parent-child I think?) and both a dynamic objects (ie. the player is not a kinematic body.

    Naive thought is just to not have the parent-child relation on the rigid bodies
     
  4. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    109
    No, not quite. The sword doesn't have a rigid body, it only has a collider. My player object, however, does have a rigid body.
     
  5. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    354
    But it moves right, that implies it's associated with a rigid body, if that's the player's rigid body, than as far as the physics engine is concerned it's the player rigid body and the ground/table/flask rigidbodies that need to be pushed apart right?
     
  6. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    578
    You can also use the physics layers matrix to make the sword's layer not collide with the player's, etc. depending on your answers to questions above.

    (Usually your player's rigidbody is kinematic anyway - you also don't want anything ELSE to ever "move" the player other than direct player input - and this would solve all such problems)
     
  7. dsternfeld

    dsternfeld

    Official Google Employee

    Joined:
    Jan 3, 2017
    Posts:
    72
    Agreed that you probably want your rigidbody to be kinematic. Also, you can use Physics.IgnoreCollision to make collisions between two colliders ignored by the physics system.

    Additionally, you may be interested in looking at the Arm Models element in Daydream Elements. It includes a modified arm model that is tuned specifically for simulating the motion of swinging a sword.



    Thanks,

    Dan
     
  8. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    109
    Thanks for your feedback everyone.
    My problem isn't with the sword colliding into my player. Come to think of it, it can't because half of it is always within the bounds of my player's capsule collider. My problem only presents itself when the sword collides with other objects - such as a static table - or the ground. When that happens, it adds force to the player (the sword's parent). Turning on Is Kinematic solves the problem, but but then I lose gravity which I need to keep my player grounded on the non-flat ground.

    Note: The movement controller I created for my player uses transform instead of force because I want movement to start and stop with zero acceleration and deceleration to prevent sim-sickness. I know that kinda breaks the rules, but it works pretty well for the most part.
     
  9. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    109
    I uploaded a video of me demonstrating my issue. The desktop screen capture quality is a little dicy, so hopefully you can see my problem despite the jerky playback.



    By the way @dsternfeld , I haven't tried the GvrArm yet - I'll experiment tonight!
     
  10. Claytonious

    Claytonious

    Joined:
    Feb 16, 2009
    Posts:
    578
    Using gravity to ground your player is a computationally expensive hack. Think of all the work that physx is doing to make that happen, and observe the side effects you're suffering from it. Just raycast against your "ground" and move your player to the hit position, or take advantage of the height field or nav mesh if you have one to find the elevation at your player's position, or whatever.

    But making your player a truly physical rigid body is not a good answer.
     
  11. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    109
    That's a cool idea!!!
     
  12. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    109
    Hang on though - I need the rigidbody on my player so that I can't pass through colliders on other objects. Besides, I kinda like the way the gravity causes me to slip down hills that are too step, etc.

    Hopefully the Swing Arm is the answer. Alas, I'm having troubles importing Daydream Elements. (attn: @dsternfeld )
     

    Attached Files:

  13. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    354
    Look you said the sword was just a collider of the parent's rigid body, I think a few people have suggested that's likely the issue.

    Colliders just represent the shape of the rigid body in the physics simulation. Adding a sword shaped collider to the player just makes them lumpier/fatter, but when the lump hits something the collision response will always be to apply separation force to the "rigid bodies".

    If you want the sword to respond to collision on it's own it needs to be it's own thing to the physics engine, something that can be moved back independently when it is thrust against the ground.

    EDIT: Also according to https://unity3d.com/learn/tutorials/topics/physics/physics-best-practices "Game objects which don’t have a RigidBody component are considered static colliders. This is important to be aware of because it’s extremely inefficient to attempt to move static colliders, as it forces the physics engine to recalculate the physical world all over again."
     
    Last edited: Oct 27, 2017
  14. dsternfeld

    dsternfeld

    Official Google Employee

    Joined:
    Jan 3, 2017
    Posts:
    72
    I think you may find that you get the results you want if you get rid of the Rigidbody completely and instead use a Character Controller which is built for your use case. When you move your player by calling CharacterController.SimpleMove, gravity will be applied automatically and the character will be correctly constrained by collisions. I believe you will want to call SimpleMove every frame even if the speed is zero, so that gravity is still applied on frames where the player isn't actively moving.

    If you do this, the collider from the sword won't be grouped together with the collider that is part of the CharacterController like it is with a Rigidbody.

    As for Daydream Elements:

    Did the advice in the issue here help you resolve the import issue? If not, please let us know.

    Using the SwingArm functionality in Daydream Elements will not help you solve the issue with the colliders. It's meant to make the motion of the sword swing feel more realistic as you move the controller around. The built in arm model is tuned for pointing at things with a laser pointer, and Daydream Elements shows how you can use different Arm Models tuned for different types of interactions. You can test it out by installing the Daydream Elements APK without needing to import it and build it yourself.

    Thanks,

    Dan
     
    ROBYER1 likes this.
  15. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    109
    Awesome!
    I've still got a lot to learn. The Character Controller looks like what I need. At quick glance, it looks like I should be able to feed it the XY position of TouchPosCentered.

    I'm focusing on another component of the game right now, I'll come back to this conundrum later!

    Thank you Dan!
     
  16. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    109
    Took me a while, I've finally taught myself how to use the Character Controller. I've imported a character model that is now animated when I walk, run and turn (thanks to NurFaceGames tutorials). The animations I'm using for the walk/turn/etc is from Unity Standard Assets ThirdPersonAnimatorController.

    I've followed the instructions on how to use the SwingArm, and I've got my sword working! No more pushback, and I still get to knock around items.

    Now, I'd like to attach the arm/hand of my character avatar to appear to be holding the sword. Now that original issue of this thread is solved, I've start a new thread asking for help on my next step here:
    https://forum.unity.com/threads/usi...s-with-third-person-character-mecanim.504139/

    Any help/guidance would be appreciated!
     
  17. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    282
    This was extremely useful for us also, thanks for the tips!