【问题标题】:C# unity if statement logicC# unity if 语句逻辑
【发布时间】:2017-06-26 21:21:36
【问题描述】:

我最近开始学习用 C# 编写 Unity 代码;在以下脚本中,尝试根据鼠标的位置启用和禁用对象(敌人)。

问题是代码在启用对象时工作正常,但我不知道如何在它被激活后禁用它,以便对象在鼠标来回移动时出现和消失 - 进出范围。如果您有解决方案,请告诉我。谢谢!

using UnityEngine; 
using System.Collections;

public class Paddle : MonoBehaviour
{

    public GameObject enemy;

    // Use this for initialization
    void Start()
    {

        enemy.SetActive(false);
    }

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

        Vector3 paddlePos = new Vector3(8f, this.transform.position.y, 0f);

        float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;

        paddlePos.x = Mathf.Clamp(mousePosInBlocks, 6f, 8f);

        this.transform.position = paddlePos;

        if (mousePosInBlocks < 6f)
        {
            print("1");

        }
        else if (mousePosInBlocks <= 6.5f)
        {
            print("2");

            enemy.SetActive(true);


        }
        else if (mousePosInBlocks <= 7.5f)
        {
            print("3");

        }
        else
        {
            print("4");
        }
    }
}

【问题讨论】:

  • Start() 方法在带有对象的场景开始时自动调用。 Update() 每帧运行一次。当场景(或创建对象)开始时,您将禁用敌人,然后如果 mousePosInBlocks 等于或小于 6.5,则激活它。如果您不确定 mousePosInBlocks 的值,可以使用 Debug.Log(mousePosInBlocks)

标签: c# if-statement unity3d logic


【解决方案1】:

当您从不调用 enemy.SetActive(false); 时,您如何期望对象被禁用?它仅在您的 Start 方法中调用,但这将被 enemy.SetActive(true); 覆盖,Update 方法每帧都会调用它。

我不确定您的对象何时应该被禁用,但只需在相应的 if 语句中添加 enemy.SetActive(false);

【讨论】:

    【解决方案2】:

    您必须添加代码行以禁用更新类中的对象,因为您说当您将鼠标移出范围时应该禁用它,我猜该行应该进入您的 else 状态

    using UnityEngine; 
    using System.Collections;
    
    public class Paddle : MonoBehaviour
    {
    
        public GameObject enemy;
    
        // Use this for initialization
        void Start()
        {
    
            enemy.SetActive(false);
        }
    
        // Update is called once per frame
        void Update()
        {
    
            Vector3 paddlePos = new Vector3(8f, this.transform.position.y, 0f);
    
            float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
    
            paddlePos.x = Mathf.Clamp(mousePosInBlocks, 6f, 8f);
    
            this.transform.position = paddlePos;
    
            if (mousePosInBlocks < 6f)
            {
                print("1");
    
            }
            else if (mousePosInBlocks <= 6.5f)
            {
                print("2");
    
                enemy.SetActive(true);
    
    
            }
            else if (mousePosInBlocks <= 7.5f)
            {
                print("3");
    
            }
            else
            {
              enemy.SetActive(false);
                print("4");
            }
        }
    }
    

    在任何情况下,您只需要在 if、if-else 或 else 语句中添加enemy.SetActive(false);,确保它在其中,否则它将在每一帧中被调用并保持禁用状态

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 2021-01-08
      • 1970-01-01
      相关资源
      最近更新 更多