1
votes

I'm making a game in unity htc vive. I wrote the below code and was expecting that whenever i click and hold on "trigger" button the debug messages would stop but they didn't! Any help is appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class move : MonoBehaviour
{
[SteamVR_DefaultAction("Squeeze")]
public static System.Random r = new System.Random(); 
public float speed = 2f;

public Transform s2;
private bool ifMoving;
public SteamVR_Action_Single squeezeAction;
public SteamVR_Action_Vector2 touchPadAction;

void Awake()
{
    s2 = GameObject.Find("Camera").GetComponent<Transform>();
}

void Start()
{
}

// Update is called once per frame
void FixedUpdate()
{
    if (SteamVR_Input._default.inActions.GrabPinch.GetStateDown(SteamVR_Input_Sources.LeftHand))
    {
        ifMoving = true;

    }
    else
    {
        ifMoving = false;
        Debug.Log("Hej " + r.Next(1000));

    }
    if (ifMoving)
    {
        Debug.Log("Hej " + r.Next(1000));
        transform.position += s2.forward * Time.deltaTime * speed;
    }
}
}
1
They don't stop since the else block I executed in every frame where you didn't change the triggers state to down. - derHugo
Thank you very much. It was a big help. - Quinhook

1 Answers

1
votes

GetStateDown()only returns true for the frame the button was pressed.

You want to use GetState() which returns trueas long as the button is pressed.