从另一个访问FirstPersonController脚本以更改值不起作用

时间:2018-11-02 01:51:59

标签: c# unity3d

我正在尝试为原型游戏即时制作一个耐力系统,完成它后,我认为没有理由不起作用,但事实并非如此。我怀疑它实际上并没有在我调用脚本时访问脚本并获取组件,因此没有标题。我不会包括FPS脚本代码,据我所知,它不是统一的标准代码,在这里不需要。否则请让我知道。下面是代码,我认为应该是一个正常的Stamina系统。任何帮助表示赞赏。

编辑:标记为已解决,除了我什么都没错,问题是错字

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityStandardAssets.Characters.FirstPerson;

public class PlayerVitals : MonoBehaviour
{

private float vital = 0;

public Slider healthSlider;
public int maxHealth;
public int healthFallRate;

public Slider thirstSlider;
public int maxThirst;
public int thirstFallRate;

public Slider staminaSlider;
public int maxStamina;
private int staminaFallRate;
public int staminaFallMult;
private int staminaRegainRate;
public int staminaRegainMult;

private CharacterController charController;
private FirstPersonController playerController;



public DeathMenu deathMenu;
public GameObject FPSController;
public Camera Meep;

void Sart()
{
    //sets max value of slider to whatever we set as the max health, thirst etc
    healthSlider.maxValue = maxHealth;
    healthSlider.value = maxHealth;

    thirstSlider.maxValue = maxThirst;
    thirstSlider.value = maxThirst;

    staminaSlider.maxValue = maxStamina;
    staminaSlider.value = maxStamina;

    staminaFallRate = 1;
    staminaRegainRate = 1;


    // call them here in the script so we dont need to write it out everytime
    charController = GetComponent<CharacterController>();
    playerController = GetComponent<FirstPersonController>();


    Meep.enabled = false;
}
void Update()
{
    //health fall
    if (thirstSlider.value <= 0)
    {
        healthSlider.value -= Time.deltaTime / healthFallRate * 2;

    }

    if (healthSlider.value <=0)
    {
        CharacterDeath();
    }




    //Thirst
    if (thirstSlider.value >= 0)
    {
        thirstSlider.value -= Time.deltaTime / thirstFallRate;
    }

    else if (thirstSlider.value <= 0)
    {
        thirstSlider.value = 0;
    }

    else if (thirstSlider.value >= maxThirst)
    {
        thirstSlider.value = maxThirst;
    }

    //stamina

    if(charController.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
    {
        staminaSlider.value -= Time.deltaTime / staminaFallRate * staminaFallMult;
    }
    else
    {
        staminaSlider.value += Time.deltaTime / staminaRegainRate * staminaRegainMult;
    }

    if (staminaSlider.value >= maxStamina)
    {
        staminaSlider.value = maxStamina;
    }
    else if (staminaSlider.value <= 0) { //if the stamina ever reaches zero, we want the run speed to equal the walk speed
        staminaSlider.value = 0;
        playerController.m_RunSpeed = playerController.m_WalkSpeed;
    }


    //currently can only walk, even after regen, below fixes that, ever above zero, we want it to be our new normal runspeed
    else if (staminaSlider.value >= 0)
    {
        playerController.m_RunSpeed = playerController.m_RunSpeedNorm;
    }
}

void CharacterDeath()
{
    // DIE DIE DIE - Reaper main 2028
   deathMenu.ToggleEndMenu (vital);
   FPSController.SetActive(false);
   Meep.enabled = true;
   Cursor.lockState = CursorLockMode.None;

}

}

1 个答案:

答案 0 :(得分:0)

您键入的是void Sart()而不是Start()