我目前正在为我的统一项目构建自定义角色移动系统,但是我的代码似乎出现了一些问题,控制台中没有出现错误,我想做的就是创建一个耐力计时器该系统会加重播放器的加班时间,并在播放器再次运行之前提供充电系统。
主要问题是当我打印当前耐力的值时,正在运行多个计时器,而且这些值也没有适当地上升 任何帮助将不胜感激
谢谢!
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;
}
}
}
答案 0 :(得分:0)
嘿,感谢所有我非常感谢他们的回答,我在代码中非常努力,认为这是我从未意识到的问题,我有两个相同的脚本与游戏对象相关联,而这反过来给我带来了乐趣行为XD,好吧,我猜想它对我们所有人都有好处!