Search Unity

How to block Controller cursor on Rigidbodies?

Discussion in 'Daydream' started by wojwen, Jul 31, 2017.

  1. wojwen

    wojwen

    Guest

    I'm going through all quality requirements for daydream and I have problem with this one:
    In my game player is inside a cockpit which is a rigidbody. An obvious sollution to make the cursor not go through cockpit parts would be adding a mesh collider, but since it's a rigidbody it needs to be convex. Because of this I have to split my collider to many parts that will be convex just to meet this quality requirement. This is terrible in terms of performance. Do you know any other way to do it?
     
    Michael-Ho likes this.
  2. Michael-Ho

    Michael-Ho

    Joined:
    Nov 2, 2012
    Posts:
    25
    i have the same question :)
     
  3. dsternfeld

    dsternfeld

    Official Google Employee

    Joined:
    Jan 3, 2017
    Posts:
    72
    You can create a duplicate dummy version of the cockpit that has a concave MeshCollider, a disabled MeshRenderer, and nothing else. If the geometry of the cockpit is complex and causes the performance cost of the MeshCollider to be too high, you can create a simplified version of the cockpit mesh to use just for collisions.

    Additionally, have you verified that the performance cost of using several box colliders to approximate the bounds of the cockpit is too expensive? It's not uncommon for MeshColliders to be more expensive than having many BoxColliders.

    Thanks,

    Dan
     
  4. wojwen

    wojwen

    Guest

    The geometry being too complex is not the problem. The problem is that the controller requires colliders so it doesn't go through mesh. Because of that we can't use mesh colliders that are concave in many cases (when there is a rigidbody on the same object). This combined with the fact that player is inside the cockpit forced me to add dozens of convex colliders instead of one concave collider - and it really annoying since Gvrcontroller works fine with it.
    I didn't because my cockpit is too complex to use box colliders.

    Thanks,
    Wojtek
     
  5. dsternfeld

    dsternfeld

    Official Google Employee

    Joined:
    Jan 3, 2017
    Posts:
    72
    If you need your physics simulation of the rigidbodies to be more accurate than is possible with a convex collider, then you can look into using something like this.

    If you only need the concave colliders for raycasting from the controller, then you can do the following:
    1. Create a GameObject as a child of the object with the RigidBody and call it "RaycastBounds".
    2. Add a MeshFilter to the RaycastBounds object, give it the same mesh as the object with the Rigidbody.
      1. Do not add a MeshRenderer to the RaycastBounds, as it does not need to be rendered.
    3. Add a MeshCollider to the RaycastBounds object.
    4. Use layers to ensure that the only the RaycastBounds colliders are used when raycasting from the controller.
    Thanks,

    Dan
     
  6. wojwen

    wojwen

    Guest

    I did what you suggested and it's the same result. The exact error message I get is:
    Thats why it's so inconvenient.

    Thanks,
    Wojtek
     
  7. dsternfeld

    dsternfeld

    Official Google Employee

    Joined:
    Jan 3, 2017
    Posts:
    72
    The RaycastBounds object should not have a RigidBody on it. Just the MeshFilter and the MeshCollider.

    Thanks,

    Dan
     
  8. wojwen

    wojwen

    Guest

    In my game (and most other games) Rigidbody has to be on the top of the hierarchy, because if it's lower it will move independently to the objects higher in hierarchy. Because of that RaycastBounds object will always be under the Rigidbody (in hierarchy). All objects that are children of an object with Rigidbody component attached act as if they have a Rigidbody on them. Because of this what you are suggesting is impossible.

    Mesh colliders are designed to work with physics and using them to interact with the Controller is inconvenient. It would be nice if we could have another component just for GvrController interactions.

    Thanks,
    Wojtek
     
  9. dsternfeld

    dsternfeld

    Official Google Employee

    Joined:
    Jan 3, 2017
    Posts:
    72
    You're right, the solution I gave above isn't adequate by itself. However, here is a more fleshed out solution that I just tested:

    ConcaveCollision1.png

    In the above image, the "Unicorn" object is selected and you can see in the inspector that it has a convex mesh collider used for physics interactions, as well as a RigidBody. Also, the layer is set to "Ignore Raycast" so that the convex mesh will not be hit by raycasts from the controller.

    ConcaveCollision2.png

    In the above image, the "UnicornRaycastBounds" object is selected, which is a child of "Unicorn". It has a MeshCollider using the same mesh as the "Unicorn" object, except this MeshCollider is concave. Additionally, it has a kinematic RigidBody on it. Including this RigidBody prevents Unity from associating the MeshCollider on this object with the RigidBody on the parent, and concave MeshColliders will work with kinematic rigidbodies.

    The script "IgnoreCollisionsWithCollider" is used to tell the physics engine to ignore collisions between "UnicornRaycastBounds" and "Unicorn" so that the RaycastBounds don't impact the physics simulation of the "Unicorn". It looks like this:

    Code (CSharp):
    1.   public Collider toIgnore;
    2.  
    3.   void Awake() {
    4.     if (toIgnore != null) {
    5.       Physics.IgnoreCollision(GetComponent<Collider>(), toIgnore);
    6.     }
    7.   }
    ConcaveCollision3.png

    In this image, you can see the cursor correctly matching the depth on the Unicorn's concave mesh.

    Thanks,

    Dan
     
    wojwen likes this.
  10. wojwen

    wojwen

    Guest

    I understand your solution, but it doesn't work for me. Every spaceship cockpit it tried it out on spins uncontrollably. The good thing is that I don't get the Non-convex MeshCollider error on the start.

    Thanks,
    Wojtek
     
  11. dsternfeld

    dsternfeld

    Official Google Employee

    Joined:
    Jan 3, 2017
    Posts:
    72
    When I tested this, the key to preventing uncontrollable spinning was using Physics.IgnoreCollision (In my example, using the IgnoreCollisionsWithCollider script). This is used to prevent the two overlapping colliders from generating collisions from each other.

    Thanks,

    Dan
     
  12. wojwen

    wojwen

    Guest

    My fault, sorry for that. I forgot about another collider I had attached. This works great!

    Thanks,
    Wojtek
     
    dsternfeld likes this.