PitrasCz
Založen: 02. 03. 2016 Příspěvky: 2
|
Zaslal: 2. březen 2016, 17:41:24 Předmět: Unity3D - PlayerGUI - nefunguje zabití hráče |
|
|
Dobrý den,
prosím o radu. Používám tento script na GUI hráče, script ukazuje - HP,jídlo,žízeň a výdrž. Je to ještě nastavené tak, že když jídlo nebo žízeň budou s hodnotou na 0, tak se začnou postupně odebírat HP až do 0, pak by měl hráč zemřít. Jenže se tak nestane,a vůbec nevím proč. Vždy když dojdou HP, tak to začne psát chybu do konzole. Jak to opravit?
Předem děkuji za odpověď.
Script:
kód: |
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerGUI : MonoBehaviour
{
//Size of Textures
Vector2 size = new Vector2(240, 40);
//Health Variables
[SerializeField]
private Vector2 healthPos = new Vector2(20, 20);
[SerializeField]
private float healthBarDisplay = 1f;
[SerializeField]
private Texture2D healthBarEmpty;
[SerializeField]
private Texture2D healthBarFull;
//Hunger Variables
[SerializeField]
private Vector2 hungerPos = new Vector2(20, 60);
[SerializeField]
private float hungerBarDisplay = 1f;
[SerializeField]
private Texture2D hungerBarEmpty;
[SerializeField]
private Texture2D hungerBarFull;
//Thirst Variables
[SerializeField]
private Vector2 thirstPos = new Vector2(20, 100);
[SerializeField]
private float thirstBarDisplay = 1f;
[SerializeField]
private Texture2D thirstBarEmpty;
[SerializeField]
private Texture2D thirstBarFull;
//Stamina Variables
[SerializeField]
private Vector2 staminaPos = new Vector2(20, 140);
[SerializeField]
private float staminaBarDisplay = 1f;
[SerializeField]
private Texture2D staminaBarEmpty;
[SerializeField]
private Texture2D staminaBarFull;
//Fall rate
[SerializeField]
private int healthFallRate = 150;
[SerializeField]
private int hungerFallRate = 150;
[SerializeField]
private int thirstFallRate = 100;
[SerializeField]
private int staminaFallRate = 35;
[SerializeField]
private bool canJump = false;
[SerializeField]
private float jumpTimer = 0.7f;
private UnityStandardAssets.Characters.FirstPerson.FirstPersonController chMotor;
private CharacterController controller;
void Start()
{
chMotor = GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>();
controller = GetComponent<CharacterController>();
}
void OnGUI()
{
//Health GUI
GUI.BeginGroup(new Rect(healthPos.x, healthPos.y, size.x, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), healthBarEmpty);
GUI.BeginGroup(new Rect(0, 0, size.x * healthBarDisplay, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), healthBarFull);
GUI.EndGroup();
GUI.EndGroup();
//Hunger GUI
GUI.BeginGroup(new Rect(hungerPos.x, hungerPos.y, size.x, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), hungerBarEmpty);
GUI.BeginGroup(new Rect(0, 0, size.x * hungerBarDisplay, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), hungerBarFull);
GUI.EndGroup();
GUI.EndGroup();
//Thirst GUI
GUI.BeginGroup(new Rect(thirstPos.x, thirstPos.y, size.x, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), thirstBarEmpty);
GUI.BeginGroup(new Rect(0, 0, size.x * thirstBarDisplay, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), thirstBarFull);
GUI.EndGroup();
GUI.EndGroup();
//Stamina GUI
GUI.BeginGroup(new Rect(staminaPos.x, staminaPos.y, size.x, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), staminaBarEmpty);
GUI.BeginGroup(new Rect(0, 0, size.x * staminaBarDisplay, size.y));
GUI.Box(new Rect(0, 0, size.x, size.y), staminaBarFull);
GUI.EndGroup();
GUI.EndGroup();
}
void Update()
{
//HEALTH CONTROL SECTION
if ((hungerBarDisplay <= 0) && (thirstBarDisplay <= 0))
{
healthBarDisplay -= Time.deltaTime / healthFallRate * 2;
}
else {
if ((hungerBarDisplay <= 0) || (thirstBarDisplay <= 0))
{
healthBarDisplay -= Time.deltaTime / healthFallRate;
}
}
if (healthBarDisplay <= 0)
{
CharacterDeath();
}
//HUNGER CONTROL SECTION
if (hungerBarDisplay >= 0)
{
hungerBarDisplay -= Time.deltaTime / hungerFallRate;
}
if (hungerBarDisplay <= 0)
{
hungerBarDisplay = 0;
}
if (hungerBarDisplay >= 1)
{
hungerBarDisplay = 1;
}
//THIRST CONTROL SECTION
if (thirstBarDisplay >= 0)
{
thirstBarDisplay -= Time.deltaTime / thirstFallRate;
}
if (thirstBarDisplay <= 0)
{
thirstBarDisplay = 0;
}
if (thirstBarDisplay >= 1)
{
thirstBarDisplay = 1;
}
//STAMINA CONTROL SECITON
if (controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
{
chMotor.setRunSpeed(10);
staminaBarDisplay -= Time.deltaTime / staminaFallRate;
}
else {
chMotor.setRunSpeed(6);
staminaBarDisplay += Time.deltaTime / staminaFallRate;
}
//JUMPING SECTION
if (Input.GetKeyDown(KeyCode.Space) && canJump == true && chMotor.getGrounded() && staminaBarDisplay >= 0.2)
{
staminaBarDisplay -= 0.2f;
chMotor.Jump();
Wait();
}
if (canJump == false)
{
jumpTimer -= Time.deltaTime;
chMotor.setJumpEnabled(false);
}
if (jumpTimer <= 0)
{
canJump = true;
chMotor.setJumpEnabled(true);
jumpTimer = 0.7f;
}
if (staminaBarDisplay <= 0.2)
{
canJump = false;
}
if (staminaBarDisplay >= 1)
{
staminaBarDisplay = 1;
}
if (staminaBarDisplay <= 0)
{
staminaBarDisplay = 0;
chMotor.setRunSpeed(6);
}
}
void CharacterDeath()
{
Application.LoadLevel("SIMPLELEVEL");
}
IEnumerator Wait()
{
yield return new WaitForSeconds(0.1f);
canJump = false;
}
} |
Chyba v konzoli
kód: |
Scene 'SIMPLELEVEL' (-1) couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
To add a scene to the build settings use the menu File->Build Settings...
UnityEngine.Application:LoadLevel(String)
PlayerGUI:CharacterDeath() (at Assets/Scripts/PlayerGUI.cs:224)
PlayerGUI:Update() (at Assets/Scripts/PlayerGUI.cs:137)
|
[/code] |
|