【问题标题】:How do i make a toggle system in Unity如何在 Unity 中制作切换系统
【发布时间】:2021-12-28 02:45:42
【问题描述】:

我正在制作一个相机切换系统来从第三人称模式切换到第一人称模式,但是当我在编辑器中进行测试时它会更改为第三人称,但是我无法切换回第一人称模式,我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,它将进入第三人称模式,但如果再次按下它,它将返回第一人称模式

【问题讨论】:

  • 逻辑有问题。将if (isTPMenabled == true) 包裹在if (isTPMenabled == false) 中很奇怪,因为它永远不会被调用。添加调试行会让你更容易调试。

标签: c# unity3d


【解决方案1】:

这是修改后的代码:

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
}

这样的逻辑更加简洁明了。

【讨论】:

  • 我明白了,我不知道如何添加 else 语句,非常感谢?
【解决方案2】:

在我看来,添加到this answer 会更干净

// If possible best already reference this via the Inspector
[SerializeField] private GameObject playerCamera;

private void Start ()
{
    // as a Fallback do this only ONCE
    if(!playerCamera) playerCamera = GameObject.Find("Player Camera");
}

void Update()
{
    // NOTE: You most probably do not want to toggle this EVERY FRAME
    // while the key stays pressed! You rather want to toggle ONCE per key press
    if (Input.GetKeyDown(KeyCode.F5))
    {
        // simply toggle the flag
        isTPMenabled = !isTPMenabled; 
        // since this is a single value assignment I'd prefer a ternary 
        playerCamera.transform.position = isTPMenabled ? transform.position + new Vector3(-2, 2, 0) : transform.position;
    }
}

如果您只是将相机设置为玩家的孩子(如果不是这样的话)并且只在 本地 空间上操作

,它会变得更容易
playerCamera.transform.localPosition = isTPMenabled ? new Vector3(-2, 2, 0) : Vector3.zero;

这样做的巨大优势是它还考虑了旋转等,而上述尚未考虑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2012-07-14
    • 2020-01-31
    • 1970-01-01
    相关资源
    最近更新 更多