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

[SOLVED]: Moving objects in VR with HMD

Discussion in 'AR/VR (XR) Discussion' started by Wicked_Manatee, Oct 30, 2016.

  1. Wicked_Manatee

    Wicked_Manatee

    Joined:
    Sep 30, 2016
    Posts:
    16
    Hey all,
    I've encountered this issue while trying to implement grabbing objects in game and can't figure out the problem here. I can move the objects around on the ground, but they do not move up into the air even if my gaze points straight up. It appears they are ignoring any Y movement, but I don't understand why.

    On my FixedUpdate for the PlayerController, I'm doing the logic.
    Code (CSharp):
    1. RaycastHit dotHit;
    2.         Vector3 rayDirection = mainCam.transform.TransformDirection(Vector3.forward);
    3.         Vector3 rayStart = mainCam.transform.position + rayDirection;
    4.         bool raycast = false;
    5.         GameObject obj = null;
    6.         // Triggers -- Grabbing dot
    7.         if (isGrabbing)
    8.         {
    9.             Grabbing();
    10.             if (Input.GetAxis("Right Trigger") == 0 || Input.GetAxis("Left Trigger") == 0)
    11.                 Drop();
    12.         }
    13.         else
    14.         {
    15.             if (Input.GetAxis("Right Trigger") > .1 || Input.GetAxis("Left Trigger") > .1)
    16.             {
    17.                 raycast = Physics.Raycast(rayStart, rayDirection, out dotHit, 5f);
    18.                 obj = dotHit.transform != null ? dotHit.transform.gameObject : null;
    19.                 if (obj != null && obj.GetComponent<Grabable>() != null) //Grabable is just an empty script added to objects that can be grabbed
    20.                 {
    21.                     obj.GetComponent<Rigidbody>().isKinematic = true;
    22.                     obj.GetComponent<Rigidbody>().useGravity = false;
    23.                     obj.GetComponent<DotController>().isBusy = true; //this tells the obj's AI to not move anymore
    24.                     grabableObj = obj;
    25.                     isGrabbing = true;
    26.                     manager.PlaySound("grunt");
    27.                 }
    28.             }
    29.         }
    And the Helper methods
    Code (CSharp):
    1.  
    2.  void Grabbing()
    3.     {
    4.         grabableObj.transform.position = Vector3.Lerp(grabableObj.transform.position,
    5.             mainCam.transform.position + mainCam.transform.TransformDirection(Vector3.forward) * 5,
    6.             Time.deltaTime * 4);
    7.         Vector3 forward = mainCam.transform.TransformDirection(Vector3.forward) * 5;
    8.         Debug.DrawRay(mainCam.transform.position, forward, Color.green);
    9.     }
    10.    
    11.     void Drop()
    12.     {
    13.         isGrabbing = false;
    14.         grabableObj.GetComponent<Rigidbody>().isKinematic = false;
    15.         grabableObj.GetComponent<Rigidbody>().useGravity = true;
    16.         grabableObj.GetComponent<DotController>().isBusy = false;
    17.         grabableObj = null;
    18.     }
    19.  
    The problem has to be with how I'm handling changing the grabbed object's position, right? The debugging ray looks to work fine, but the objects don't follow it.
    Let me know if you have ideas.
    Thanks
     
  2. Wicked_Manatee

    Wicked_Manatee

    Joined:
    Sep 30, 2016
    Posts:
    16
    There is something that is restricting Y movement... I don't have Y movement in my game usually so I'm assuming something I did earlier is stopping this...?

    I tried implementing it just off of a Ray. It SHOULD be moving upwards; I have the ray printing to console as well.
    move here! (8.8, -1.4, -3.8)
    UnityEngine.MonoBehaviour:print(Object)
    PlayerController:Grabbing(Ray) (at Assets/Scripts/PlayerController.cs:240)
    PlayerController:MapButtons() (at Assets/Scripts/PlayerController.cs:128)
    PlayerController:FixedUpdate() (at Assets/Scripts/PlayerController.cs:59)
    ..
    move here! (9.1, 2.4, -3.4)
    UnityEngine.MonoBehaviour:print(Object)
    PlayerController:Grabbing(Ray) (at Assets/Scripts/PlayerController.cs:240)
    PlayerController:MapButtons() (at Assets/Scripts/PlayerController.cs:128)
    PlayerController:FixedUpdate() (at Assets/Scripts/PlayerController.cs:59)
    ..
    move here! (9.0, 4.2, -4.4)
    UnityEngine.MonoBehaviour:print(Object)
    PlayerController:Grabbing(Ray) (at Assets/Scripts/PlayerController.cs:240)
    PlayerController:MapButtons() (at Assets/Scripts/PlayerController.cs:128)
    PlayerController:FixedUpdate() (at Assets/Scripts/PlayerController.cs:59)


    There is no Constraints on Y movement for the rigid bodies. Is there something in the NavMesh Agent of unity that is preventing this? Gravity gets turned off when I grab them.

    Still stumped.

    Edit: I edited the player's rotation in a manner that allowed Y movement, and the Player was able to "fly" upwards based on rotation. If an object was grabbed, it did not leave the floor to accompany player. If I just use the Unity Editor while game is running, I cannot move the objects on their Y axis either.

    Edit2: NavMeshAgent is indeed preventing the Y Movement. Just disabled it and tried... this built nav seems to be more trouble than its worth
     
    Last edited: Nov 5, 2016