напиши скрипт для юнити добавляющий стамину в юнити 3д вот код:using…
- User
напиши скрипт для юнити добавляющий стамину в юнити 3д вот код:using System.Collections.Generic; using UnityEngine;
public class FirstPersonMovement : MonoBehaviour { public float speed = 5;
[Header("Running")] public bool canRun = true; public bool IsRunning { get; private set; } public float runSpeed = 9; public KeyCode runningKey = KeyCode.LeftShift; Rigidbody rigidbody; /// <summary> Functions to override movement speed. Will use the last added override. </summary> public List<System.Func<float>> speedOverrides = new List<System.Func<float>>(); void Awake() { // Get the rigidbody on this. rigidbody = GetComponent<Rigidbody>(); } void FixedUpdate() { // Update IsRunning from input. IsRunning = canRun && Input.GetKey(runningKey); // Get targetMovingSpeed. float targetMovingSpeed = IsRunning ? runSpeed : speed; if (speedOverrides.Count > 0) { targetMovingSpeed = speedOverrides[speedOverrides.Count - 1](); } // Get targetVelocity from input. Vector2 targetVelocity =new Vector2( Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed); // Apply movement. rigidbody.velocity = transform.rotation * new Vector3(targetVelocity.x, rigidbody.velocity.y, targetVelocity.y); }}
- Assistant
- User
Серьезность Код Описание Проект Файл Строка Состояние подавления Ошибка CS0103 Имя "speedOverridesspeedOverrides" не существует в текущем контексте. Assembly-CSharp D:\mine\test2.0\Assets\Mini First Person Controller\Scripts\FirstPersonMovement.cs 49 Активные Ошибка CS0103 Имя "targetMovingeness" не существует в текущем контексте. Assembly-CSharp D:\mine\test2.0\Assets\Mini First Person Controller\Scripts\FirstPersonMovement.cs 52 Активные
- Assistant
Прошу прощения за ошибки в предыдущем коде. В тексте появились опечатки, которые вызвали ошибки компиляции.
Вот исправленные строки, которые вызвали ошибки:
Исправленный код
using System.Collections.Generic; using UnityEngine; public class FirstPersonMovement : MonoBehaviour { public float speed = 5; [Header("Running")] public bool canRun = true; public bool IsRunning { get; private set; } public float runSpeed = 9; public KeyCode runningKey = KeyCode.LeftShift; public float stamina = 100f; public float maxStamina = 100f; public float staminaDecreasePerSecond = 10f; public float staminaRecoveryRate = 5f; private Rigidbody _rigidbody; public List<System.Func<float>> speedOverrides = new List<System.Func<float>>(); void Awake() { _rigidbody = GetComponent<Rigidbody>(); } void Update() { if (IsRunning && stamina > 0) { stamina -= staminaDecreasePerSecond * Time.deltaTime; } else if (stamina < maxStamina) { stamina += staminaRecoveryRate * Time.deltaTime; } stamina = Mathf.Clamp(stamina, 0, maxStamina); } void FixedUpdate() { IsRunning = canRun && Input.GetKey(runningKey) && stamina > 0; float targetMovingSpeed = IsRunning ? runSpeed : speed; if (speedOverrides.Count > 0) { targetMoving$peed = speedOverrides[speedOverrides.Count - 1](); } Vector2 targetVelocity = new Vector2(Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed); _rigidbody.velocity = transform.rotation * new Vector3(targetVelocity.x, _rigidbody.velocity.y, targetVelocity.y); } } - User
при достижении 0 стамина все равно работает. Сделай когда стамина = 0 то только после 3 секунд идет востановление
- Assistant