Search Unity

Restrict the movement of the object attached to the controller in Oculus Go

Discussion in 'AR/VR (XR) Discussion' started by Arsandis, Nov 7, 2018.

  1. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    20
    Hey,
    I have written a script to pick an object with a rigidbody component by the ray casted from the controller and move it around. Now, I want to limit the movement of the object. After parenting the object to the controller, i want it to move in just one direction, e.g. along the x-axis only. I tried doing it by freezing the object's position in its rigid body component, however, it as no effect, when the controller is attached to the object.

    If anyone could help me with this, I would greatly appreciate it!
    Thanks in advance.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well if you're moving it with code, you can limit it with code, too. Just zero out the Y and Z components of the position delta (or equivalently, reassign these to their original values after each position change).
     
  3. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    20
    Hey,
    I am just using this code to parent it to the controller and then it moves along the contoller.

    Code (CSharp):
    1. public virtual void Store(Transform NewParent)
    2.     {
    3.         //The following stops the object being effected by physics while it's in the players hand
    4.         rb.isKinematic = true;
    5.         //And fixes it to the new parent it is given by the player script to follow.
    6.         transform.parent = NewParent;
    7.         //It then resets it's position and rotation to match it's new parent object
    8.         //transform.localRotation = Quaternion.identity;
    9.         //transform.localPosition = Vector3.zero;
    10.     }
    I am not sure how to limit it here.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Ah, well you can't do it that way. What you should to instead is:

    1. When the object is grabbed, get the position of the object relative to the controller, using something like controller.transform.InverseTransformPoint(object.position), and store this in a property.
    2. In LateUpdate (after the controller has moved), calculate the new world position of that property value, using controller.transform.TransformPoint(pointYouStoredInStep1).
    3. Use just the X value of the result to set the position of your grabbed object.
     
  5. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    20
    Thanks a lot man.
    I will give it a go!
     
    JoeStrout likes this.