Advertisement
ReflectedMirror

battery pickup

Aug 16th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public var raycastLength : int = 5; //How far away can you be to pick up the battery?
  2. public var batteryIncrease : float = 1.0; //How much power do you get by one battery?
  3. public var batteryObjectName : String = "Battery"; //Must be the same name as the battery in the scene or it won't work.
  4.  
  5. //Importing the "Player_Toggle_Flashlight"
  6. public var flashlight : GameObject;
  7. private var ptf_Script : Player_Toggle_Flashlight;
  8.  
  9. //Audio
  10. public var mainPlayer : Transform;
  11. public var pickupSound : AudioClip;
  12.  
  13. //Variables END
  14.  
  15. function Start()
  16. {       //Get the script Player_Toggle_Flashlight
  17.         ptf_Script = flashlight.GetComponent(Player_Toggle_Flashlight);
  18. }
  19.  
  20. function Update()
  21. {       //Pick up the battery
  22.         BatteryPickup();
  23. }
  24.  //Function for picking up the battery
  25. function BatteryPickup()
  26. {       //If i press E, then something happens
  27.         if(Input.GetKeyDown(KeyCode.E))
  28.         {      
  29.                 //Where will the ray start? Camera --> main --> ScreenPointToRay   vector3(3d)
  30.                 var rayOrigin = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0.0));
  31.                 var hit : RaycastHit;
  32.                 //
  33.                 if(Physics.Raycast (rayOrigin, hit, raycastLength))
  34.                 {       //If Raycast hit the gameObject named batteryObjectName(Battery), refer to variables
  35.                         if(hit.collider.gameObject.name == batteryObjectName)
  36.                         {       //Then the var batteryLife inside the pft_script = batteryLife + batteryIncrease
  37.                                 ptf_Script.batteryLife += batteryIncrease;
  38.                                 //Then destroy the object
  39.                                 Destroy(hit.collider.gameObject);
  40.                                 //Then play the sound of pick up battery (pickupSound)
  41.                                 //audio.PlayClipAtPoint(pickupSound, mainPlayer.position);
  42.                         }
  43.                 }
  44.         }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement