Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycasts and Interpolated Physics Bodies

Discussion in 'Physics' started by SuperUltraHyper, Feb 24, 2016.

  1. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    I am using a modified version of Oculus's Gaze Pointer Stuff. Basically it has raycast going from the camera forward looking for intersections with objects. On an intersection it places a quad (crosshair) offset from the object. The camera is nested in a series of other game objects.

    This all works as desired. However, the root object is a vehicle. I was having an issue with jerky head rotation and I was forced to flag the (Vehicle's) Rigid Body Interpolate method to 'Interpolate' which the Unity documentation mentions. This fixed the head rotation shudder completely.

    Unfortunately this fix has caused an issue with the raycast from the camera / crosshair. Now the crosshair flickers badly as the engine (I am guessing here) interpolates the rigid bodies position and the raycast misses / hits the object intermittently. If I turn the Rigid Body Interpolate method to 'none' it will work perfectly again.

    Any idea how to solve this issue?

    Thanks!!
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    When a Raycast is thrown from inside a collider (such as the vehicle collider) then the expect behavior is this collider to be ignored by the raycast. However, this doesn't always work when the collider belongs to a rigidbody with interpolation enabled.

    I had a similar situation and solved it by forcing the Raycast to explicitly ignore the vehicle collider. You can do this by setting the layer of the GameObject containing the collider to 2. This is the "IgnoreRaycast" layer: any collider belonging to this layer is ignored by Raycast queries. Thus, the vehicle collider won't interfere with the Raycast being thrown from inside it, even with interpolation enabled.

    Note that if you want other Raycast queries to intercept the vehicle (i.e. other Raycasts thrown from outside) then you have to restore the original layer after throwing the first Raycast. Something like this:

    Code (CSharp):
    1. int prevLayer = collider.gameObject.layer;
    2. collider.gameObject.layer = 2;
    3. // (do the raycast here)
    4. collider.gameObject.layer = prevLayer;
     
  3. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    That didn't work unfortunately. Setting the parent gameobject (The Vehicle Rigid Body) and any associated mesh colliders to the ignore ray cast layer and had no effect on the flicker / camera forward ray casts...

    To Solve it I created a new gameobect (Trigger Group) in the same location as the parent group (Vehicle Group). Then I moved any trigger from the Vehicle Group to the Trigger Group. Finally I made a script move the Trigger Group to the Vehicle Group on Update(). No more flicker. Hate the hack though.
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    For objects which have interpolation and you want to cast from inside reliably, you should be using rigidbody.position as this is the "true" position of the object as far as physx is concerned instead of .transform (which holds interpolated position).
     
  5. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    @hippocoder: So the parent object has a rigid body but the nested object, the camera, doesn't have a rigid body attached. I use the camera's position for the ray cast. To fix it, am I to calculate the camera offset of its nested position and cast from there then? Can I just add a rigid body to the camera and use that? The camera is 3-6 levels deep in the hierarchy, depending on platform.

    Thanks for any help you can provide!
     
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    If interpolation is being used, just sample the world space transform.position of the camera. Adding rigidbodies makes absolutely no sense. Turn on interpolation for the parent rigidbody so it's smooth.
     
  7. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    @hippocoder: I am using the camera's transform.position as the origin of the ray cast. There is no rigid body to reference for the ray casts using this method. The ray cast triggers are attached the to vehicle rigid body and they also have a rigid body. I am using interpolation on the vehicle rigid body. Flagging the nested vehicle trigger rigid bodies as interpolate has no impact on the issue.

    The ray cast hit / misses intermittently using this method. I can't see where I am going wrong?