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.