- Home /
What's wrong with my if states of eulerAxis.x in Update?
I have:
public float moveAngleMin = 5.0f;
public float moveAngleMax = 45.0f;
public float runAngleMin = 355.0f;
public float runAngleMax = 315.0f;
and in the Update function:
void Update() {
// Check rotation of head in X axis
if (vrCamera.eulerAngles.x >= moveAngleMin && vrCamera.eulerAngles.x < moveAngleMax)
{
// Move forward
moveForward = true;
runForward = false;
}
if (vrCamera.eulerAngles.x <= runAngleMin && vrCamera.eulerAngles.x >= runAngleMax)
{
// Run forward
moveForward = false;
runForward = true;
}
else {
// Stop moving
moveForward = false;
runForward = false;
}
}
The fun fact about above "if"s is that they work... for 2nd if - my character is able to run when I move my head up. When moving below 5 - nope he can't. But after changing the order of this "if"s - he can move but can't run - I tried else if and other things - can anybody help? I'll upvote of course good answer.
Thank you in advance!
Answer by UnityCoach · Dec 07, 2016 at 12:26 PM
I may be mistaken, but I'm pretty sure you meant to write this :
if (vrCamera.eulerAngles.x >= runAngleMin && vrCamera.eulerAngles.x <= runAngleMax)
// instead of this
if (vrCamera.eulerAngles.x <= runAngleMin && vrCamera.eulerAngles.x >= runAngleMax)
Thank you for your quick reply but no - that part it's okay and it's working. The problem is that the only thing that's working is only 2nd part whatever I put there. Right now is that part about running. But when I move there statement about moving it's also working and run is not working. I'm pretty sure that my statements are defined correctly, bu maybe I should put them in one "if"? and later define speed by float?
The else statement is working as a charm and when I'm observing how VRCamera attached to the player is working it's correct (though it was some time I realised that in eulerAngles you don't have -45 but 315 ;) )
Sorry for the roughness of my draft but maybe it'll help better understand my problem and what I'm trying to achieve.
I'm using Unity 5.4 btw.
Even thought it's not the problem, you may want to use Finite States to simplify things a little. Also, if they're mutually exclusive, you can use else if, to make sure one prevails on the other.
Like :
public enum STATE {Idle, $$anonymous$$oving, Running};
private STATE _state;
private STATE State
{
get {return _state;}
set
{
if (_state != value)
{
_state = value;
}
}
}
void Update() {
// Check rotation of head in X axis
if (vrCamera.eulerAngles.x >= moveAngle$$anonymous$$in && vrCamera.eulerAngles.x < moveAngle$$anonymous$$ax)
{
State = STAT$$anonymous$$$$anonymous$$oving;
}
else if (vrCamera.eulerAngles.x <= runAngle$$anonymous$$in && vrCamera.eulerAngles.x >= runAngle$$anonymous$$ax)
{
State = STAT$$anonymous$$Running;
}
else {
State = STAT$$anonymous$$Idle;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Look Rotation flipping object rotation after 180 degrees 1 Answer
Draw texture directly on Oculus screen 1 Answer
Enemy should follow the Player if he is in range, but how? 2 Answers