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 Albert-han · May 13, 2014 at 11:13 AM · pickuphorrorbattery

battery pickup

Hi guys im making a hrror game and need a battery pickup.i am using this script for flashlight.

var lightSource : Light; //Connect the light source in the Inspector static var maxEnergy : float = 100; //The energy amount of the flashlight private var currentPower; var turnedOn : boolean = false; //Boolean to check whether it's turned on or off var drainSpeed : float = 2.0; //The speed that the energy is drained private var alpha : float;
private var duration : float = 0.2;
private var baseIntensity : float; var rectHeight: float;

function Update () { if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight(); if(currentPower < maxEnergy/4 && lightSource.enabled){ var phi : float = Time.time / duration 2 Mathf.PI; var amplitude : float = Mathf.Cos( phi ) * .5 + baseIntensity; lightSource.light.intensity = amplitude + Random.Range(0.1, 1.0) ; } lightSource.light.color = Color(alpha/maxEnergy, alpha/maxEnergy, alpha/maxEnergy, alpha/maxEnergy); alpha = currentPower;

    if (turnedOn==true) {
      if(currentPower > 0.0) currentPower -= Time.deltaTime * drainSpeed; 
         if(currentPower <= 0.0) {lightSource.enabled = false;}
    }
    if (turnedOn==false) {
    if(currentPower < maxEnergy) currentPower += Time.deltaTime * drainSpeed/2; 
    }

}

//When the player press F we toggle the flashlight on and off function ToggleFlashlight () { turnedOn=!turnedOn; if (turnedOn && maxEnergy>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 && maxEnergy>0) { maxEnergy -= drainSpeed*Time.deltaTime; yield; } lightSource.enabled = false; }

//This is called from outside the script to alter the amount of energy static function AlterEnergy (amount : int) { maxEnergy = Mathf.Clamp(maxEnergy+amount, 0, 100); }

//Display current battery on your flashlight function OnGUI () { GUI.Label (Rect(70, Screen.height/rectHeight - 75,150,60), "Battery: " + maxEnergy.ToString("F0") + "%"); }

and i tried all the battery pick up i counld find but all didnt work.i attached to a cube and made sphere collider but i just walked through it.

Comment
Add comment · Show 2
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 RyanPaterson · May 13, 2014 at 11:54 AM 0
Share

Hey, just a little heads up cause I have the problem sometimes posting a question. Sometimes the [ code ] tags are pretty weird. I'd edit it and paste them in again, it looks really messy and can be quite hard to read

avatar image Addyarb · May 13, 2014 at 12:10 PM 0
Share

Can you rephrase your question please? I'm having trouble understanding the problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OtsegoDoom · May 13, 2014 at 01:26 PM

So your problem is that your battery isn't doing anything when you "collect" it? You need to make sure you actually have code that detects when the player has touched the battery collectable.

 function OnTriggerEnter (other : Collider) {
 
    if (other.tag == "Player") {
       //Add your code to do whatever it is you want to do with the battery
    }
 }

Put this script on your battery. Make sure the collider on your battery is set to be a trigger. The player would need to have a rigidbody for OnTriggerEnter to work, and would need to have the tag "Player".

Comment
Add comment · 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

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

23 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

Related Questions

Flashlight pickup, battery etc 3 Answers

How would I go about removing a mesh renderer component on collision with a trigger? 1 Answer

Destroy trigger after a certain time 2 Answers

How do I highlight an object when the player is near it 1 Answer

Battery Pickup Regeneration 2 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