Hi unity,
My first post didn’t get any replies so I’m going to try this again. Well i have my first script I tried making and its this flashlight script. I’m trying to make it where my FPC has to pick up a flashlight by clicking on it and it spawns to him where it looks like hes holding it. and he has to collect batteries to keep his battery going like the lamp and oil in Amnesia. The problem i’m having is that i can’t pick up the flashlight. Sorry for my noobie scripting.
var lightSource : Light;
var drainSpeed : float = 2.0;
var hasFlashlight : boolean = false;
var turnedOn : boolean = false;
var rectHeight : float;
var distanceToFlashlight : float = 5.0;
var spawnTo : Transform;
var objectMoving : Transform;
static var maxEnergy : float = 100;
var currentPower : float;
private var alpha : float;
private var duration : float = 0.2;
private var baseIntensity : float;
function Start() {
Screen.lockCursor = true;
}
function Update () {
if (Input.GetMouseButtonDown(0))
{
Debug.Log ("Clicked Mouse");
var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
var hit : RaycastHit;
if ( Physics.Raycast( ray, hit, distanceToFlashlight ) )
{
if (hit.collider.gameObject.name == "Flashlight")
{
Debug.Log ("Got Flashlight")
}
else {
Debug.Log ("Didn't Hit");
}
}
}
if (hasFlashlight == true){
objectMoving.rigidbody.useGravity = false;
objectMoving.parent = spawnTo;
objectMoving.transform.position = spawnTo.transform.position;
objectMoving.transform.rotation = spawnTo.transform.rotation;
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;
}
}
function ToggleFlashlight () {
turnedOn=!turnedOn;
if (turnedOn maxEnergy>0) {
TurnOnAndDrainEnergy();}
else {
lightSource.enabled = false;
}
}
function TurnOnAndDrainEnergy () {
lightSource.enabled = true;
while (turnedOn maxEnergy>0) {
maxEnergy -= drainSpeed*Time.deltaTime;
yield;
}
lightSource.enabled = false;
}
static function AlterEnergy (amount : int) {
maxEnergy = Mathf.Clamp(maxEnergy+amount, 0, 100);
}
function OnGUI () {
GUI.Label (Rect(70, Screen.height/rectHeight - 75,150,60), "Battery: " + maxEnergy.ToString("F0") + "%");
}