Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 geehealey · Jun 03, 2015 at 11:32 AM · collidersintdoorsoperator

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);

}

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
2

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;
         }
     }

Comment
Add comment · Show 2 · 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 geehealey · Jun 04, 2015 at 08:11 AM 0
Share

Sorry man. Didn't work. Thanks anyway.

avatar image Gardes · Jun 04, 2015 at 03:56 PM 1
Share

So what is not working and how you implement it?

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

3 People are following this question.

avatar image avatar image avatar image

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


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