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 fernandovt · Oct 26, 2013 at 06:55 AM · pickupkeyflashlight

FlashLight Fixes Please??

I have a working flashlight script that drains out the energy of my flashlight and turns it off, I want to fix some things, any suggestions will help a lot:):)

1) When the power of the flashlight goes out, i can still press f and hear the sound of the button to turn the flashlight on/of... How do i disable it until my flashlight has energy again??

2)I have another Script to pick up a flashlight object that i made by going through the object, the script goes attached to the player and the object must have a "FlashLight" tag. I would like that when i pick up this item, just then the first script gets activated, so then i can press "f" to turn the flashlight on and start draining it's batteries

3)I would prefer to pick the flashlight up by pressing "e" near it...:)

Thats all, If anyone can help me in any of the 3 problems it will mean a lot:)

The FlashlightPickUp Script(attached to the player):

 private var hit : RaycastHit;
 private var rayDistance : float;
 private var contactPoint : Vector3;
 var PickupSound: AudioClip;
     
 function OnTriggerEnter (other : Collider) {     
     if (other.CompareTag ("Flashlight")) { 
        audio.PlayOneShot(PickupSound);    
        Destroy(other.gameObject);               
     } 
 }

The FlashLight Script, wich drains energy and can be turned on/off with "f" key:

 var lightSource : Light; //Connect the light source in the Inspector
 var shootSound:AudioClip;
 static var energy : float = 100; //The energy amount of the flashlight
 static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
 private var drainSpeed : float = 9.0; //The speed that the energy is drained
  lightSource.enabled = false; 
 function Update () {
     if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
         
 }
  
 //When the player press F we toggle the flashlight on and off
 function ToggleFlashlight () {
     turnedOn=!turnedOn;
       audio.PlayOneShot(shootSound);
     if (turnedOn && energy>0) {
        TurnOnAndDrainEnergy();
     } else {
        lightSource.enabled = false;
      
     }
 }
  
 //When the flashlight is turned on we enter a while loop which drains the energy
 function TurnOnAndDrainEnergy () {
     lightSource.enabled = true;
     while (turnedOn && energy>0) {
        energy -= drainSpeed*Time.deltaTime;
        yield;
     }
     lightSource.enabled = false;
 //I tried here with----->   shootSound.enable = false;     ,it didn't work
 }
  
 //This is called from outside the script to alter the amount of energy
 static function AlterEnergy (amount : int) {
     energy = Mathf.Clamp(energy+amount, 0, 100);
 }
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
1

Answer by Crystalline · Oct 26, 2013 at 07:01 AM

If you want to make your flashlight work only if it has energy:

 function ToggleFlashlight () {
    if(energy>1){
        turnedOn=!turnedOn;
       audio.PlayOneShot(shootSound);
     if (turnedOn && energy>0) {
        TurnOnAndDrainEnergy();
     } else {
        lightSource.enabled = false;
     }
     }
 }
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 fernandovt · Oct 26, 2013 at 07:21 AM 0
Share

It did work!!!:), Any ideas of how to relate this script to the PickUp script so it only get's activated when i get the Flashlight object tagged "FlashLight"???

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

16 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

Related Questions

How to pickup flashlight with "E" key ? 1 Answer

Very simple picking up items script? 2 Answers

Flashlight pickup, battery etc 3 Answers

Pick up item code is not working 2 Answers

Problems with key script 0 Answers


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