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 EthanL · Jun 13, 2015 at 07:12 PM · javascripttoggleflashlighthorror

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'

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 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;
                  }
             }
     }


Comment
Add comment · Show 3 · 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 EthanL · Jun 14, 2015 at 02:09 PM 0
Share

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? :)

avatar image EthanL · Jun 14, 2015 at 02:09 PM 0
Share

There also seems to be a problem with 33, 24 saying that I need to insert a semicolon

avatar image Cherno · Jun 14, 2015 at 02:15 PM 0
Share

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

I want to turn my flashlight on and off, while still maintaining the flicker effects. 3 Answers

help with Flashlight Pickup 1 Answer

Only run the function once 2 Answers

How can I draw a line in Unity with mouse ....? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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