首页 > 解决方案 > 如何让玩家在坠落时死亡?

问题描述

我正在开发一个基于 Unity 的基本游戏来提高自己。这是一个基本的无尽跑步平台游戏。

如果玩家在地上时右键单击,它会跳跃;如果它不在地面上,它会下落得更快。

但是我不知道如何让玩家在无法抓住平台时在坠落时死亡。你能检查我的代码吗?我正在尝试找到一个“if”命令来实现它。

using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerControls : MonoBehaviour
{
    public Rigidbody2D rb;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;
    private bool onGround;
    float CurrentFallTime;
    public float MaxFallTime = 7;
    bool PlayerIsFalling;


    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        rb.velocity = new Vector2(5, rb.velocity.y);
        onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);

        CurrentFallTime += Time.deltaTime;

        if (Input.GetMouseButtonDown(0) && onGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, 12);
        }

        if (Input.GetMouseButtonDown(0) && !onGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, -10);
        }

        // I want it to die and go to game over screen when it exceeds the CurrentFallTime
        if ()
        {
            if (CurrentFallTime >= MaxFallTime)
            {
                SceneManager.LoadScene("GameOver");
            }
        }      
    }
}

编辑:它解决了!我简单地添加了“if(onGround)”并重置了 CurrentFallTime。这是新代码:

using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerControls : MonoBehaviour
{
    public Rigidbody2D rb;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;
    private bool onGround;
    float CurrentFallTime;
    public float MaxFallTime = 7;
    bool PlayerIsFalling;


    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        rb.velocity = new Vector2(5, rb.velocity.y);
        onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);

        CurrentFallTime += Time.deltaTime;

        if (onGround)
        {
            CurrentFallTime = 0f;
        }

        if (Input.GetMouseButtonDown(0) && onGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, 12);
        }

        if (Input.GetMouseButtonDown(0) && !onGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, -10);
        }



        if (CurrentFallTime >= MaxFallTime)
        {
            SceneManager.LoadScene("GameOver");
        }

    }
}

标签: c#unity3d

解决方案


这是我现在才想到的东西,所以我不确定它的效果如何,但你可以创建一个单面平面对撞机并将其定位为跟随玩家的 x 和 y 坐标,但保持略低于地面高度,例如 1 个单位。然后检查玩家何时与飞机相撞,如果确实如此,那么您就知道玩家已经摔倒了。

因此,创建一个空的 GameObject 并添加对撞机,不需要任何网格属性并将对撞机触发器设置为 true。然后在播放器控件中添加类似更新的内容。

colliderGo.transform.location = new Vector3(transform.x, groundHeight - 1, transform.z)

同样在播放器控制器功能中

function onTrigger(Collider col) {
    if (col.tag == "fallDetector") {
        Debug.Log("What a cruel world")
        playHasFallen = true;
    }
}

像这样的东西应该工作。


推荐阅读