Skip to main content
Corrected to take the distance in account
Source Link

Actually Lights does not contain colliders, but what you can do is add a CircleCollider2D component to a light, then track if your object collide with the collider of this light and only then trace a ray from your object (character) to the light, if the ray hits a collider before hitting the light then you know your character is in the shadow part of this light!

I am not sure you could go away with this without using raycast2D, but dont be afraid once you get a hold on it it can be very straight-forward.

For example to put on your character :

private bool inTheShadow = false;

void Update() {
    if(!inTheShadow) {
        //Replace following lines with your own magic
        Debug.Log("Too much Light!!");
    }
    else { Debug.Log("Vampire Secure Area!!"); }
}

void OnCollisionStay2D(Collision2D coll) {
    
    if (coll.gameObject.tag == "Light") {//is that component a light? (need to have the light tag on your light
        RaycastHit2Dfloat hitdistance = Physics2DVector3.Raycastdistance(transform.position,coll.gameObject.transform.position);
        Vector3 rayDirection = (coll.gameObject.transform.position-transform.position).normalizednormalized;
        RaycastHit2D hit = Physics2D.Raycast(transform.position,rayDirection,distance);
        if(hit.collider!=coll) { //Am i hitting the light or smthing else?
            inTheShadow = true;
        }
        else {inTheShadow = false;}
    }  
}

This is a non optimized version done quickly without unity or anything related near hand

Actually Lights does not contain colliders, but what you can do is add a CircleCollider2D component to a light, then track if your object collide with the collider of this light and only then trace a ray from your object (character) to the light, if the ray hits a collider before hitting the light then you know your character is in the shadow part of this light!

I am not sure you could go away with this without using raycast2D, but dont be afraid once you get a hold on it it can be very straight-forward.

For example to put on your character :

private bool inTheShadow = false;

void Update() {
    if(!inTheShadow) {
        //Replace following lines with your own magic
        Debug.Log("Too much Light!!");
    }
    else { Debug.Log("Vampire Secure Area!!"); }
}

void OnCollisionStay2D(Collision2D coll) {
    
    if (coll.gameObject.tag == "Light") {//is that component a light? (need to have the light tag on your light
        RaycastHit2D hit = Physics2D.Raycast(transform.position, (coll.gameObject.transform.position-transform.position).normalized);
        if(hit.collider!=coll) { //Am i hitting the light or smthing else?
            inTheShadow = true;
        }
        else {inTheShadow = false;}
    }  
}

This is a non optimized version done quickly without unity or anything related near hand

Actually Lights does not contain colliders, but what you can do is add a CircleCollider2D component to a light, then track if your object collide with the collider of this light and only then trace a ray from your object (character) to the light, if the ray hits a collider before hitting the light then you know your character is in the shadow part of this light!

I am not sure you could go away with this without using raycast2D, but dont be afraid once you get a hold on it it can be very straight-forward.

For example to put on your character :

private bool inTheShadow = false;

void Update() {
    if(!inTheShadow) {
        //Replace following lines with your own magic
        Debug.Log("Too much Light!!");
    }
    else { Debug.Log("Vampire Secure Area!!"); }
}

void OnCollisionStay2D(Collision2D coll) {
    
    if (coll.gameObject.tag == "Light") {//is that component a light? (need to have the light tag on your light
        float distance = Vector3.distance(transform.position,coll.gameObject.transform.position);
        Vector3 rayDirection = (coll.gameObject.transform.position-transform.position).normalized;
        RaycastHit2D hit = Physics2D.Raycast(transform.position,rayDirection,distance);
        if(hit.collider!=coll) { //Am i hitting the light or smthing else?
            inTheShadow = true;
        }
        else {inTheShadow = false;}
    }  
}

This is a non optimized version done quickly without unity or anything related near hand

added 181 characters in body
Source Link

Actually Lights does not contain colliders, but what you can do is add a 2D circle colliderCircleCollider2D component to a light, then track if your object collide with the collider of this light and only then trace a raytrace a ray from your object (character) to the light, if the ray hits a collider before hitting the light then you know your character is in the shadow part of this light!

I am not sure you could go away with this without using raycast2D, but dont be afraid once you get a hold on it it can be very straight-forward.

