- Home /
Could anyone help me with .js code to be able to pickup a flashlight and turn it on and off
Creating a horror game. New to code, can anyone help me to be able to pick up the flashlight with the firstperson character with 'e' and then be able to toggle in on and off with'f'
Answer by Cherno · Jun 13, 2015 at 08:06 PM
I will try to explain what kinds of things you need to know about for your endeavour. My UnityScript is a little rusty (I recommend using C#, it will help you in the mid- to long run) and I didn't test the code, all from memory and the API.
First of all, you need a GameObject which represents your Flashlight item. I assumethat you already have a model of the flashlight, otherwise just use the default cube as a placeholder. Place it somewhere near the player in the scene on the ground. Make sure it has a collider (box, mesh or sphere will all work equally well in this case). Change it's name to "Flashlight". It also needs a Light component, you probably want to set the type to Spotlight.
Now you need to make the game know when the player is pointing at the flashlight. To do this, you can cast a ray with Physics.Raycast. You need an origin point for the raycast, it obviously has to start somewhere, and a direction vector. You can use the player camera's transform.position vector3 as the origin, and the camera's transform.forward as the direction. You need to store the camera's Transform component in a variable so you can access it in the raycast. You also need to store the flashlight object in a GameObject variable once we pick it up so we can access it later, to switch it on and off.
public var cameraTransform : Transform;//drag and drop the camera into this field in the inspector
public var flashlightObject : GameObject;
function Update () {
if (Input.GetKeyDown (Keycode.E)) {
var hit : RaycastHit;
if (Physics.Raycast (cameraTransform.position, cameraTransform.forward, hit, 2)) {
print ("There is something in front of the object!");
if(hit.collider.gameObject.name == "Flashlight") {
print ("It's a flashlight!");
//assign the object hit by the RC (flashlight, in this case) to the flashlightObject variable
flashlightObject = hit.collider.gameObject;
//make the flashlight a child of the camera so it moves and rotates with it
flashlightObject.transform.parent = cameraTransform;
//set the flashlight's position and rotation
flashlightObject.transform.localPositon = Vector3.zero;
flashlightObject.transform.rotation = cameraTransform.rotation;
}
}
}
if (Input.GetKeyDown (Keycode.F)) {
//imediately return if we don't have a flashlight yet.
if(flashlightObject == null) {
print ("We have no flashlight!");
return;
}
//temporarily store the Light component on the FL in a new variable
Light light = flashlightObject.GetComponent(Light);
//make sure it's not null or we will get a NullReference error
if(light != null) {
//switch the Light component's state.
light.enabled = !light.enabled;
}
}
}
Thanks for the answer, trying it out now! I noticed you mentioned C#. Is there any way you could show me the code for this as well just in case? :)
There also seems to be a problem with 33, 24 saying that I need to insert a semicolon
Change this line:
Light light = flashlightObject.GetComponent(Light);
to:
var light : Light = flashlightObject.GetComponent(Light);
As for C# conversion: You only need to change the syntax of the function and variable delcarations. However, C# scripts as a whole have a different structure than US scripts. the Unity user manual has a section dedicated to explaining the differences.