C# 如何让玩家在摔倒时死亡?
我正在开发一个关于团结的基本游戏来提高自己。这是一款基本的无止境跑步平台游戏 当玩家在地上时,如果你右击,它会跳;如果它不在地上,它会掉得更快 但我不知道如何让一名球员在无法抓住平台时摔倒而死。你能查一下我的密码吗?我试图找到一个“如果”命令来实现它C# 如何让玩家在摔倒时死亡?,c#,unity3d,C#,Unity3d,我正在开发一个关于团结的基本游戏来提高自己。这是一款基本的无止境跑步平台游戏 当玩家在地上时,如果你右击,它会跳;如果它不在地上,它会掉得更快 但我不知道如何让一名球员在无法抓住平台时摔倒而死。你能查一下我的密码吗?我试图找到一个“如果”命令来实现它 using JetBrains.Annotations; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngin
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");
}
}
}
}
使用JetBrains.annotation;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SceneManagement;
公共类玩家控制:单一行为
{
公共刚体2d rb;
公开审查;
公众浮标地面检查半径;
公共层码头;
私人住宅;
浮动时间;
公共浮点数MaxFallTime=7;
布尔普拉耶斯法林;
void Start()
{
rb=GetComponent();
}
无效更新()
{
rb.velocity=新矢量2(5,rb.velocity.y);
onGround=物理2D.重叠圆(地面检查位置、地面检查半径、whatIsGround);
CurrentFallTime+=Time.deltaTime;
if(Input.GetMouseButtonDown(0)和&onGround)
{
rb.velocity=新矢量2(rb.velocity.x,12);
}
if(Input.GetMouseButtonDown(0)&&&!onGround)
{
rb.velocity=新矢量2(rb.velocity.x,-10);
}
//我希望它死掉,并在超过当前时间时通过屏幕进入游戏
如果()
{
如果(CurrentFallTime>=MaxFallTime)
{
场景管理器。加载场景(“游戏结束”);
}
}
}
}
编辑:问题解决了!我简单地添加了“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");
}
}
}
使用JetBrains.annotation;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.SceneManagement;
公共类玩家控制:单一行为
{
公共刚体2d rb;
公开审查;
公众浮标地面检查半径;
公共层码头;
私人住宅;
浮动时间;
公共浮点数MaxFallTime=7;
布尔普拉耶斯法林;
void Start()
{
rb=GetComponent();
}
无效更新()
{
rb.velocity=新矢量2(5,rb.velocity.y);
onGround=物理2D.重叠圆(地面检查位置、地面检查半径、whatIsGround);
CurrentFallTime+=Time.deltaTime;
如果(每轮)
{
CurrentFallTime=0f;
}
if(Input.GetMouseButtonDown(0)和&onGround)
{
rb.velocity=新矢量2(rb.velocity.x,12);
}
if(Input.GetMouseButtonDown(0)&&&!onGround)
{
rb.velocity=新矢量2(rb.velocity.x,-10);
}
如果(CurrentFallTime>=MaxFallTime)
{
场景管理器。加载场景(“游戏结束”);
}
}
}
这是我现在才想到的,所以我不确定它的效果如何,但您可以创建一个单面平面碰撞器,并将其定位为跟随播放器的x和y坐标,但保持略低于地面高度,例如1unit。然后检查玩家何时与飞机相撞,如果飞机相撞,你就知道玩家摔倒了
因此,创建一个空的游戏对象并添加碰撞器,不需要任何网格属性,并将碰撞器触发器设置为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;
}
}
类似的东西应该可以使用。这是我刚刚想到的东西,所以我不确定它是否可以使用,但是你可以创建一个单面平面碰撞器,并将其放置在播放器的x和y坐标上,但要稍微低于地面的高度,例如1unit。然后检查玩家何时与飞机相撞,如果飞机相撞,你就知道玩家摔倒了 因此,创建一个空的游戏对象并添加碰撞器,不需要任何网格属性,并将碰撞器触发器设置为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;
}
}
类似的东西应该可以用。您好,我计划写一个if语句,而不是添加一个碰撞器。我的计划是添加类似的内容;if(Vector2(rb.velocity.x,-10)和onGround){if(CurrentFallTime>=MaxFallTime){SceneManager.LoadScene(“GameOver”);}}但是我不能在if语句中写向量:(你好,我计划写一个if语句而不是添加一个碰撞器。我的计划是添加类似的东西;if(Vector2(rb.velocity.x,-10)和onGround){if(CurrentFallTime>=MaxFallTime){SceneManager.LoadScene(“GameOver”);}}但是我不能在if语句中写Vector:(很高兴听到你修复了它!你能把修复作为答案(你能回答自己的问题)发布以便于参考吗?很高兴听到你修复了它!你能把修复作为答案发布(你能回答自己的问题)吗为了便于参考?