【问题标题】:Unity Camera Change C#Unity 相机更改 C#
【发布时间】:2015-06-26 23:55:06
【问题描述】:

我有三个角色,每个角色都附有一个摄像头。默认情况下,它们是禁用的,但只有一个。我制作了一个角色选择器,应该可以更改它们。我有一个问题,我可以移动所选的一个,但是相机停留在最后一个。 这是脚本:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using UnityStandardAssets.Utility;


    public class GameManageer : MonoBehaviour {



        public Camera[] cams = new Camera[3];
        public Character CurrentCharacter;
        public List<Character> Characters = new List<Character>();
        public List<Item> AllItems;
        bool ShowCharWheel;
        public int SelectedCharacter;
        public int lastCharacter;
        public static GameManageer Instance;



        void Awake(){
            Instance = this;

            foreach (Character c in Characters){
                c.Instance = Instantiate(c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
                c.Instance.GetComponent<PlayerController>().LocalCharacter = c;
            }
            ChangeCharacter(Characters[PlayerPrefs.GetInt("SelectedChar")]);
        }


        // Use this for initialization
        void Start () {


        }

        // Update is called once per frame
        void Update () {


            if (Input.GetKey (KeyCode.C)) {
                ShowCharWheel = true;
            } else {
                ShowCharWheel = false;
            }



        }


        void ChangeCharacter(Character c){

            lastCharacter = SelectedCharacter;
            SelectedCharacter = Characters.IndexOf (c);

            cams [SelectedCharacter].gameObject.SetActive (true);
            cams [lastCharacter].gameObject.SetActive (false);
            CurrentCharacter = c;
            Characters [lastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
            Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;              
            PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);
        }



        void OnGUI(){
            if (ShowCharWheel) {


                GUILayout.BeginArea(new Rect(Screen.width - 64, Screen.height - 192,64,192));
                foreach (Character c in Characters){
                    if (GUILayout.Button(c.Icon,GUILayout.Width(64),GUILayout.Height(64))){

                        ChangeCharacter(c);
                    }

                }
                GUILayout.EndArea();
            }

        }
    }

    [System.Serializable]
    public class Character {

        public string Name;
        public Texture2D Icon;
        public GameObject PlayerPrefab;
        public GameObject Instance;
        public Transform HomeSpawn;
    }
    [System.Serializable]
    public class Item{
        public string Name;
        public Texture2D Icon;
        public ItemInstance InstancePrefab;
    }

【问题讨论】:

  • Camera.main = cams [SelectedCharacter]; --- 可能不起作用,但它可以带你到某个地方..
  • Camera.main 是只读的

标签: c# unity3d


【解决方案1】:

这应该可以完成工作:

cams[SelectedCharacter].enabled = true;
cams[lastCharacter].enabled = false;

【讨论】:

  • 相机没有这种方法
  • 你能检查cams[SelectedCharacter].camera.active = true;
  • 它本身就是一个相机,如果我想设置它,我必须使用 .gameObject.SetActive(true);但由于某种原因它不起作用
  • 你在哪里填充 cams 数组?
  • cams[lastCharacter].enabled = false; cams[SelectedCharacter].enabled = true;
【解决方案2】:

使用深度:

foreach (Camera cam in cams)
{
    cam.depth = cam == cams[SelectedCharacter] ? 10 : 0;
}

我认为这里真正的问题是,你必须在场景中管理更多摄像机,除此之外只有最后一个和当前选定的角色......在这种情况下:

foreach (Camera cam in cams)
{
    cam.SetActive(cam == cams[SelectedCharacter]);
}

【讨论】:

  • 我很抱歉,但它什么也没做
  • 您是否将所有不使用的相机设置为深度 0?
  • 我照你说的做了
  • 我不知道应该把它放在哪里
  • 而不是两行cams [SelectedCharacter].gameObject.SetActive (true);cams [lastCharacter].gameObject.SetActive (false);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多