- Home /
Need help with a mirror puzzle script ^^'
Hi everyone ! I've written this script to make a kind of puzzle where the player have to rotate mirrors so they are reflecting light to each other and activate some kind of trigger. Here's my script : (I've hidden the part for the visuals and the rotation wich works perfectly :
public class RayMirror : MonoBehaviour {
//the transform where the beam is shooted from (named gem because it's a gem ^^)
public Transform gem;
//raycast hit variable
RaycastHit hit;
// Bools
public bool IsActivated = false;
private GameObject currentMirror;
void Update () {
if(IsActivated == true)
{
//raycast from the gem in the forward direction of the mirror
Physics.Raycast(gem.position, transform.forward, out hit);
//if it hits another mirror
if (hit.transform.gameObject.GetComponent<RayMirror>() != null)
{
//if the mirror isn't already activated
if(hit.transform.gameObject.GetComponent<RayMirror>().IsActivated == false)
{
//set this mirror as the current mirror
currentMirror = hit.transform.gameObject;
//then activate it
currentMirror.GetComponent<RayMirror>().Activate();
}
}
//if it hits something that isn't the current mirror
if (currentMirror != null && hit.transform.gameObject != currentMirror)
{
//so disable the current mirror
currentMirror.GetComponent<RayMirror>().DeActivate();
//and clear the current mirror variable
currentMirror = null;
}
}
}
public void Activate()
{
IsActivated = true;
}
public void DeActivate()
{
IsActivated = false;
//if this mirror was powering another mirror, then disable it
if (hit.transform.gameObject == currentMirror)
{
currentMirror.GetComponent<RayMirror>().DeActivate();
currentMirror = null;
}
}
It works perfectly fine, but the problem is the following : When I rotate this mirror, the first one it is hitting stays activated, wich is'nt the case for all the other it is hitting during the rotation. What is this due to ? How can I solve this ? Thanks in advance ^_^
So without answering your actual question I would like to ask some questions and pose some thoughts.
Are you actually reflecting the light at the reflected angle, or are you simply projecting the light forward out of the mirror?
Why does a mirror have to be inactive to reflect light? $$anonymous$$irrors can reflect multiple light sources.
I would look at the problem from a different angle (pun intended) and create a LightBeam class or similar that picks up mirrors as it collides.
Answer by Kevin_Jourdain · Oct 24, 2017 at 10:26 AM
- I'm simply projecting a ray directly forward the mirror : "Physics.Raycast(gem.position, transform.forward, out hit);"
Yeah but if the mirror is already activated i't useless to activate it a second time considering that it can reflect only one ray forward ^^'
I'm actually a begginer at unity and C# and I don't really see how I could do that, but i'll think about that solution :)
Thanks anyway ^^
Yes. If your projecting light then there is no point in having more than 1 beam. As for a lightbeam see what you think of this
using UnityEngine;
public class LightBeam : $$anonymous$$onoBehaviour
{
public Vector3 origin;
public Vector3 direction;
private RaycastHit rayInfo;
private LightBeam reflection;
private void Start()
{
rayInfo = new RaycastHit();
}
void Update()
{
Ray ray = new Ray(origin, direction);
if (Physics.Raycast(ray, out rayInfo))
{
if (rayInfo.transform.name == "mirror") // use whatever logic here to test for hitting a mirror
{
if (reflection == null)
{
reflection = new LightBeam();
}
reflection.origin = rayInfo.point;
reflection.direction = Vector3.Reflect(direction, rayInfo.normal);
}
Debug.DrawLine(origin, rayInfo.point, Color.white);
}
else
{
Destroy(reflection);
Debug.DrawRay(origin, direction, Color.white);
}
}
}
wow, that's far more simple than my system ! I'll test as soon as I can, but anyway thanks you very much ! =D
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Offseting insantiated object 1 Answer
Logic question, unique path finding system 2 Answers
Convert c# to Js? 1 Answer