What you’ll likely need to do is to use OnTriggerEnter() and OnTriggerExit() to set a flag indicating whether the object is currently in the trigger, and then only execute your camera-switching code when that flag is true.
TheLlama, you’re calling your CamController every frame. Don’t quite see the point of creating a separate function in that case. Plus, you’re using an if/else statement when it’s not really necessary. Either way is fine, I guess. But to me, less is more.
Got ya. And you’re right, you don’t need to use Start() necessarily. You can enable one or the other camera in the inspector with the checkbox. If you don’t do that, however, both cameras will be enabled to start until you push the “C” key the first time (at least the way I wrote it, Might be a better way) Could cause an error.
And for Sebastiao97, a second boolean might work well on you platform trigger. In other words, you could create another boolean variable set to false,then set it to true when the player hits the platform in OnTriggerEnter(), then flip it to false again OnTriggerExit(). Then just plug the boolean into your if statement
var standingOnPlatform : boolean = false;
if (Input.GetKeyDown ("c") (standingOnPlatform))
{
//your code
}
Finally had time to sit down with this. Here’s the complete script I came up with. I’m not sure it’s what you want, but give it a try. Attach it to your “platform” game object and check the box “Is Trigger”. Drop your cameras into the variables in the inspector. When the player is on the platform and “c” is pressed, it should change cameras. As soon as the player leaves, it should change back. Sorry to take so long (full time job :()
Hope this helps.
var camera1 : Camera;
var camera2 : Camera;
private var changeCam: boolean = true;
private var onPlatform: boolean = false;
function Start ()
{
camera1.enabled = true;
camera2.enabled = false;
}
function Update () {
if (Input.GetKeyDown("c") onPlatform)
{
changeCam = !changeCam;
camera1.enabled = changeCam;
camera2.enabled = !changeCam;
}
}
function OnTriggerEnter (hit : Collider)
{
if (hit.gameObject.tag == "Player")
{
onPlatform = true;
}
}
function OnTriggerExit()
{
onPlatform = false;
camera1.enabled = true;
camera2.enabled = false;
changeCam = true;
}
You’re welcome. Hope it helps. By the way, make sure your player is tagged “Player” from the drop down menu in the inspector, otherwise the script won’t work.
Hmm. Perhaps you can post the entire script. I tested the script I wrote above and it works fine for switching between two cameras. You have two scripts here. Which are you using? I notice some basic errors such as misspelling Start and closing brackets on the functions too soon {} like that on your old script. You also have “Player” as “player” in the one I wrote. Caps matter in names.
Try copying and pasting the script I wrote above into an empty .js script in Unity. You only need this one script attached to your platform and it should work. Unity will try to compile the old script you wrote even if you don’t use it, so you’ll get error messages anyway. Try deleting the old one, or saving it outside of this project if you want to keep it. You don’t want scripts with errors sitting in your projects. I’ve had problems with that in the past (and desperate postings on this forum) until I figured that out.
I don’t think that “=” is the problem. It’s a bit hard to see from the screen shot.Perhaps you can copy and past your code into a code block here. (click the “go advanced” button below and you’ll see a # sign. Past your code, highlight it and click on that.)
I do notice that you have function Start() spelled function Star (). You’ll need to correct that. Also, go through and make sure that you use brackets where needed { and parentheses where needed (
I almost always get some sort of error when I first write code. It’s usually a parentheses that wasn’t closed or a bracket that wasn’t closed. Look for capital letters that shouldn’t be capped and visa versa. Check to make sure you use = for defining values and == for comparing values. These are the little things that will mess you up and throw an error. And make sure that all statements that need it end with ;
It looks like you changed all of your =='s to ='s, but there were only some of them that needed to be changed. (Short version: use ‘=’ for assignment and ‘==’ for equality comparison.)
Also, you can just paste your code and errors directly into your posts (using code tags where appropriate); you don’t need to go to all the trouble of linking an image.
There’s nothing obviously wrong with the script as far as I can see. Perhaps try using GetKeyUp (“c”) instead of GetKeyDown just to make sure it’s not something with a sticking key. If that doesn’t work, try Debug.Log () on the booleans to see which one is flipping. If your player is moving in and out of the trigger area, that would flip the camera back to the default (camera1). Put Debug.Log (onPlatform); after function Update() (inside the brackets) and see what you get.
var camera1 : Camera;
var camera2 : Camera;
private var changeCam: boolean = true;
private var onPlatform: boolean = false;
function Start ()
{
camera1.enabled = true;
camera2.enabled = false;
}
function Update ()
{
Debug.Log (onPlatform); [COLOR="lime"]// add this to your script[/COLOR]
if (Input.GetKeyDown("c") onPlatform)
{
changeCam = !changeCam;
camera1.enabled = changeCam;
camera2.enabled = !changeCam;
}
}
function OnTriggerEnter (hit : Collider)
{
if (hit.gameObject.tag == "Player")
{
onPlatform = true;
}
}
function OnTriggerExit()
{
onPlatform = false;
camera1.enabled = true;
camera2.enabled = false;
changeCam = true;
}
What I understand from your post is that the cameras are switching back and forth (camera1 then camera2) without you pressing the “c” button. Is that correct? It’s happening automatically?
If that’s true, it sounds like one of the true/false statements may be switching for some reason. By using Debug.Log(), you can test if this is what is happening. I pasted the code above and put a note where to place the Debug.Log. In the lower left hand corner of your console, you should see the word true or false when the player is on or off of the trigger. If it’s flipping back and forth, then this could be the problem.
If the cameras are switching when you hit “c”, the script is doing what it’s supposed to do. In this case, I’ve misunderstood what you intended. Perhaps you can clarify. And thank you for trying so hard in English. I know it’s hard to make us understand.
The problem is that you’ve put a semicolon ; after your if statement on line 16. You don’t want that there. There’s nothing wrong with line 18. It should be = not ==
if (Input.GetKeyDown("c") onPlatform) [COLOR="seagreen"]// no semicolon[/COLOR]
Often an error message is due to something a line or two above where it seems to be. You’ll begin to see that the more you work with Unity and Unitron.