For example to put on your character :

private bool inTheShadow = false;

void Update() {
    if(!inTheShadow) {
        //Replace following lines with your own magic
        Debug.Log("Too much Light!!");
    }
    else { Debug.Log("Vampire Secure Area!!"); }
}

void OnCollisionStay2D(Collision2D coll) {
    
    if (coll.gameObject.tag == "Light") {//is that component a light? (need to have the light tag on your light
        RaycastHit2D hit = Physics2D.Raycast(transform.position, (coll.gameObject.transform.position-transform.position).normalized);
        if(hit.collider!=coll) { //Am i hitting the light or smthing else?
            inTheShadow = true;
        }
        else {inTheShadow = false;}
    }  
}

This is a non optimized version done quickly without unity or anything related near hand

Actually Lights does not contain colliders, but what you can do is add a 2D circle collider component to a light, then track if your object collide with the collider of this light and only then trace a ray from your object (character) to the light, if the ray hits a collider before hitting the light then you know your character is in the shadow part of this light!

I am not sure you could go away with this without using raycast2D, but dont be afraid once you get a hold on it it can be very straight-forward.

For example to put on your character :

private bool inTheShadow = false;

void Update() {
    if(!inTheShadow) {
        //Replace following lines with your own magic
        Debug.Log("Too much Light!!");
    }
    else { Debug.Log("Vampire Secure Area!!"); }
}

void OnCollisionStay2D(Collision2D coll) {
    
    if (coll.gameObject.tag == "Light") {//is that component a light? (need to have the light tag on your light
        RaycastHit2D hit = Physics2D.Raycast(transform.position, (coll.gameObject.transform.position-transform.position).normalized);
        if(hit.collider!=coll) { //Am i hitting the light or smthing else?
            inTheShadow = true;
        }
        else {inTheShadow = false;}
    }  
}

This is a non optimized version done quickly without unity or anything related near hand

Actually Lights does not contain colliders, but what you can do is add a CircleCollider2D component to a light, then track if your object collide with the collider of this light and only then trace a ray from your object (character) to the light, if the ray hits a collider before hitting the light then you know your character is in the shadow part of this light!

I am not sure you could go away with this without using raycast2D, but dont be afraid once you get a hold on it it can be very straight-forward.

For example to put on your character :

private bool inTheShadow = false;

void Update() {
    if(!inTheShadow) {
        //Replace following lines with your own magic
        Debug.Log("Too much Light!!");
    }
    else { Debug.Log("Vampire Secure Area!!"); }
}

void OnCollisionStay2D(Collision2D coll) {
    
    if (coll.gameObject.tag == "Light") {//is that component a light? (need to have the light tag on your light
        RaycastHit2D hit = Physics2D.Raycast(transform.position, (coll.gameObject.transform.position-transform.position).normalized);
        if(hit.collider!=coll) { //Am i hitting the light or smthing else?
            inTheShadow = true;
        }
        else {inTheShadow = false;}
    }  
}

This is a non optimized version done quickly without unity or anything related near hand

Source Link

Actually Lights does not contain colliders, but what you can do is add a 2D circle collider component to a light, then track if your object collide with the collider of this light and only then trace a ray from your object (character) to the light, if the ray hits a collider before hitting the light then you know your character is in the shadow part of this light!

I am not sure you could go away with this without using raycast2D, but dont be afraid once you get a hold on it it can be very straight-forward.

For example to put on your character :

private bool inTheShadow = false;

void Update() {
    if(!inTheShadow) {
        //Replace following lines with your own magic
        Debug.Log("Too much Light!!");
    }
    else { Debug.Log("Vampire Secure Area!!"); }
}

void OnCollisionStay2D(Collision2D coll) {
    
    if (coll.gameObject.tag == "Light") {//is that component a light? (need to have the light tag on your light
        RaycastHit2D hit = Physics2D.Raycast(transform.position, (coll.gameObject.transform.position-transform.position).normalized);
        if(hit.collider!=coll) { //Am i hitting the light or smthing else?
            inTheShadow = true;
        }
        else {inTheShadow = false;}
    }  
}

This is a non optimized version done quickly without unity or anything related near hand