- Home /
Question by
Englad · Jun 25, 2014 at 02:10 PM ·
raycastcameraswhilewhile-loop
While/if loop problem
I've been trying to work it out but i'm really struggling, the loop activates, the boolean (inCover) activates, but when you move away from the object that is raycasted, it doesn't switch to FPSCam again, and inCover doesn't turn back to false.
any help greatly appreciated
if (Input.GetKeyDown (KeyCode.C)) {//when the player pressess space
//check how far they are from the object
while (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.back), 1) & !inCover) {// cast a ray forward 10
Debug.Log ("There is something in front of the object!"); // display to the console then change to third person camera
FPSCam.enabled = false;
THIRDCam.enabled = true;
inCover = true;
Comment
Answer by HarshadK · Jun 25, 2014 at 02:15 PM
Considering your code is inside Update() function, do it like this:
if (Input.GetKeyDown (KeyCode.C)) {//when the player pressess space check how far they are from the object
{
if(Physics.Raycast (transform.position, transform.TransformDirection (Vector3.back), 1) && !inCover)
{// cast a ray forward 10
Debug.Log ("There is something in front of the object!"); // display to the console then change to third person camera
FPSCam.enabled = false;
THIRDCam.enabled = true;
inCover = true;
} else
{
FPSCam.enabled = true;
THIRDCam.enabled = false;
inCover = false;
}
}
Note: & is for bitwise operation and && is for 'AND'
Thank you, I had already figured it out and someone else pointed out to me about the & operator though your way looks better