How to pass a Vector3 from inside an IF statement to another

Hi, I’ve only been using Unity and writing code for a few months but I have an issue I can not seem to get around.

I am attaching a script to a Player object (Steam VR) to enable flying.
The ability to fly will be enabled when the left and right Grips are pressed in initially.
The flying direction is then a difference between the initial right controller vector when the grip is initially pressed and the location of the controller monitored through the Update function, until the Grip is released.
I am also monitoring the Players Y height when flying is initialised so the they will be returned to that height when the Grips are let go.

The problem I have is with the controller positions. I can grab the initial position and then set a boolean to confirm I have that initial Vector 3 and that is good.
I then say if the boolean is true the take a difference between the initial controller Vector and current controller Vector to enable a transform.position on the player.
In my second “IF” statement the value that was set in the initial IF statement is not available.

A web search says I should declare the Vector3 variable as private at the start of the script, which only stops script errors. Because I set it globally and it cannot get the actual value into the flying logic it defaults to (0,0,0)!!

So, in my script the Vector3 of the initial controller position is set in variable “rightConInit”. The scrip error says that in the second IF statement this variable .does not exist. How do I pass this value over?

    // Set initial Player Y height and right controller vector
    // when flying mode is enabled for first time
    if (leftGrip == true && rightGrip == true && isInitSet == false)
    {
        // Grab controller and head Vector3
        SteamVR_Action_Pose[] poseActions = SteamVR_Input._default.poseActions;
        Vector3 rightConInit = poseActions[0].GetLocalPosition(SteamVR_Input_Sources.RightHand);
        print("FirstRight Controller Initial is at = " + rightConInit);
        Vector3 playerPosInit = head.localPosition;
        print("Head Initial Vector is at = " + playerPosInit);
        headY = playerPosInit.y;
        print("Head Y position is = " + headY);
        isInitSet = true;
        print("isInitSet value is now at = " + isInitSet);
    }
    else
    {
        isInitSet = false;
        print("isInitSet value has now dropped to = " + isInitSet);
    }

    // Capture right controller current vector, compare and move
    if (leftGrip == true && rightGrip == true && isInitSet == true)
    {
        SteamVR_Action_Pose[] poseActions = SteamVR_Input._default.poseActions;
        Vector3 rightConVec = poseActions[0].GetLocalPosition(SteamVR_Input_Sources.RightHand);
        print("Right Controller Vector Position is at = " + rightConVec);
        Vector3 movement = rightConVec - rightConInit;
        print("Movement Vector is = " + movement);
        ////body.AddForce(movement * 60f * triggerValueLeft);
        transform.position += movement * decel;
    }
    else
    {
        isInitSet = false;
    }

Thanks.
Craig.

Hello, you need to create the var in the same space were you are using it, put this line just before both of your ifs

Vector3 rightConInit;

and remove the vector 3 from inside your first if

rightConInit = poseActions[0].GetLocalPosition(SteamVR_Input_Sources.RightHand);

You can clean that up quite a bit. I didn’t see a use for your head location, so that was removed for now:

// Movement variables
private SteamVR_Action_Pose[] poseActions;
private Vector3 rightGripInitial = Vector3.zero;
private Vector3 rightGripCurrent = Vector3.zero;

void Update()
{
    // Shared across branches
    SteamVR_Action_Pose[] poseActions = SteamVR_Input._default.poseActions;

    // Check if we're gripping both
    if (leftGrip == false || rightGrip == false)
    {
        isInitSet = false;
        return;
    }

    // Haven't enabled flying and both grips are active
    if (isInitSet == false) {

        // Our initial grip with the right controller
        rightGripInitial = poseActions[0].GetLocalPosition (SteamVR_Input_Sources.RightHand);
        isInitSet = true;

    }

    // Enabled flying and both grips are active
    if (isInitSet == true) {
        
        // Our current grip with the right controller
        rightGripCurrent = poseActions[0].GetLocalPosition (SteamVR_Input_Sources.RightHand);
        Vector3 rightGripDelta = rightGripCurrent - rightGripInitial;
        
        // Move based on the difference between current and initial
        transform.position += rightGripDelta * decel;
    }
}

Let me know if this is closer to your desired behavior.

You can clean that up quite a bit. I didn’t see a use for your head location, so that was removed for now:

// Movement variables
private SteamVR_Action_Pose[] poseActions;
private Vector3 rightGripInitial = Vector3.zero;
private Vector3 rightGripCurrent = Vector3.zero;

void Update()
{
    // Shared across branches
    SteamVR_Action_Pose[] poseActions = SteamVR_Input._default.poseActions;

    // Check if we're gripping both
    if (leftGrip == false || rightGrip == false)
    {
        isInitSet = false;
        return;
    }

    // Haven't enabled flying and both grips are active
    if (isInitSet == false) {

        // Our initial grip with the right controller
        rightGripInitial = poseActions[0].GetLocalPosition (SteamVR_Input_Sources.RightHand);
        isInitSet = true;

    }

    // Enabled flying and both grips are active
    if (isInitSet == true) {
        
        // Our current grip with the right controller
        rightGripCurrent = poseActions[0].GetLocalPosition (SteamVR_Input_Sources.RightHand);
        Vector3 rightGripDelta = rightGripCurrent - rightGripInitial;
        
        // Move based on the difference between current and initial
        transform.position += rightGripDelta * decel;
    }
}

Let me know if this is closer to your desired behavior.