- Home /
How can I change the character beign controled and the camera with a button?
Hey folks, this is the first game I'm programming alone and here's the situation: I have 3 characters that need to be controled. It's an Isometric camera and you must switch the characers with buttons(not specific). There's already a camera following them but I can't figure how to change the camera without controlling all the characters at once.
In short, I'm looking for a way to control 3 characters separatedely, each one of the with a different camera.
OBS: C# codes wil help me more since I'm trying to learn it.
Answer by minefriek · Jun 04, 2014 at 03:11 AM
Well Im no expert in C#, though I've used it for a couple things, but I can show you what I did in javascript for an FPS/RTS game. the principals are basically the same
var cam1: Camera;
var cam2: Camera;
var cam3: Camera;
function Start() {
cam1.enabled = true;
cam2.enabled = false;
cam3.enabled = false;
Screen.lockCursor = true;
}
function Update() {
// First Person/Third Person Toggle
if (Input.GetKeyDown(KeyCode.PageUp) && (cam1.enabled == true)) {
cam1.enabled = false;
cam2.enabled = true;
cam3.enabled = false;
Screen.lockCursor = true;
}
else if (Input.GetKeyDown(KeyCode.PageUp) && (cam2.enabled == true)) {
cam1.enabled = true;
cam2.enabled = false;
cam3.enabled = false;
Screen.lockCursor = true;
}
// Strategy Toggle
if (Input.GetKeyDown(KeyCode.PageDown) && (cam1.enabled == true)) {
cam1.enabled = false;
cam2.enabled = false;
cam3.enabled = true;
GameObject.Find("Avatar").GetComponent(CharacterController).enabled = false;
Screen.lockCursor = false;
}
else if (Input.GetKeyDown(KeyCode.PageDown) && (cam2.enabled == true)) {
cam1.enabled = false;
cam2.enabled = false;
cam3.enabled = true;
GameObject.Find("Avatar").GetComponent(CharacterController).enabled = false;
Screen.lockCursor = false;
}
else if (Input.GetKeyDown(KeyCode.PageDown) && (cam3.enabled == true)) {
cam1.enabled = true;
cam2.enabled = false;
cam3.enabled = false;
GameObject.Find("Avatar").GetComponent(CharacterController).enabled = true;
Screen.lockCursor = true;
}
Do not just drag and drop this into a javascript and port it into your game
basically what your looking for to disable a character controller is the GameObject.Find part that toggles fps to rts in this case. However the code can be pretty much adapted to your needs.
The variables specified at the top of the code allow you to select the cameras you wish to switch between meaning theres less hassle setting everything up.
But the key thing here for you is the gameobject part. What you need to do is to is to enable or disable the character controller for your 3 characters depending on which camera is selected. This allows for you to control one character at a time instead of having the problem of all of them moving at once
I probably just confused you more but let me know if this helps.
I got the concept for it, Thanks! But this script goes to the cameras or the character?
Got it working now. Thanks. This is the code I used, for future references.
void Start() {
camera.enabled = true;
camera2.enabled = false;
camera3.enabled = false;
GameObject.Find("Filho").GetComponent<$$anonymous$$ovimentoFilho>().enabled = true;
GameObject.Find("$$anonymous$$ae").GetComponent<$$anonymous$$ovimentoPais>().enabled = false;
GameObject.Find("Pai").GetComponent<$$anonymous$$ovimentoPais>().enabled = false;
}
void Update() {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Alpha2) && (camera.enabled == true)) {
camera.enabled = false;
camera2.enabled = true;
camera3.enabled = false;
//Se a camera estiver no filho, vai para a $$anonymous$$ae
GameObject.Find("Filho").GetComponent<$$anonymous$$ovimentoFilho>().enabled = false;
GameObject.Find("$$anonymous$$ae").GetComponent<$$anonymous$$ovimentoPais>().enabled = true;
GameObject.Find("Pai").GetComponent<$$anonymous$$ovimentoPais>().enabled = false;
GameObject.Find("Filho").GetComponent<>() = false;
Your answer
Follow this Question
Related Questions
character switching on collision 1 Answer
switch character 1 Answer
setting a character controller orientation by script 1 Answer
How to switch between four characters when a button is pressed? 1 Answer
changing player (getting in vehicles) 3 Answers