Search Unity

Gear VR Touchpad FPS Script

Discussion in 'AR/VR (XR) Discussion' started by Harald_Heide, Dec 10, 2015.

  1. Harald_Heide

    Harald_Heide

    Joined:
    Jul 22, 2015
    Posts:
    81
    Hi,
    If someone could refine this script it would be appreciated.

    It's attached to an empty player gameobject
    with a camera and an empty called Body beneath.
    The camera is where you would attach a gun or something
    and the body is where your graphcis of shoes or whatever goes.

    Tap and hold the touchpad and you can move forward along the camera.forward direction or backwards
    along same vector.

    But I guess it got room for refinement. :)
     

    Attached Files:

  2. kavs

    kavs

    Joined:
    Jun 29, 2012
    Posts:
    25
    I'm looking for something similar, so I'll post what I have here. This uses deadzones to try and reduce some of the sensitivity, and it works if you 'scroll' to move in either direction.

    I want to move the player forward if they are touching 'up' on the GearVR touchpad that frame. What do I need to change/add? It doesn't look like the GearVR keeps track of absolute locations on the track pad, so do I need to look for swipes then hold velocity until the key is released?

    Code (csharp):
    1.  
    2. void Update()
    3.     {
    4.         // Grab this frame's input from the GearVR pad.
    5.         float moveX = Input.GetAxis("Mouse X");
    6.         float moveY = Input.GetAxis("Mouse Y");
    7.  
    8.         // Define local vectors based on where I'm facing
    9.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    10.         Vector3 backward = transform.TransformDirection(Vector3.back);
    11.         Vector3 left = transform.TransformDirection(Vector3.left);
    12.         Vector3 right = transform.TransformDirection(Vector3.right);
    13.  
    14.         // Initialize movement for this frame; default to nothing
    15.         Vector3 movement = Vector3.zero;
    16.  
    17.         // Handle X translation
    18.         if (moveX > deadzoneX)
    19.         {
    20.             movement += left;
    21.         }
    22.         else if (moveX < -deadzoneX)
    23.         {
    24.             movement += right;
    25.         }
    26.  
    27.         // Handle Y translation
    28.         if (moveY > deadzoneY)
    29.         {
    30.             movement += forward;
    31.         }
    32.         else if (moveY < -deadzoneY)
    33.         {
    34.             movement += backward;
    35.         }
    36.  
    37.         // Move!
    38.         controller.SimpleMove(movement * speed);
    39.     }
    40.  
     

    Attached Files:

    Last edited: Dec 13, 2015
  3. kavs

    kavs

    Joined:
    Jun 29, 2012
    Posts:
    25
    I ended up going with something like this as I don't need the touchpad for tapping yet. This moves you forward assuming the trackpad is held down. This works really well for exploratory experiences that don't require other input.

    Any advice on how to put a better curve on acceleration/deceleration or how to more cleanly clamp values would be appreciated. :)


    SIMPLE MOVEMENT:
    Code (csharp):
    1.  
    2. void ForwardMovement()
    3.     {
    4.         //Define the forward vector using your facing direction
    5.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    6.  
    7.         // If the touchpad is being held down, move forward in the direction you are facing.
    8.         if (Input.GetMouseButton(0))
    9.         {
    10.             controller.SimpleMove(forward * currentSpeed);
    11.         }
    12.     }
    13.  
    WITH ACCELERATION:
    Code (csharp):
    1.  
    2.     float currentSpeed = 0.0F;
    3.     public float maxSpeed = 1.0F;
    4.     public float acceleration = 0.15F;
    5.  
    6. void ForwardMovement()
    7.     {
    8.         //Define the forward vector using your facing direction
    9.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    10.  
    11.         // If the touchpad is being held down, move forward in the direction you are facing.
    12.         if (Input.GetMouseButton(0))
    13.         {
    14.             currentSpeed += acceleration;
    15.             currentSpeed = Mathf.Clamp(currentSpeed, 0.0F, maxSpeed);
    16.  
    17.             controller.SimpleMove(forward * currentSpeed);
    18.         }
    19.  
    20.         // If the touchpad was released, stop movement and reset velocity.
    21.         if (Input.GetMouseButtonUp(0))
    22.         {
    23.             currentSpeed = 0.0F;
    24.         }
    25.     }
    26.  
     
  4. kavs

    kavs

    Joined:
    Jun 29, 2012
    Posts:
    25
    I've cleaned up my acceleration script a bit and threw some comments in there. Attaching it here so anyone can use it or improve on it. I'd love to add acceleration/deceleration on a curve along with clean up the clamping code, so let me know if you have ideas!
     

    Attached Files:

  5. Harald_Heide

    Harald_Heide

    Joined:
    Jul 22, 2015
    Posts:
    81
    This is more of a static speed thingie but with increasing speed when swipe forwards or decrease when swipe backwards.
    Create an empty and name it "OVR Player" with a capsule collider, rigid body and attached script. Drag the prefab MainCamera from the VR Samples unity package (Asset store thingie) beneath "OVR Player" and also drag it into the scripts public variables for VR Input and Cam.
    Then drag Rigid body component into Rigidbody public variable on "OVR Player". Also beneath the First empty create an empty sibling of the MainCamera prefab called Body. Guns or whateva should be attached beneath the MainCamera (Sibling to VRCameraUI) and body graphics feet and stuff could be attached beneath the empty called body. When you start "walking" by swiping forward on touchpad, Body should adjust to same direction. You steer by looking at your destination of choice. Beware of the ridge of your plane or you might fall an fall and fall down deep into the abyss foreva, Happy Christmess...

    Oops! Forgot to mention you should probably Freeze all Rotations under Constraints on the RidgidBody or you might be in for one h... of a ride .... :)
     

    Attached Files:

    Last edited: Dec 14, 2015
    epionpion and Bentoon like this.
  6. Harald_Heide

    Harald_Heide

    Joined:
    Jul 22, 2015
    Posts:
    81
    Love your approach as well kavs.
    Nice to have a script for the Charactercontroller as well.
    I haven't quite figured out yet all pros and cons regarding Charactercontroller versus RigidBody.
     
  7. CManzione

    CManzione

    Joined:
    May 11, 2015
    Posts:
    7
    Hey Y'all,

    I've been looking for something exactly like this. Do I add this script to the OVR Player Controller? Also is there a way to test this without building to the phone every time?

    I'm assuming OVR_RB_Player.cs is the most current version
     
  8. FingerInd

    FingerInd

    Joined:
    Apr 5, 2016
    Posts:
    38
    Great script! works really well with a little demo I've got running.. maybe tapping and holding the direction would work better, and a double tap and hold to increase the speed? I'll try to figure out how to do that :)
     
  9. shimibs

    shimibs

    Joined:
    Jul 13, 2016
    Posts:
    3
    i am trying to get that script work, unfortunately i am not a big scripter, just a new guy that try to make his charcter move in the scene(i already made the navmash in the scene),does anyone can explain me how should i put that script and he will work ??
     
  10. danam

    danam

    Joined:
    Mar 30, 2015
    Posts:
    11
    This script worked well, how ever I noticed the MouseLook functionality does not work in Unity Editor mode. Forward and backward movement works in the Editor with A and D keys, S to stop. I was able to Get MouseLook in Editor mode by attaching the following script to the MainCamera, hold Atl to activate (disabled at runtime).
     

    Attached Files:

    Last edited: Nov 20, 2016
  11. Patchbristol

    Patchbristol

    Joined:
    May 17, 2015
    Posts:
    4
    Hello @kavs,

    I've implemented your cleaned up 'GVRPlayerMovement' script, and it works fine. I was wondering if you could help me adjust the script so I can make the player move backwards when hitting the back button on the touchpad? Would be great to add this feature!

    Many thanks and happy new year!

    Patrick
     
  12. ironsniper1

    ironsniper1

    Joined:
    Mar 18, 2017
    Posts:
    2
    the GVRPlayerMovement.cs script works great for moving forward but is there any way to make so i can move backward?
     
  13. grue19

    grue19

    Joined:
    Dec 21, 2017
    Posts:
    3
    hey wow this script is great!