Search Unity

Question Fast moving object collision with Controller object

Discussion in 'XR Interaction Toolkit and Input' started by Benmaster, Dec 24, 2022.

  1. Benmaster

    Benmaster

    Joined:
    Aug 11, 2016
    Posts:
    39
    Im trying to solve a fast movement issue like i face in other projects, but is the first time I do for VR Controllers.
    I have a XR Origin, done with XR Interaction Toolkit 2.1.1
    In the LeftHand Controller I have a raket attached, this is set as Kinematic, 10 mass, Continius Dinamic
    Now I have a balls, with 10 mass, Non Kinematic, Non Gravity, Continius Dinamic

    Because i wan the balls to be physically moved and react "realistic", and because the raket dont have velocity (because is a Kinematic object attached to the XR Controller, and have no velocity) I add to the balls the velocity with a calculation in the raket:


    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         // Update the previous position variable with the current position
    4.         previousPosition = currentPosition;
    5.  
    6.         // Update the current position variable with the new position of the object
    7.         currentPosition = transform.position;
    8.  
    9.  
    10.         velocity = GetVelocity();
    11.         angularVelocity = GetAngularVelocity();
    12.  
    13.     }
    14.  
    15.    public Vector3 GetVelocity()
    16.     {
    17.         // Calculate the velocity of the object using the previous and current position
    18.         return (currentPosition - previousPosition) / Time.fixedDeltaTime;
    19.     }
    20.  
    21.     public Vector3 GetAngularVelocity()
    22.     {
    23.         // Get the angular velocity of the object using the Rigidbody component
    24.         return GetComponent<Rigidbody>().angularVelocity;
    25.     }
    26.  
    27.    void OnCollisionEnter(Collision collision)
    28.     {
    29.         // Get the collision point from the collision parameter
    30.         Vector3 collisionPoint = collision.contacts[0].point;
    31.  
    32.         //Debug.Log(collision.gameObject);
    33.         //Debug.Log(velocity);
    34.         collision.gameObject.GetComponent<Rigidbody>().velocity = velocity;
    35.         collision.gameObject.GetComponent<Rigidbody>().angularVelocity = angularVelocity;
    36.     }
    You can imagine that I have a problem.. when you hit to fast the ball with the raket, some times the OnCollisionEnter dont trigger, or trigger when the ball is some frames inside the raket model, this cause that some times the speed is not correctly transfered to the balls or directly the ball miss the hit.

    I try tweaking Prject Setting -> physics, change COntact offset to 0.001, Default Solver Iterations and Velocity Iterations to 200, but nothing helps, some times the balls dont get the real raket speed or directly the balls miss all event.

    I know the "fast" solution is make the objects bigger, but because the raket is attached to the XR Origin, when I change the XR Origin scale, the raket keep same scale, and the scale change dont help.

    Maybe my issue is the raket script I have? maybe is a better way to get the controller velocity to transfer to the balls on fast movements, but I found nothing about this.

    Thanks
     
  2. Benmaster

    Benmaster

    Joined:
    Aug 11, 2016
    Posts:
    39
    I forgot completly.. go to project setting- > Time -> Fixed Timestep to 0.005, now i can hit with any collider at any speed and work correctly