我正在开发有关Unity的基本游戏以提高自我。这是一个基本的无尽的跑步平台游戏。
如果玩家在地面上单击鼠标右键,它将跳;如果它不在地面上,它的下落速度会更快。
但是我无法弄清楚当玩家无法抓住平台时如何使其摔倒而死。您能检查一下我的密码吗?我正在尝试找到一个“ if”命令来实现它。
Release
编辑:解决了!我简单地添加了“ 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 (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");
}
}
}
}
答案 0 :(得分:0)
这是我现在刚刚想到的,因此我不确定它的效果如何,但是您可以创建一个1面平面对撞机,并将其定位为跟随玩家的x和y坐标,但保持略低于地面高度,例如1个然后检查玩家何时与飞机相撞,如果确实如此,那么您就知道玩家已经摔倒了。
因此,创建一个空的GameObject并添加对撞机,不需要任何网格属性,并将对撞机触发器设置为true。然后在播放器控件中添加诸如update之类的内容。
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;
}
}
类似的事情应该起作用。