- Home /
2 questions: camera change and a question about using &&
Does anyone have a script that would allow me to change cameras? If not, could someone point me to a tutorial or something? Thanks. Now for the real question: I have a game where you are flying in a spaceship and when you fly too close to a planet it loads a level (namely the planet level). I used the AND operator for this: Code:
if ( transform.position.z >= 6088.709 && transform.position.z <= -5826.924
&& transform.position.x >= -5928.328 && transform.position.x <= 5926.727)
&& transform.position.y <= 5954.533 && transform.position.y >= -7152.666 )
{
Application.LoadLevel(1);
}
Translation: if the player's position is between z 6088.709 and -5826.924, x -5928.328 and 5926.727 and y 5954.533 and -7152.666, it loads level (1)
This method is clearly not working ( it gives the error "unknown identifier: &&" )
Is there a better way of doing this?
You have 2 closing parentheses after the if statement. transform.position.x <= 5926.727)
Answer by Jesse Anders · Nov 06, 2010 at 09:00 PM
It looks like you have an errant ')' in your conditional; that's probably what's causing the error to be generated.
As for better ways of doing it, a couple of alternatives would be to use a trigger, or to use the Bounds struct and the function Bounds.Contains().
Well that stopped the error... Its not doing what I wanted it to do still.
Answer by · Nov 07, 2010 at 04:37 AM
In addition to the errant ')', you've confused your <= and >= ... I formatted your code to make it easier to see.
if ( transform.position.z >= 6088.709 && transform.position.z <= -5826.924
// if z is greater than 6088 and less than -5826
&& transform.position.x >= -5928.328 && transform.position.x <= 5926.727
// and x is greater than -5928 and less than 5926
&& transform.position.y <= 5954.533 && transform.position.y >= -7152.666 )
// and y is less than 5954 and greater than -7152
{
Application.LoadLevel(1);
}
You need to flip the checks on 'z', and it should work. It'd be easier to spot this kind of error if you did your checks in the same order. i.e. "if x is greater than 1 and less than 2" and stuck to that format for y and z.
However, as Jesse said, there are better ways to implement this functionality, but this will fix your problem in the interim.
Your answer
