I'm a beginner in Unity and i'm facing a little problem / question. The main purpose of my 2D game is based on the mouvment of a specific light and the player have to avoid getting inside his rays. My actual game got different type of layers :
- Z = -5 : A front light (don't pay attention of this one)
- Z = 0 : The main scene, where the player move and interact with collider (2D based, player is a sprite with rigidbody2d and circlecollider2d
- Z = 1 : Some elements to avoid the light to go through and give the player a chance to hide
- Z = 10 : The main light that the player have to avoid
My problem is the following one, i don't know how to interact a Point Light (so a 3d object) and a sprite (2D). I tried to do some "OnTriggerEnter / Stay" in the Point Light but it never trigger. I heard about "Raycast" but i prefer to ask some solution before learning this. If anyone can give me tips, it would be very good :)
Thanks !
[EDIT SOLUTION, THANKS TO Géry Arduino]
#pragma strict
public var moon : Component;
function Start () {
}
function Update () {
var radiusMoon = moon.transform.light.range;
var zMoon = moon.transform.position.z;
var zPlayer = transform.position.z;
if(zMoon - radiusMoon <= zPlayer) {
var zDirection = transform.position.z - zMoon;
var xDirection = transform.position.x - moon.transform.position.x;
var yDirection = transform.position.y - moon.transform.position.y;
var direction = Vector3(xDirection, yDirection, zDirection);
Debug.DrawRay(moon.transform.position, direction, Color.red, 10);
if(Physics.Raycast(moon.transform.position, direction, radiusMoon)) {
Debug.Log("there is something between me and the object....");
}
else {
Debug.Log("i'm free");
}
}
}