代码之家  ›  专栏  ›  技术社区  ›  ridhomblr

如何在Unity中创建切换系统

  •  0
  • ridhomblr  · 技术社区  · 3 年前

    我正在制作一个相机切换系统,从第三人称模式切换到第一人称模式,但是当我在编辑器中播放test时,它会变为第三人称模式,但是我不能切换回第一人称模式,我是Unity中的新手,我自己做的

    下面是摄影机切换脚本的代码

    {
    public bool isTPMenabled = false;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.F5))
        {
            if (isTPMenabled == false)
            {
                if (isTPMenabled == true)
                {
                    GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
                    isTPMenabled = false;
                    return;
                }
                GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x - 2, this.transform.position.y + 2, this.transform.position.z);
                isTPMenabled = true;
                return;
            }
        }
        if (Input.GetKey(KeyCode.F4))
        {
            GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
        }
    

    我的目标是,如果按F5一次,它将进入第三人称模式,但如果再按一次,它将返回第一人称模式

    1 回复  |  直到 3 年前
        1
  •  0
  •   Raptor    3 年前

    修订后的守则如下:

    void Update()
    {
        if (Input.GetKey(KeyCode.F5))
        {
            if (isTPMenabled == false)
            {
                GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x - 2, this.transform.position.y + 2, this.transform.position.z);
                isTPMenabled = true;
            } else { // means "isTPMenabled == true"
                GameObject.Find("Player Camera").transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z);
                isTPMenabled = false;
            }
        }
        // Omitted the unrelated F4 codes
    }