【问题标题】:Unity falling platform passes through ground while falling统一落台落下时穿过地面
【发布时间】:2017-05-31 23:26:52
【问题描述】:

我有一个立方体,上面有一个盒子碰撞器和一个触发器。当玩家站在上面时,它会掉下来。

我希望平台在与某物碰撞后自行销毁,并在此之前在其起始位置实例化自身。

所以我的代码是这样的:

void OnTriggerEnter(Collider col)
{
    if (col.CompareTag("Player"))
        isFalling = true;
}

void OnCollisionEnter(Collision col)
{
    if (!col.gameObject.CompareTag("Player"))
    {
        Instantiate(gameObject, startPosition, startRotation);
        Destroy(gameObject);
    }
}

void Update()
{
    if (isFalling)
    {
        fallingSpeed += Time.deltaTime / 20;
        transform.position = new Vector3(transform.position.x, transform.position.y - fallingSpeed, transform.position.z);
    }
}

好吧,当我的平台坠毁时,它只是穿过地面。甚至没有检测到碰撞。

有人给我提示吗?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    所以我刚刚弄错了。

    平台没有连接刚体。因此它无法与地面发生碰撞。

    这是我的新代码:

    private void Start()
        {
            data.PlatformRigid.useGravity = false; // Disable the gravity to make it stay in the air
        }
    
        private void OnTriggerEnter(Collider col)
        {
            if (!data.Activated) // just do this 1 time 
            {
                if (col.CompareTag("Player")) // just start executing the following code if the colliding object is the player
                {
                    data.Activated = true; // don't execute this code a second time
                    data.PlatformRigid.useGravity = true; // start falling
                }
            }
        }
    
        private void OnCollisionEnter(Collision col)
        {
            if (!col.gameObject.CompareTag("Player"))
            {
                Instantiate(gameObject, data.StartPosition, data.StartRotation); // Create itself at default
                Destroy(gameObject); // Destroy itself
            }
        }
    

    我不再需要计算更新中的下降速度。当玩家撞击平台时,我只是禁用重力并启用它。

    【讨论】:

      【解决方案2】:

      如果您的对撞机设置为触发,它不会触发 OnCollisionEnter 事件。相反,将您的代码放在OnTriggerEnter 中,如下所示:

      void OnTriggerEnter(Collider col)
      {
          if (col.CompareTag("Player")) {
              isFalling = true;
          }
          else
          {
              Instantiate(gameObject, startPosition, startRotation);
              Destroy(gameObject);
          }
      }
      

      【讨论】:

      • 没有我的平台有一个对撞机和第二个带触发器的对撞机
      猜你喜欢
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多