- Home /
user input
My code involves me waiting for user input. The code cannot progress unless the trigger is clicked on the vive controller. However, when I try to incorporate a while loop, unity crashes.
void ThrowBall()
{
trackedController = GameObject.FindWithTag("Right Controller").GetComponent<SteamVR_TrackedController>();
ballscript = GameObject.FindObjectOfType(typeof(BallScript)) as BallScript;
if (ballscript.getLocation() == 2)
{
self.StopPlayback();
player1.StopPlayback();
player2.StopPlayback();
self.Play("idling");
player1.Play("idling");
player2.Play("idling");
while (trigger == false)
{
int x = 0;
x = x + 1;
Debug.Log(x);
}
}
ballscript.ReleaseMe();
}
would i be able to check for a trigger input in update? would that work?
Or actually let me just explain the whole thing to you. I have a little ball throwing script with 2 automated people and a real person. I want the people to idle while the real person doesnt hit the trigger yet. When the trigger is hit, I want the animations to continue
Unfortunately i can't remember how the input works in Vive... it's been a few months. But I'm sure some method gets called or some boolean gets set to true when a trigger is pressed.
The animation system of Unity makes it pretty easy to make a "state machine". You don't have to do anything special, like go into a loop, if you want to wait for stuff to happen.. Just set your animations to idle when the game starts (if i understand correctly) and set up the animation controller so that the animations stay idle until, where ever you react to player input, you tell the animator to go into the next animation. Then stay there until something else happens that requires an animation change.
You are now telling the animator to play the "idling" animation. If the animation changes to something else without you asking it to, it's a problem in the animator's transition conditions.
Btw: Unity did not crash but hang / got stuck in your loop. Just wanted to put the ter$$anonymous$$ology right -.-
Answer by NoseKills · May 10, 2017 at 03:16 PM
Unity apps are single threaded programs unless you make new threads yourself. Each thread executes the code line by line by line (simplification). Since you got only one thread, the execution of code can only be happening at a singular point at any given time. So when you have a loop like this
while (trigger == false)
{
int x = 0;
x = x + 1;
Debug.Log(x);
}
those are the only 3 lines of code that are executing in the whole app. That's including the code in Unity's engine where animations are played, screen is redrawn, sounds are played, and most importantly in your case, input is gathered. Unity can't even display the Debug.Logs since it's stuck in the loop (since you reintroduce int i every time in the loop, it would print out 1 every time anyways)
Nowhere inside that loop you give trigger
a chance to change its value so the loop could ever end.
You don't say what it is you actually want to do in that loop/instead of the loop, but whatever it is, you have to enable changing trigger back to true inside the loop. Or setting a proper start condition, doing your stuff in Update while that lasts, and making another condition for stopping doing it.
If what you want to do requires the screen to be redrawn after each change (animating a position or such), you'll have to do it the latter way (no loop, just do stuff each Update as a condition is met) because a tight loop won't allow it.
Your answer
Follow this Question
Related Questions
how to make gaze input on vr cardboard has 3 seconds to respond command 0 Answers
VR Navigation ala TiltBrush or Quill 1 Answer
SteamVR 1.2.0 - only first two connected controllers give button input 0 Answers
Getting started with VR (Vive) and unity 5.5.0 0 Answers
How do I determine the rotation of the XR unity controller if it is held upright or held facing down 2 Answers