Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Пожалуйста, помогите исправить код на C#!

Артём Зиненко Ученик (228), на голосовании 3 недели назад
Что делать если стамина в минус идёт, а бег не прекращается?

Вот код:

using System.Collections;

using System;

using System.Collections.Generic;

using UnityEngine;



public class PlayerMovement : MonoBehaviour

{

public CharacterController controller;



public float speed = 5f;

public float runSpeed = 12f;

public float gravity = -9.81f;

public float jump = 1f;

public float maxStamina = 100f;

public float currentStamina;

public float regenStamSpeed = 2f;

public float staminaSpend = 2f;

public float jumpCost = 30f;



public Transform groundCheck;

public float groundDistance = 0.4f;

public LayerMask groundMask;



private bool isRunning;



Vector3 velocity;

bool isGrounded;



private void Start()

{

currentStamina = maxStamina;

StartCoroutine(RegenStamina());

}



// Update is called once per frame

void Update()

{

isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);



if (isGrounded && velocity.y < 0)

{

velocity.y = -2f;

}



float x = Input.GetAxis("Horizontal");

float z = Input.GetAxis("Vertical");



Vector3 move = transform.right * x + transform.forward * z;



controller.Move(move * speed * Time.deltaTime);



if (Input.GetKey(KeyCode.LeftShift) && currentStamina <= maxStamina)

{

Debug.Log("Run On");

controller.Move(move * runSpeed * Time.deltaTime);

isRunning = true;

}

else

{

isRunning = false;

}



if (Input.GetButtonDown("Jump") && isGrounded)

{

velocity.y = Mathf.Sqrt(jump * -2f * gravity);

currentStamina -= jumpCost;

}



velocity.y += gravity * Time.deltaTime;



controller.Move(velocity * Time.deltaTime);



if(isRunning)

{

currentStamina -= staminaSpend;

if (currentStamina <= 0)

{

isRunning = false;

RegenStamina();

}

}



}



IEnumerator RegenStamina()

{

while (true)

{

if (currentStamina < maxStamina)

{

currentStamina += regenStamSpeed;

yield return new WaitForSeconds(1f);

}

else

{

yield return null;

}

}

}

}
Дополнен 1 месяц назад
Спасибо за помощь! Я решил проблему!
Голосование за лучший ответ
Дмитрий Onyx Знаток (394) 1 месяц назад
А теперь тоже самое, только скринами
Артём ЗиненкоУченик (228) 1 месяц назад
5 cек
Дмитрий OnyxЗнаток (394) 1 месяц назад
При проверки условия currentStamina < maxStamina выполняйте проверку что currentStamina >= 0
Артём Зиненко Ученик (228) Дмитрий Жмуров, нет, не работает((
Артём ЗиненкоУченик (228) 1 месяц назад
сейчас проверю
Артём ЗиненкоУченик (228) 1 месяц назад
или может я тупой и неправильно сделал
Артём ЗиненкоУченик (228) 1 месяц назад
if (Input.GetKey(KeyCode.LeftShift) && currentStamina <= maxStamina)
{
Debug.Log("Run On");
controller.Move(move * runSpeed * Time.deltaTime);

if (currentStamina > 0);
{
isRunning = true;
}
}
Дмитрий OnyxЗнаток (394) 1 месяц назад
if (Input.GetKey(KeyCode.LeftShift) && currentStamina <= maxStamina && currentStamina >= 0)
Артём ЗиненкоУченик (228) 1 месяц назад
Я вижу прогресс, число останавливается на -0.5 и дальше не идёт!!!
Но по логике кода isRunning должно быть false после иссекания стамины
Похожие вопросы