c# - 统一处理玩家运动和耐力系统的 C# 代码问题
问题描述
我目前正在为我的统一项目构建一个自定义角色移动系统,但我的代码似乎有一些问题,控制台中没有出现错误,我想做的是为耐力系统创建一个计时器,它将让玩家加班加点疲劳,并且在玩家再次跑步之前还有一个充电系统。
主要问题是当我打印当前耐力的值时,多个计时器正在运行,而且这些值也没有适当上升,因为如果有任何帮助,我们将不胜感激
谢谢!
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerController : MonoBehaviour {
CharacterController characterControl;
[SerializeField] float walkSpeed = 10f;
[SerializeField] float sprintSpeed = 15f;
[Header("Stamina")]
[SerializeField] float maxStaminaDecreaseTimer = .5f;
[SerializeField] float maxStaminaIncreaseTime = 1.0f;
[SerializeField] float speedChange = 15f;
float maxStamina = 100f;
float currentStamina;
float currentSpeed;
bool isSprinting = false;
float staminaDecrease = 0.5f;
float staminaIncrease = 1.0f;
//Timer System
float currentStaminaDecreaseTimer;
float currentStaminaIncreaseTimer;
// Movement System
void Start() {
characterControl = GetComponent<CharacterController>();
currentStaminaDecreaseTimer = maxStaminaDecreaseTimer;
currentStaminaIncreaseTimer = maxStaminaIncreaseTime;
currentSpeed = walkSpeed;
currentStamina = maxStamina;
}
void Update() {
MovePlayer();
StaminaSystem();
}
void MovePlayer() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 moveSide = transform.right * horizontal * currentSpeed;
Vector3 moveForward = transform.forward * vertical * currentSpeed;
characterControl.SimpleMove(moveSide * Time.deltaTime);
characterControl.SimpleMove(moveForward * Time.deltaTime);
}
void StaminaSystem() {
if (CrossPlatformInputManager.GetButton("Sprint")) {
if (!isSprinting && currentStamina > 0) {
currentSpeed = sprintSpeed;
isSprinting = true;
}
} else {
if (isSprinting) {
currentSpeed = walkSpeed;
isSprinting = false;
}
}
if (isSprinting) {
if (currentStaminaDecreaseTimer <= 0) {
currentStamina -= staminaDecrease;
currentStaminaDecreaseTimer = maxStaminaDecreaseTimer;
}
currentStaminaDecreaseTimer -= Time.deltaTime;
} else if (!isSprinting) {
if (currentStaminaIncreaseTimer <= 0) {
currentStamina += staminaIncrease;
currentStaminaIncreaseTimer = maxStaminaIncreaseTime;
}
if (currentStamina > maxStamina) {
currentStamina = maxStamina;
}
print(currentStamina);
currentStaminaIncreaseTimer -= Time.deltaTime;
}
}
}
解决方案
嘿,伙计们,感谢所有回复,我非常感谢他们,我在我的代码中如此努力地认为这是我从未意识到我有两个相同的脚本与游戏对象相关联的问题,这反过来又给了我有趣的行为 XD,好吧,我想这发生在我们最好的人身上!
推荐阅读
- excel - 调用将 dict 作为值传递的子过程时,参数不是可选的
- microsoft-graph-api - 微软的 REST API
- react-native - 是否可以使用 react native webview 检索视频内容的状态(直到他/她播放)onclick exit button
- php - Change datetimepicker format in Laravel
- google-analytics - 哪些页面导致了转化
- android - recyclerview 和分页滚动问题
- sql - 如何在sql server中添加动态列,即从声明变量中选择列名
- tomcat - tomcat-war 文件配置
- eclipse - 如何关闭 Eclipse 退出时的所有选项卡?
- javascript - ReactJS IconButton 在 Firefox 中不起作用