c# - How to constantly update stamina system
问题描述
I'm trying to make a stamina system where if the player holds Shift they lose stamina.
I have it where every time Shift is pressed they lose some but how do I make them constantly lose stamina?
For instance every second they lose 3 instead of only losing 3 on the key press.
Here's the current code I have for it.
//running
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = speed + run_speed;
Stamina = Stamina - 3;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = speed - run_speed;
Stamina = Stamina + 3;
}
解决方案
Based on your code you can declare a boolean bool isRunning
on the class and have it set to true
when you run.
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = speed + run_speed;
// Stamina = Stamina - 3;
isRunning = true;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = speed - run_speed;
// Stamina = Stamina + 3;
isRunning = false;
}
Add this to your update function
private void Update(){
if (isRunning) { Stamina -= 3 * Time.deltaTime }
else { Stamina += 3 * Time.deltaTime }
}
推荐阅读
- java - 如何访问数组改造 2 / android studio 中的对象“播放器”
- django - 是否可以在 Django 中以一种形式创建两个不同的提交按钮?
- javascript - 有没有办法可以从Firefox扩展中的DOM以外的页面获取链接?
- android - 如何防止由于回收站视图上的空列表而导致 android 应用程序崩溃?
- excel - 我需要在 Excel 中的 VBA 中设置路径,以便文件可以在多台计算机上使用
- python - pyspark中的reducebykey,元组中有多个关键字段
- php - 如何通过variation_ID从woocommerce中的购物车页面动态获取特定的产品变体数量?
- swift - Swift - 委托函数不返回值
- c# - 我可以将字节数组转换为 InkCanvas StrokeCollection 吗?
- java - Java - 在不停止服务器的情况下重新启动 WebSocket