Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Vive Controllers Rigidbody::MovePosition

Discussion in 'AR/VR (XR) Discussion' started by Rogalog, Jul 18, 2016.

  1. Rogalog

    Rogalog

    Joined:
    Jun 18, 2016
    Posts:
    23
    Hi all,

    I'm creating a cricket type game using the HTC Vive and I'm having issues with the ball sometimes going through the bat.

    I've searched for quite a while and have discovered that it seems to be because the Vive controller's position is set using the transform, rather than using the Rigidbody::MovePosition functionality resulting in a 'teleporting' effect and the correct collision not occurring. Note: I have the cricket bat as a child of the controller.

    I found this post on the steam forums where one of the Fantastic Contraption developers replied recommending the modification of the SteamVR_TrackedObject script to use rigidbody movement instead of transform movement. However, I'm still very new to both Unity and VR...

    I've tried dozens of ways but I can't quite seem to get it working..

    Could anyone shed some light on how I could implement this so that my collision react appropriately?

    Thank you! :)
     
  2. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Have you tried not parenting it and calling MovePosition in LateUpdate on a script attached to the bat?
     
  3. scottunity

    scottunity

    Joined:
    Feb 1, 2014
    Posts:
    72
    RigidBody has 3 modes of collision detection as well. Discrete, continuous and continuous dynamic. If it's discrete and something moves by quickly then it won't see or calculate the inbetween positions.
     
  4. Rogalog

    Rogalog

    Joined:
    Jun 18, 2016
    Posts:
    23
    I experimented with some scripts but the main issue I had was I couldn't get the rotation correct. I tried using the controllers rotation with a specified offset but to no avail.. I will give this another shot today. If you have any tips in the meantime, please feel free to share! ;)

    Thank you; both the ball and the bat are on continuous dynamic, however I'm pretty sure it's due to the controller transform being modified directly rather than using the physics engine.

    EDIT: I /think/ it's working now. Here's a snippet of the code I used.

    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         if ((int)tObj.index != -1)
    4.         {
    5.             device = SteamVR_Controller.Input((int)tObj.index);
    6.  
    7.             // Make rigidbody follow controller.
    8.             rb.MovePosition(controller.transform.position);
    9.  
    10.             // Adjust rotation.
    11.             Quaternion newRotation = controller.transform.rotation * Quaternion.Euler(-90f, 0f, 0f);
    12.             transform.rotation = newRotation;
    13.         } else {
    14.             Debug.Log("Controller index not found.");
    15.         }
    16.     }
    I was trying this before but couldn't work out how to modify the rotation correctly (and still don't fully understand, but that's a lesson for another day). One thing I still don't understand why it's happening is the controller index not being found and consequently being set to -1 (None in the enum).

    Thank you for all your help :)
     
    Last edited: Jul 20, 2016
    scottunity likes this.