Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ellis · Oct 17, 2015 at 01:44 PM · raycastenumarmorhealth

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ellis · Oct 17, 2015 at 02:12 PM 0
Share

Thank you mate! That was a super simple solution, mad I wasn't able to come up with that my self xD

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges