- Home /
Dispenser Help
So I have made a script that I've applied to something that can be compared with the Dispensers in Half Life, aka the stations that gives you health and armor. I have a problem though, no matter where they are in the level, if I drain one they all get drained (and I get both health and armor if I have both of them out)
Script: using UnityEngine; using System.Collections;
public class RechargeScript : MonoBehaviour {
public enum tankTypes {Health, Armor};
public tankTypes tankType;
public Texture[] states; //Element 0 = Usable; Element 1 = Depleted;
public float ammount; //Max 100;
public GameObject front;
FPSController fpsc;
// Use this for initialization
void Awake(){
fpsc = GameObject.FindWithTag ("Player").GetComponent<FPSController> ();
}
void Start () {
bool state = true;
if (states.Length > 2 || states.Length < 2) {
Debug.LogError ("Variable states in " + gameObject.name + " takes 2 elements and 2 only!");
state = false;
}
if (ammount > 0 && state == true) {
front.GetComponent<Renderer> ().material.mainTexture = states [0];
} else if (ammount == 0 && state == true) {
front.GetComponent<Renderer> ().material.mainTexture = states [1];
}
}
// Update is called once per frame
void Update () {
ammount = Mathf.Clamp (ammount, 0, 100);
RaycastHit hit;
if (Input.GetButton ("Use")) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray, out hit, 1)){
if(hit.transform.tag == "RechargeStation"){
if(ammount > 0){
ammount -= 10 * Time.deltaTime;
if(tankType == tankTypes.Armor)
fpsc.armor += 10 * Time.deltaTime;
else if(tankType == tankTypes.Health)
fpsc.health += 10 * Time.deltaTime;
}
else{
Debug.Log("Tank is Empty");
front.GetComponent<Renderer>().material.mainTexture = states[1];
}
}
}
}
}
}
It works as intended exept for the problem I've explained. FPSController is a script I've made, shouldn't need more info than that, I just ask for the health and armor variables from it anyways. If you need anymore info tell me and Ill add it in.
Thanks!
Answer by Dave-Carlile · Oct 17, 2015 at 01:52 PM
This code is going to be executed for every object the script is on right?
if (Input.GetButton ("Use")) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast (ray, out hit, 1)){
if(hit.transform.tag == "RechargeStation"){
It's also true for every object the script is on because a ray from the camera position to the station that happens to be in front of the player is going to hit that station.
What's missing is a check to make sure the station being hit is the one that the script is on. Something like...
if (hit.transform.tag == "RechargeStation" && hit.transform == this.transform)
{
// this particular station is the one that was hit
}
Thank you mate! That was a super simple solution, mad I wasn't able to come up with that my self xD
Your answer
Follow this Question
Related Questions
Static var health 1 Answer
How can i make a ray cast take health from enemies 2 Answers
ray destroying prefab not clone? 2 Answers
Finding a certain variable is failing 0 Answers
Raycast Shoot Error 1 Answer