Getting a variable from a different script.
I included 2 files
I use the prefab in the unity standard assets first person ridgid body controller and am attempting in to edit it to check for when a player stamina reaches 0 to then disable the “Left Shift” Run key until Stamina regenerates. The script that needs a variable to continue working is posted first and second is the script that handles the stamina.
I am having issues getting a variable from GUISurvival.js
Starting on Line 49 In the RidgidbodyFirstPersonController.cs are the changes I have made. Line 49 is labeled //LINE 49 starts here>>>>>>>>>>>>>>>>>>>> to make it easier to find.
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (Rigidbody))]
[RequireComponent(typeof (CapsuleCollider))]
public class RigidbodyFirstPersonController : MonoBehaviour
{
[Serializable]
public class MovementSettings
{
public float ForwardSpeed = 8.0f; // Speed when walking forward
public float BackwardSpeed = 4.0f; // Speed when walking backwards
public float StrafeSpeed = 4.0f; // Speed when walking sideways
public float RunMultiplier = 2.0f; // Speed when sprinting
public KeyCode RunKey = KeyCode.LeftShift;
public float JumpForce = 30f;
public AnimationCurve SlopeCurveModifier = new AnimationCurve(new Keyframe(-90.0f, 1.0f), new Keyframe(0.0f, 1.0f), new Keyframe(90.0f, 0.0f));
[HideInInspector] public float CurrentTargetSpeed = 8f;
#if !MOBILE_INPUT
public bool m_Running;
#endif
public void UpdateDesiredTargetSpeed(Vector2 input)
{
if (input == Vector2.zero) return;
if (input.x > 0 || input.x < 0)
{
//strafe
CurrentTargetSpeed = StrafeSpeed;
}
if (input.y < 0)
{
//backwards
CurrentTargetSpeed = BackwardSpeed;
}
if (input.y > 0)
{
//forwards
//handled last as if strafing and moving forward at the same time forwards speed should take precedence
CurrentTargetSpeed = ForwardSpeed;
}
#if !MOBILE_INPUT
if (Input.GetKey(RunKey))
{
//LINE 49 starts here>>>>>>>>>>>>>>>>>>>>
var characterStaminaInstance = "GUISurvival.js";
characterStaminaInstance = GameObject.FindObjectOfType("GUISurvival").GetComponent("GUISurvival");
if (characterStaminaInstance.currentStamina <= 0) {
CurrentTargetSpeed = ForwardSpeed;
m_Running = false;
}
else
{
CurrentTargetSpeed *= RunMultiplier;
m_Running = true;
}
}
else
{
m_Running = false;
}
#endif
}
#if !MOBILE_INPUT
public bool Running
{
get { return m_Running; }
}
#endif
}
[Serializable]
public class AdvancedSettings
{
public float groundCheckDistance = 0.01f; // distance for checking if the controller is grounded ( 0.01f seems to work best for this )
public float stickToGroundHelperDistance = 0.5f; // stops the character
public float slowDownRate = 20f; // rate at which the controller comes to a stop when there is no input
public bool airControl; // can the user control the direction that is being moved in the air
[Tooltip("set it to 0.1 or more if you get stuck in wall")]
public float shellOffset; //reduce the radius by that ratio to avoid getting stuck in wall (a value of 0.1f is nice)
}
public Camera cam;
public MovementSettings movementSettings = new MovementSettings();
public MouseLook mouseLook = new MouseLook();
public AdvancedSettings advancedSettings = new AdvancedSettings();
private Rigidbody m_RigidBody;
private CapsuleCollider m_Capsule;
private float m_YRotation;
private Vector3 m_GroundContactNormal;
private bool m_Jump, m_PreviouslyGrounded, m_Jumping, m_IsGrounded;
public Vector3 Velocity
{
get { return m_RigidBody.velocity; }
}
public bool Grounded
{
get { return m_IsGrounded; }
}
public bool Jumping
{
get { return m_Jumping; }
}
public bool Running
{
get
{
#if !MOBILE_INPUT
return movementSettings.Running;
#else
return false;
#endif
}
}
private void Start()
{
m_RigidBody = GetComponent<Rigidbody>();
m_Capsule = GetComponent<CapsuleCollider>();
mouseLook.Init (transform, cam.transform);
}
private void Update()
{
RotateView();
if (CrossPlatformInputManager.GetButtonDown("Jump") && !m_Jump)
{
m_Jump = true;
}
}
private void FixedUpdate()
{
GroundCheck ();
Vector2 input = GetInput();
if ((Mathf.Abs (input.x) > float.Epsilon || Mathf.Abs (input.y) > float.Epsilon) && (advancedSettings.airControl || m_IsGrounded)) {
// always move along the camera forward as it is the direction that it being aimed at
Vector3 desiredMove = cam.transform.forward * input.y + cam.transform.right * input.x;
desiredMove = Vector3.ProjectOnPlane (desiredMove, m_GroundContactNormal).normalized;
desiredMove.x = desiredMove.x * movementSettings.CurrentTargetSpeed;
desiredMove.z = desiredMove.z * movementSettings.CurrentTargetSpeed;
desiredMove.y = desiredMove.y * movementSettings.CurrentTargetSpeed;
if (m_RigidBody.velocity.sqrMagnitude <
(movementSettings.CurrentTargetSpeed * movementSettings.CurrentTargetSpeed)) {
m_RigidBody.AddForce (desiredMove * SlopeMultiplier (), ForceMode.Impulse);
}
}
if (m_IsGrounded)
{
m_RigidBody.drag = 5f;
if (m_Jump)
{
m_RigidBody.drag = 0f;
m_RigidBody.velocity = new Vector3(m_RigidBody.velocity.x, 0f, m_RigidBody.velocity.z);
m_RigidBody.AddForce(new Vector3(0f, movementSettings.JumpForce, 0f), ForceMode.Impulse);
m_Jumping = true;
}
if (!m_Jumping && Mathf.Abs(input.x) < float.Epsilon && Mathf.Abs(input.y) < float.Epsilon && m_RigidBody.velocity.magnitude < 1f)
{
m_RigidBody.Sleep();
}
}
else
{
m_RigidBody.drag = 0f;
if (m_PreviouslyGrounded && !m_Jumping)
{
StickToGroundHelper();
}
}
m_Jump = false;
}
private float SlopeMultiplier()
{
float angle = Vector3.Angle(m_GroundContactNormal, Vector3.up);
return movementSettings.SlopeCurveModifier.Evaluate(angle);
}
private void StickToGroundHelper()
{
RaycastHit hitInfo;
if (Physics.SphereCast(transform.position, m_Capsule.radius * (1.0f - advancedSettings.shellOffset), Vector3.down, out hitInfo,
((m_Capsule.height/2f) - m_Capsule.radius) +
advancedSettings.stickToGroundHelperDistance, ~0, QueryTriggerInteraction.Ignore))
{
if (Mathf.Abs(Vector3.Angle(hitInfo.normal, Vector3.up)) < 85f)
{
m_RigidBody.velocity = Vector3.ProjectOnPlane(m_RigidBody.velocity, hitInfo.normal);
}
}
}
private Vector2 GetInput()
{
Vector2 input = new Vector2
{
x = CrossPlatformInputManager.GetAxis("Horizontal"),
y = CrossPlatformInputManager.GetAxis("Vertical")
};
movementSettings.UpdateDesiredTargetSpeed(input);
return input;
}
private void RotateView()
{
//avoids the mouse looking if the game is effectively paused
if (Mathf.Abs(Time.timeScale) < float.Epsilon) return;
// get the rotation before it's changed
float oldYRotation = transform.eulerAngles.y;
mouseLook.LookRotation (transform, cam.transform);
if (m_IsGrounded || advancedSettings.airControl)
{
// Rotate the rigidbody velocity to match the new direction that the character is looking
Quaternion velRotation = Quaternion.AngleAxis(transform.eulerAngles.y - oldYRotation, Vector3.up);
m_RigidBody.velocity = velRotation*m_RigidBody.velocity;
}
}
/// sphere cast down just beyond the bottom of the capsule to see if the capsule is colliding round the bottom
private void GroundCheck()
{
m_PreviouslyGrounded = m_IsGrounded;
RaycastHit hitInfo;
if (Physics.SphereCast(transform.position, m_Capsule.radius * (1.0f - advancedSettings.shellOffset), Vector3.down, out hitInfo,
((m_Capsule.height/2f) - m_Capsule.radius) + advancedSettings.groundCheckDistance, ~0, QueryTriggerInteraction.Ignore))
{
m_IsGrounded = true;
m_GroundContactNormal = hitInfo.normal;
}
else
{
m_IsGrounded = false;
m_GroundContactNormal = Vector3.up;
}
if (!m_PreviouslyGrounded && m_IsGrounded && m_Jumping)
{
m_Jumping = false;
}
}
}
}
GUISurvival.js Stamina Code starts on line 89
#pragma strict
var currentHealth : float = 100.0;
var maxHealth : float = 100.0;
var currentThirst : float = 100.0;
var maxThirst : int = 100;
var thirstMultiplyer : int = 50;
var currentHunger : float = 100.0;
var maxHunger : int = 100;
var hungerMultiplyer : int = 100;
public var currentStamina : float = 100.0;
var maxStamina : int = 100;
var staminaMultiplyer : int = 3;
public var CurrentTargetSpeed : float = 8f;
public var ForwardSpeed : float = 8.0f;
public var m_Running = false;
private var barLength = 0.0;
private var skillHeight = 0.0;
private var skillLength = 0.0;
private var skillxLoc = 0.0;
private var skillyLoc = 0.0;
private var RBController : CharacterController;
private var speed : float = 6.0f;
private var jumpSpeed : float = 8.0f;
private var gravity : float = 20.0f;
private var moveDirection : Vector3 = Vector3.zero;
function Start()
{
RBController = GetComponent(CharacterController);
barLength = Screen.width / 8;
skillLength = Screen.width / 20;
skillHeight = Screen.height / 8;
skillyLoc = Screen.height - 100;
skillxLoc = Screen.width / 4;
}
function Update()
{
if(currentHealth <= 0)
{
CharacterDeath();
}
/* THIRST CONTROL SECTION*/
//Normal thirst degredation
if(currentThirst >= 0)
{
currentThirst -= Time.deltaTime / thirstMultiplyer;
}
if(currentThirst <= 0)
{
currentThirst = 0;
}
if(currentThirst >= maxThirst)
{
currentThirst = maxThirst;
}
/* HUNGER CONTROL SECTION*/
if(currentHunger >= 0)
{
currentHunger -= Time.deltaTime / hungerMultiplyer;
}
if(currentHunger <= 0)
{
currentHunger = 0;
}
if(currentHunger >= maxHunger)
{
currentHunger = maxHunger;
}
/*STAMINA CONTROL*/
if (Input.GetKey(KeyCode.LeftShift))
{
if(currentStamina >= 0) {
m_Running = true;
currentStamina -= Time.deltaTime / staminaMultiplyer * 8;
}
if(currentStamina <= 0)
{
currentStamina = 0;
}
if(currentStamina >= maxStamina)
{
currentStamina = maxStamina;
}
if(currentStamina >= maxStamina)
{
currentStamina = maxStamina;
} else {
currentStamina += Time.deltaTime / staminaMultiplyer * 8;
m_Running = false;
}
}
/* DAMAGE CONTROL SECTION*/
if(currentHunger <= 0 && (currentThirst <= 0))
{
currentHealth -= Time.deltaTime / thirstMultiplyer;
}
else
{
if(currentHunger <= 0 || currentThirst <= 0)
{
currentHealth -= Time.deltaTime / hungerMultiplyer;
}
}
}
function CharacterDeath()
{
Application.LoadLevel("DeathMenu");
}
function OnGUI()
{
//Icons
GUI.Box(new Rect(5, 30, 60, 23), "Health");
GUI.Box(new Rect(5, 55, 60, 23), "Thirst");
GUI.Box(new Rect(5, 80, 60, 23), "Hunger");
GUI.Box(new Rect(5, 105, 60, 23), "Stamina");
//Health / Hunger / Thirst bars
GUI.Box(new Rect(65, 30, barLength, 23), currentHealth.ToString("0") + "/" + maxHealth);
GUI.Box(new Rect(65, 55, barLength, 23), currentThirst.ToString("0") + "/" + maxThirst);
GUI.Box(new Rect(65, 80, barLength, 23), currentHunger.ToString("0") + "/" + maxHunger);
GUI.Box(new Rect(65, 105, barLength, 23), currentStamina.ToString("0") + "/" + maxStamina);
GUI.Box(new Rect(skillxLoc, skillyLoc, skillLength, skillHeight), "1");
}