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 FlamingChickenWings · Feb 07, 2015 at 04:14 PM · arrayforenergy

[solved] passing energy from one object to another

Hi guys.

I have a bunch of receptacles which I want to be able to pass energy to one another, and have their own energy fade away. I do this by having them alter the energy variables in objects around them, but because of this they keep eachother from fading out.

So I wanted to have an array which saves all the receptacles which pass energy to an object, and have that object exclude those when it passes the energy itself.

The code that I used here:

 var energyfeed : float = 0.01;
 private var energyOrigin = new Array();
 
 var hitColliders = Physics.OverlapSphere(transform.position,6);
         
 for(var i=0; i<hitColliders.Length; i++) {
     if(hitColliders[i].gameObject.name=="receptacle") {
         for(var o=0;i<energyOrigin.length; o++) {
             if(hitColliders[i].gameObject!=energyOrigin[o]) {
                 var comp = hitColliders[i].GetComponent(ReceptacleBehaviour);
                 comp.lightEnergy+=comp.lightDecay+energyfeed;
                 comp.energyOrigin.Add(gameObject);
                 Debug.Log("check 2");
             }
             Debug.Log("check 1");
         }
     }
 }

if I remove the second for loop it works with the problems mentioned above. Does anyone see my problem here? Thanks.

Edit: It's finally working :D If anyone needs the final code, here it is:

 var debug : boolean = false;
 var lightEnergy : float = 0;
 var lightDecay : float = 0.05;
 var energyfeed : float = 0.01;
 private var hitColliders : Collider[];
 private var passing : boolean = false;
 private var cooldown : int = 0;
 
 function Update () {
 
     if (lightEnergy>0 && debug==false)
         lightEnergy-=lightDecay;
     if(lightEnergy>1)
         lightEnergy=1;
     if(lightEnergy<0) {
         lightEnergy=0;
         hitColliders = null;
         cooldown = 50;
         }
     if(cooldown>0)
         cooldown-=1;
     if(cooldown==1) {
         passing = false;
         }
     
     setBrightness(lightEnergy);
     
     
     if(lightEnergy>0.6) {    
         if(passing==false) {
             hitColliders = Physics.OverlapSphere(transform.position,6);
             passing = true;
             if(hitColliders!=null)
                 Debug.Log("Cupcakes");
         }
     }
     
     if(passing==true && cooldown==0) {
         for(var i=0; i<hitColliders.length; i++) {
             var obj = hitColliders[i].gameObject;
             if(obj.name=="receptacle") {
                 var comp = obj.GetComponent(ReceptacleBehaviour);
                 if(comp.passing==false) {
                     comp.lightEnergy+=comp.lightDecay+energyfeed;
                 }
             }
         }
     }
 }


PS. "SetBrightness" is just a function that adjusts some shader settings.

Comment
Add comment · Show 3
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 toddisarockstar · Feb 07, 2015 at 05:00 PM 0
Share

im not sure what the array is for. why not have a true/false variable in each of the scripts that goes on when the object is passing? then have spherecast check for it.

also, I fear spherecasting from multiple objects every frame might slow ya down.

if it only happens a few times a second ins$$anonymous$$d it pry wont effect your gameplay. i would put the spherecast into something like this:

 slow=slow-1;
 if(slow<1){slow=10;
 
           put spherecast here
 }
 
 


 
avatar image FlamingChickenWings · Feb 07, 2015 at 06:15 PM 0
Share

Thank for your answer! I tried that, and it made the transition a bit choppy (not unexpected). However, your true/false variable idea can definitely work, and I sort off combined both parts of your answer.

You were right about the spherecasting, by the way, but when I stopped to think a little bit I realised that none of the receptacles ever moved, so there was no reason to check for them every single frame.

So, I re-wrote the code to this: var energyfeed : float = 0.01; private var hitColliders = new Array();

 if(lightEnergy>0.6) {    
     if(hitColliders==null)
            hitColliders = Physics.OverlapSphere(transform.position,4);
     
     for(var i=0; i<hitColliders.length; i++) {
         if(hitColliders[i].gameObject.name=="receptacle") {
             if(hitColliders[i].gameObject.GetComponent(ReceptacleBehaviour)==null) {
                 var comp = hitColliders[i].gameObject.GetComponent(ReceptacleBehaviour);
                 comp.lightEnergy+=comp.lightDecay+energyfeed;
             }
         }
     }
 }

Sadly I can't test this, 'cause on the "hitColliders[i].gameObject" parts it keeps telling me that gameObject isn't a member of Object. I'm not really sure how to get around this, because, as far as I knew, OverlapSphere just returns an array. The only difference I made here, as far as I can tell, is that i created the variable before having OverlapSphere write to it. Anyway, I'll let you know how well this works once I solve that little problem. Thanks for your reply :)

Edit: I was a bit dumb. Changed "private var hitColliders = new Array();" to "private var hitColliders : Collider[];" and that got rid of the error. I also changed "if(hitColliders[i].gameObject.GetComponent(ReceptacleBehaviour)==null)" to "if(hitColliders[i].gameObject.GetComponent(ReceptacleBehaviour).hitColliders==null)" It's still not perfect though, 'cause now the second receptacle is just flashing. It seems I'm going to have to make another boolean anyways.

avatar image FlamingChickenWings · Feb 07, 2015 at 06:54 PM 0
Share

After a lot of tinkering it's working and running smoothly :D Thanks for the comment that helped me get there eventually :P

I'll put the updated, full Update() code in the original comment in case anyone needs this in the future... for whatever reason.

0 Replies

· Add your reply
  • Sort: 

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

how do i make a loop for a boolean? 2 Answers

What am I overlooking? -1 Answers

how to find if all objects in array are missing 1 Answer

Problem with nested for loops 2 Answers

[Solved]Using multiple arrays in one statement? 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