如何根据来自不同脚本的变量缩放对象?
How do i scale an object based on a variable from a different script?
我为我的第三人称玩家创建了一个力场——基本上是一个围绕他的球体。
我的玩家有耐力花车(curStamina 和 maxStamina)。
我正在尝试弄清楚如何根据玩家当前的耐力自动缩放球体的大小。所以力场(球体)基本上是对玩家当前拥有的耐力量(curStamina)的视觉解释,并且根据 curStamina 增加或减少大小。
这是我的耐力脚本供参考。感谢任何有关我将如何处理的帮助!
public class Stamina : MonoBehaviour
{
public float stamina = 100;
public float maxStamina = 100;
public float drainPerSecond = 2.0f, gainPerSecond = 1.0f;
public bool online = false, usingStamina = false;
//public int lives;
public GameObject staminaImagePrefab;
private Transform staminaPanel;
private Image staminaImage;
//private Color lerpedColor; // Not currently used
// Start is called before the first frame update
void Start()
{
if (transform.CompareTag("Player"))
{
staminaPanel = GameObject.FindGameObjectWithTag("StaminaPanel").transform;
GameObject staminaObject = Instantiate(staminaImagePrefab, staminaPanel);
staminaImage = staminaObject.transform.GetChild(1).GetComponent<Image>();
}
}
// Update is called once per frame
void Update()
{
if (usingStamina)
{
DrainStamina();
}
else
{
RegenerateStamina();
}
if (transform.CompareTag("Player"))
{
staminaImage.fillAmount = (float)stamina / maxStamina;
}
}
private void RegenerateStamina()
{
stamina += gainPerSecond * Time.deltaTime;
if (stamina > maxStamina) stamina = maxStamina;
}
public void StaminaDamage(float value)
{
stamina -= value;
}
private void DrainStamina()
{
stamina -= drainPerSecond * Time.deltaTime;
if (stamina < 0)
{
// Out of stamina
stamina = 0;
}
}
public void ResetStamina(float _stamina, float _maxStamina)
{
stamina = _stamina;
maxStamina = _maxStamina;
if (transform.CompareTag("Player"))
{
staminaImage.fillAmount = (float)stamina / maxStamina;
}
}
}
下面是一个如何实现它的例子:
public float MinForceFieldScale = 1f;
public float MaxForceFieldScale = 4f;
[SerializeField]
private Transform forceFieldTransform;
private float DeltaForceFieldScale => MaxForceFieldScale - MinForceFieldScale;
// Update is called once per frame
void Update()
{
if (usingStamina)
{
DrainStamina();
}
else
{
RegenerateStamina();
}
if (transform.CompareTag("Player"))
{
staminaImage.fillAmount = (float)stamina / maxStamina;
}
UpdateForceFieldSize();
}
private void UpdateForceFieldSize()
{
float actualStaminaPercent = stamina / maxStamina;
float forceFieldSize = MinForceFieldScale + DeltaForceFieldScale * actualStaminaPercent;
forceFieldTransform.localScale = Vector3.one * forceFieldSize;
}
我为我的第三人称玩家创建了一个力场——基本上是一个围绕他的球体。 我的玩家有耐力花车(curStamina 和 maxStamina)。
我正在尝试弄清楚如何根据玩家当前的耐力自动缩放球体的大小。所以力场(球体)基本上是对玩家当前拥有的耐力量(curStamina)的视觉解释,并且根据 curStamina 增加或减少大小。
这是我的耐力脚本供参考。感谢任何有关我将如何处理的帮助!
public class Stamina : MonoBehaviour
{
public float stamina = 100;
public float maxStamina = 100;
public float drainPerSecond = 2.0f, gainPerSecond = 1.0f;
public bool online = false, usingStamina = false;
//public int lives;
public GameObject staminaImagePrefab;
private Transform staminaPanel;
private Image staminaImage;
//private Color lerpedColor; // Not currently used
// Start is called before the first frame update
void Start()
{
if (transform.CompareTag("Player"))
{
staminaPanel = GameObject.FindGameObjectWithTag("StaminaPanel").transform;
GameObject staminaObject = Instantiate(staminaImagePrefab, staminaPanel);
staminaImage = staminaObject.transform.GetChild(1).GetComponent<Image>();
}
}
// Update is called once per frame
void Update()
{
if (usingStamina)
{
DrainStamina();
}
else
{
RegenerateStamina();
}
if (transform.CompareTag("Player"))
{
staminaImage.fillAmount = (float)stamina / maxStamina;
}
}
private void RegenerateStamina()
{
stamina += gainPerSecond * Time.deltaTime;
if (stamina > maxStamina) stamina = maxStamina;
}
public void StaminaDamage(float value)
{
stamina -= value;
}
private void DrainStamina()
{
stamina -= drainPerSecond * Time.deltaTime;
if (stamina < 0)
{
// Out of stamina
stamina = 0;
}
}
public void ResetStamina(float _stamina, float _maxStamina)
{
stamina = _stamina;
maxStamina = _maxStamina;
if (transform.CompareTag("Player"))
{
staminaImage.fillAmount = (float)stamina / maxStamina;
}
}
}
下面是一个如何实现它的例子:
public float MinForceFieldScale = 1f;
public float MaxForceFieldScale = 4f;
[SerializeField]
private Transform forceFieldTransform;
private float DeltaForceFieldScale => MaxForceFieldScale - MinForceFieldScale;
// Update is called once per frame
void Update()
{
if (usingStamina)
{
DrainStamina();
}
else
{
RegenerateStamina();
}
if (transform.CompareTag("Player"))
{
staminaImage.fillAmount = (float)stamina / maxStamina;
}
UpdateForceFieldSize();
}
private void UpdateForceFieldSize()
{
float actualStaminaPercent = stamina / maxStamina;
float forceFieldSize = MinForceFieldScale + DeltaForceFieldScale * actualStaminaPercent;
forceFieldTransform.localScale = Vector3.one * forceFieldSize;
}