Search Unity

Low latency follow/attach to none rigid body.

Discussion in 'Physics' started by AndersMalmgren, Apr 26, 2016.

  1. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I'm experimentering with the Vive controllers. I have based my code on this repo

    https://github.com/TomorrowTodayLabs/NewtonVR

    It let you attach a rigid body to the controller (Which is a none rigid body). Se code below.
    The upside is that it completely behaves as a rigidbody while still being "attached" to the controller, it will not pass nor clip through other object with colliders etc, the downside is that it will not follow the controller in realtime, it will rubberband to the controller.

    Is there some middleway that will keep most of its behavior but update from the Update method instead of the FixedUpdate?

    Code (CSharp):
    1. protected override void FixedUpdate()
    2.         {
    3.             base.FixedUpdate();
    4.  
    5.             if (IsAttached == true)
    6.             {
    7.                 Vector3 PositionDelta;
    8.                 Quaternion RotationDelta;
    9.  
    10.                 float angle;
    11.                 Vector3 axis;
    12.  
    13.                 if (InteractionPoint != null)
    14.                 {
    15.                     RotationDelta = AttachedHand.transform.rotation * Quaternion.Inverse(InteractionPoint.rotation);
    16.                     PositionDelta = (AttachedHand.transform.position - InteractionPoint.position);
    17.                 }
    18.                 else
    19.                 {
    20.                     RotationDelta = PickupTransform.rotation * Quaternion.Inverse(this.transform.rotation);
    21.                     PositionDelta = (PickupTransform.position - this.transform.position);
    22.                 }
    23.  
    24.                 RotationDelta.ToAngleAxis(out angle, out axis);
    25.  
    26.                 if (angle > 180)
    27.                     angle -= 360;
    28.  
    29.                 if (angle != 0)
    30.                 {
    31.                     Vector3 AngularTarget = (Time.fixedDeltaTime * angle * axis) * AttachedRotationMagic;
    32.                     this.Rigidbody.angularVelocity = Vector3.MoveTowards(this.Rigidbody.angularVelocity, AngularTarget, 10f);
    33.                 }
    34.  
    35.                 Vector3 VelocityTarget = PositionDelta * AttachedPositionMagic * Time.fixedDeltaTime;
    36.              
    37.                 this.Rigidbody.velocity = Vector3.MoveTowards(this.Rigidbody.velocity, VelocityTarget, 10f);
    38.             }
    39.         }
     
  2. SlattyNaN

    SlattyNaN

    Joined:
    Mar 20, 2016
    Posts:
    2
    Last edited: May 1, 2016
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    No I have waited to fix this :D Really interested in a good solution, its crucial for our game (A shooter). The guy behind http://hotdogshorseshoesandhandgrenades.com/ seems to have solved it. Its alot less lag in his game (I have tried it).
     
  4. SlattyNaN

    SlattyNaN

    Joined:
    Mar 20, 2016
    Posts:
    2
    Yeah, plenty of games do it really well, I don't think it should be too tough, but there may be comprimises. Working on it now :), will update later today if I get anywhere.
     
  5. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Cool, you can set it to isKInimetic true and use the transofrm to move it without delay / FixedUpdate interpolation. It will still react to other rigid bodies, but I will not react to walls, floors and other none rigid bodies :/ If you come up with something please let me know
     
  6. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358