- Home /
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:(
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
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
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
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 :)
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
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...
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!
Your answer
Follow this Question
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