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 yanosch · Mar 27, 2014 at 11:37 AM · raycastbuttontriggerlight

What am i doing wrong? I want to make a raycast acces panel with lights

I want to make a raycast acces panel with lights

Assigned this to the main camera:

var CODELIGHT : Light;

 function Update()
 {
 
 flashlight = GetComponent("Light");
 
 }
 {
     if (Input.GetKeyDown(KeyCode.E))
     {
         var hit : RaycastHit;
         if (Physics.Raycast(transform.position, transform.forward, hit, 2.0F))
         {
             if(hit.transform.tag == "Obj1")
             {
                {
                 
             if (flashlight.enabled) {
             CODELIGHT.enabled = false;
             }
             else {
             CODELIGHT.enabled = true;
             
             }
         }
     }
 }
 
 }
 } 

hooked the script up to the main camera en made a cube with a boxcollider as trigger, tagged the cube Obj1. And assigned a pointlight to the script, but sadly... No succes:(

Comment
Add comment · Show 1
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 yanosch · Mar 27, 2014 at 12:12 PM 0
Share

UPDATE:

the code is now:

 var flashlight : Light;
 function Start () {
 flashlight = GetComponent("Light");
 }
 function Update()
 {
 
     
 
 
         if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.E))
         {
             var hit : RaycastHit;
             if (Physics.Raycast(transform.position, transform.forward, hit, 2.0F))
             {
                 if(hit.transform.tag == "Obj1")
                
             
                 if (flashlight.enabled) {
                 flashlight.enabled = false;
                 }
                 else {
                 flashlight.enabled = true;
                 
                 }
             
         }
 
 
     }
 } 
 


still noo banana's though

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by wibble82 · Mar 27, 2014 at 12:25 PM

First step, find out what (if anything) that ray cast is hitting.

In addition its worth noting that 'hit.transform' acceses the rigid body associated with the collider that was hit, which may not be the correct object. Use hit.collider. to access the actual game object with the collider on.

So add Debug.Log(hit.collider.name) inside the ray cast if statement, and see what gets printed.

If it's hitting the wrong thing (for example the player) you can use ray cast filters (easily found with a google search) to only look at specific layers.

(extra note) - just to be sure, I'm guessing the object with this script attached does have the light component also attached? Easily checked with Debug.Log(flashLight) just to make sure it is actually finding something to turn on!

-Chris

Comment
Add comment · Show 8 · 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 yanosch · Mar 27, 2014 at 01:39 PM 0
Share

Thanks dude! I fixed it with your help!

Problem now however is, i have two objects, with different tags, but the raycast hits both of them at the same time! even though they lie quite some space from eachother

avatar image wibble82 · Mar 27, 2014 at 02:03 PM 0
Share

How do you mean 'hits both of them at the same time'? It only hits one thing!

p.s. if that answer's right, could ya mark it as right so other people with the same problem know it works :)

avatar image yanosch · Mar 27, 2014 at 02:35 PM 0
Share

I have a safe,

in front of the safe is a keypad panel. Which has numbers on it, you can type in the combintion..

each number has a spotlight and a box collider and a unique tag... However, if my point my crosshair at one of them and press "E" they both activate...

It's driving me nuts! Since the box colliders are small enough and I'm very sure I couldn't hit one with another

avatar image yanosch · Mar 27, 2014 at 02:41 PM 0
Share

I seriously lost track now, the raycast seems to act like a collision trigger, when I'm standing in front of the panel I can point my cursor in every direction and press "e" and both of the buttons will llight up and activate...

avatar image yanosch · Mar 27, 2014 at 02:45 PM 0
Share

https://www.dropbox.com/s/6x0w0ewvvichfpe/Screeny%20Video%20$$anonymous$$ar%2027%2C%202014%2C%203.43.02%20P$$anonymous$$.mov

this is the situation

Show more comments
avatar image
0

Answer by yanosch · Mar 26, 2018 at 10:55 AM

I'm so late with this, but thanks,as I remember correctly, this solved it!

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

21 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

Related Questions

Raycast hits multiple targets 1 Answer

How to Find Owner of a Trigger? 1 Answer

active script from distance 3 Answers

I am making android fps and my raycast shooting button don't work. What I should do? 1 Answer

RayCast Activating A Button 0 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