- Home /
I need to make a door open only when the player's essence has reached a certain level
Hi,
I am trying to script an interaction between the player and a door. The door has a trigger-collider on it and I want it to open when the player makes contact with it, BUT ONLY IF the player's essence (health) has reached a certain amount (at least 80); the process should then drain the player's health by 80. The essence is controlled by a different script and counts up at the rate of 0.5 p/s. I think the particular problem I am having is in accessing the currentEssence from the essenceScript and then subtracting from it as currentEssence is a variable and -80 is an integer. I have exhausted the forums for similar problems. Help me Obi Wan Kenobi: you're my only hope.
//essenceScript
var clockBG : Texture2D; var clockFG : Texture2D; var clockFGMaxWidth : float; private var currentEssence : float; var essenceRegen : float = 1;
function Start() { startTime = 10.0; clockFGMaxWidth = clockFG.width; currentEssence = 10; }
function Update () { currentEssence += essenceRegen * Time.deltaTime; if(currentEssence >= 100) currentEssence = 100; if(currentEssence <= 0) currentEssence = 0;
}
function OnGUI() {
var newBarWidth:float = (currentEssence/100) * clockFGMaxWidth;
Debug.Log("current essence " + currentEssence);
var gap:int = 20;
GUI.BeginGroup(new Rect (Screen.width - clockBG.width - gap,
gap, clockBG.width, clockBG.height));
GUI.DrawTexture(Rect (0,0, clockBG.width, clockBG.height), clockBG);
GUI.BeginGroup(new Rect(5,6, newBarWidth, clockFG.height));
GUI.DrawTexture(Rect(0,0, clockFG.width, clockFG.height), clockFG);
GUI.EndGroup();
GUI.EndGroup();
}
//dooropener
private var currentEssence: essenceScript = GetComponent(essenceScript);
function OnTriggerEnter() {
if (currentEssence >= 80) currentEssence -80; yield WaitForSeconds (2); Destroy(gameObject);
}
Answer by Gardes · Jun 03, 2015 at 02:40 PM
I wrote it down in c# and explained a bit in there. Just change the variable to your own in JS, that should work then.
essenceScript essenceScript;
void Start () {
essenceScript = GetComponent<essenceScript>();
/*
* I think your script is not attached to your Door,
* so this may not work.
*
* If it's attached to your Player you can go like
*
* essenceScript = GameObject.FindGameObjectWithTag ("Player").GetComponent<essenceScript> ();
*
* Your Player needs to be tagged properly then.
*/
}
void OnTriggerEnter (Collider other){
if (other.gameObject.CompareTag ("Player") && essenceScript.currentEssence >= 80) {
essenceScript.currentEssence -= 80;
}
}
Your answer
Follow this Question
Related Questions
physics.OverlapSphere colliders 1 Answer
Room rotation for door alignment 1 Answer
Opening Multiple Doors 2 Answers
Operator + cannot be used to combine Object and Int 1 Answer
looking for help with item pickups 1 Answer