[C#][Animator][Solved] Camera switching function fails to work (code included)
I finally figured out how to make my camera dynamic during battle scenes using the animator.
From what I understand, having a function attached to the camera, and calling it using a trigger in the animation, is supposed to run that function,
I made 2 cameras. One is called MainCamera. The second is SecondCamera.
I created this script for switching the main camera to the second one:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class CamSwitch1 : MonoBehaviour
{
public Camera cam1;
public Camera cam2;
// Use this for initialization
void Switch ()
{
cam1.enabled = false;
cam2.enabled = true;
}
void Switchback ()
{
cam1.enabled = true;
cam2.enabled = false;
}
}
Camera switches fine, except camera 2 shows nothing but black, and plays no animation (thus cant call the switch to main).
What am I doing wrong? I want different cameras for different animations, and eventually want to code in a camera that is in a certain position when events (e.g. attack) are selected, but I cannot even switch to a second camera or animation.
I am aware using UnityEditor is probably unnecessary.
Check the camera's culling mask, location and rotation in Scene view (pause game, view that), etc.
I should have specified that the second camera was a clone of the first, with only a different tag, and later a different animation.
Answer by DiegoSLTS · Aug 30, 2015 at 02:29 AM
Is your SecondCamera gameobject active? The "enabled" property turns on and off a component (the "Camera" component in this example), not the actual game object. If SecondCamera is not active then turning on the Camera component won't do anything.
Try this:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class CamSwitch1 : MonoBehaviour
{
public GameObject cam1;
public GameObject cam2;
// Use this for initialization
void Switch ()
{
cam1.SetActive(false);
cam2.SetActive(true);
}
void Switchback ()
{
cam1.SetActive(true);
cam2.SetActive(false);
}
}
Also, I'd set the MainCamera tag for the current active camera so Camera.main keeps working.
Edit:
Realized more changed than just the cam*.enabled and re-attached the cameras to the script. Problem solved!
Thank you so much!!!!
Your answer
Follow this Question
Related Questions
Camera rotate X degree based on player rotate 0 Answers
Camera Rotation Switching Problem 0 Answers
Weird lightning, orthographic camera. Also not matching scene view. 0 Answers
Creating a camera controller for isometric view - Positioning the camera 1 Answer
Mouse Scroll not working on Camera (But can scroll the player) 0 Answers