Using VR controller's axis to move object over time

Hi.

I’m trying to create a script to move an object with the movement on the y-axis of right VR Vive controller.

Right now I have this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FN_Slide : MonoBehaviour
{
    private SteamVR_TrackedObject trackedObject;
    private SteamVR_Controller.Device device;

    // Use this for initialization
    void Start()
    {
        GameObject RController = GameObject.Find("Controller (right)");
        trackedObject = RController.GetComponent<SteamVR_TrackedObject>();
        device = SteamVR_Controller.Input((int)trackedObject.index);
        transform.localPosition = new Vector3(0, 0, 0);

    }

    // Update is called once per frame
    void Update()
    {

        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad) && device.GetAxis().x < 0.5 && device.GetAxis().x > -0.5)
        {
            transform.localPosition = new Vector3(0, 0, ((device.GetAxis().y - 1f)*(-0.025f)));
            //transform.localPosition = Vector3.Lerp(new Vector3(0, 0, 0), new Vector3(0, 0, 0.05f), ((device.GetAxis().y+1)/(-2)));

        }
        if (device.GetPressUp(SteamVR_Controller.ButtonMask.Touchpad))
        {
            transform.localPosition = new Vector3(0, 0, 0);
        }


        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad) && device.GetAxis().x < 0.5 && device.GetAxis().x > -0.5 && device.GetAxis().y < -0.75)
        {
            device.TriggerHapticPulse(10000);
        }
    }
}

The problem is that whenever I press the touchpad in the range of -0.5 to 0.5 on the x-axis the object moves to that position and stays there regardless of changes to the device.GetAxis().y positions.

The same thing happens with

transform.localPosition = new Vector3(0, 0, ((device.GetAxis().y - 1f)*(-0.025f)));

and with

transform.localPosition = Vector3.Lerp(new Vector3(0, 0, 0), new Vector3(0, 0, 0.05f), ((device.GetAxis().y+1)/(-2)));

I want it to move with the device.GetAxis().y changes (when I slide my finger on the touchpad).

Can anyone help me?

This should help - GitHub - cfkwok/ViveJoystick: A new way of movement using the HTC Vive controllers by emulating a joystick.

You are not moving the transform, you are positioning it. Is that what you want to do?
If not, you should do localPosition = localPosition + movement

I got something…
I used:

transform.localPosition = new Vector3(0, 0, (device.GetAxis().y - 1) * -0.025f);

and it moved ok with the touchpad, but there is a catch:
I want it to happen only when the touchpad is pressed and it’s x axis is between -0.5 and 0.5.

In order to get that effect I created a simple if() question:

if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad) && device.GetAxis().x < 0.5 && device.GetAxis().x > -0.5)
        {
            transform.localPosition = new Vector3(0, 0, (device.GetAxis().y - 1) * -0.025f);
        }

This code just moves it to the position of GetAxis().y and it stays there, even when GetAxis().y changes.

Why my transform.localPosition code works outside of the if question and not inside of it?

look at @whileBreak 's response.

I tried:

transform.localPosition = transform.localPosition + new Vector3(0, 0, (device.GetAxis().y - 1f) * -0.025f);

but I get the same effect as with:

transform.localPosition = new Vector3(0, 0, (device.GetAxis().y - 1f) * -0.025f);

becouse starting/resting position of that object is just 0,0,0.

Okay, so you don’t plan to move it continuously?

In regards to your issue from your previous post. I would suggest that you Debug.Log to check your values and parts of your if statement, just to be sure… if it’s behaving differently than you expect. Maybe you can narrow it down.

I want it to move continuously as long as the trackpad is pressed and device.GetAxis().x is between -0.5 and 0.5, but when those conditions are not met I want it to just jump to 0,0,0 and stay there regardless of device.GetAxis().y readings.

Did you Debug log the values?

I did and here are my results:

  1. I get good values of the device.GetAxis().y outside of the if question
  2. The If question itself works fine
  3. But inside of the question (when the conditions are met), I get one value and it is not updated.

Here is the exact code:

 Debug.Log("Ouside: "+device.GetAxis().y);
        if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad) && device.GetAxis().x < 0.5 && device.GetAxis().x > -0.5)
        {
            Debug.Log("inside"+device.GetAxis().y);
            transform.localPosition = new Vector3(0, 0, (device.GetAxis().y - 1f) * -0.025f);

        }

Ah, okay… so I just noticed something. Is there a GetPress (instead of GetPressDown)? If so, try that… re: not updating - meaning it only fires once?

2 Likes

OMG, You are right!

It does work now.

Thank you so much for noticing this detail!

Ah, cool :slight_smile: Sorry I didn’t notice it sooner, was getting confused lol.

Take it easy, enjoy your game :slight_smile:

1 Like