- Home /
Character freezes when I change camera
Hello.
Yesterday I was trying to figure out how to play with cameras ingame.
The idea was to make it so the camera switches whilst playing like in Resident Evil games, when the character steps into an area, the camera will switch to either a more dramatic view or just a more comfortable view of the area, say for when the character goes behind a wall or into a different section of the room.
The problem I was having is that I want 2 types of cameras, one that follows the character (in third view), and another type that will be static.
My main camera follows the character in an isometric view, so the player can't rotate the camera, so, when they go behind a wall or object, I want the camera to switch, and when they step out of that area I want it to go back to the isometric view. I hope its not confusing to understand.
So, I followed some tutorials and my isometric camera is working perfectly. Then I made a test to switch cameras. So now I have 2 cameras in my Project view, "CharacterCamera" is the camera that's following the character, and "StaticCamera" is the one that is locked into position looking at a certain area of the field.
I then made a script that I attached to both cameras, what it does is, upon pressing the keyboard key "E", the "CharacterCamera" is disabled and the "StaticCamera" is enabled, using a script I found on this website:
GameObject.Find("CharacterCamera").GetComponent("Camera").active = false;
GameObject.Find("StaticCamera").GetComponent("Camera").active = true;
So, it all works out, however there's a problem, my camera does change, however my character stops moving and I get a error on the Console Log, I didn't write the error down, but it had something to do with the ThirdPersonController.Update() and what not, the idea I got was that now the game does not know how to follow the character and it gives an error making the game crash, I couldn't move at all, but the character was now in the "StaticCamera" view as I wanted it to.
I know I should post the error but I forgot to note it down and I'm at work at the moment so can't provide it at the moment, but this is all I did, hopefully someone has run into this issue and can help?
Update:
This is the error I'm getting upon changing cameras:
NullReferenceException ThirdPersonController.UpdateSmoothedMovementDirection () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/ThirdPersonController.js:130) ThirdPersonController.Update () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/ThirdPersonController.js:304)
Answer by uvavoo · Dec 06, 2012 at 03:24 PM
I think the error is deactivating the main camera. Set up a trigger where when the player enters it the camera switches and when the player leaves it switches back to the main camera. It is important to set the temporary cameras 'depth' to a higher number than 0. This will put the new cameras view above the main cameras, the character and main camera should continue to function as normal.(see screenshot)
Use something like the following code. using UnityEngine; using System.Collections;
public class SwitchCam : MonoBehaviour
{
void OnTriggerEnter ( Collider other )
{
if (other.gameObject.tag == "Player")
{
{
GameObject Tcam6 = GameObject.Find("temp_Camera6");
Tcam6.camera.enabled = true;
}
}
}
void OnTriggerExit ( Collider other )
{
if (other.gameObject.tag == "Player")
{
{
GameObject Tcam6 = GameObject.Find("temp_Camera6");
Tcam6.camera.enabled = false;
}
}
}
}
Answer by FuzzyLuke · Dec 06, 2012 at 08:19 PM
Thank you for the help, that did the trick.
And even bigger thanks for explaining triggers to me! :)