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)));
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:
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.