lisovaccaro
lisovaccaro

Reputation: 33956

Make objects between camera and character transparent

I'm working on a script for my camera to make objects between itself and the character transparent.

I managed to make it work with RayCast however I don't know how to restablish objects alpha value after they escape the ray.

This is my current code:

private void XRay() {
    float characterDistance = Vector3.Distance(transform.position, GameObject.Find("Character").transform.position);
    Vector3 fwd = transform.TransformDirection(Vector3.forward);

    RaycastHit hit;
    if (Physics.Raycast(transform.position, fwd, out hit, characterDistance)) {

        // Add transparence
        Color color = hit.transform.gameObject.renderer.material.color;
        color.a = 0.5f;
        hit.transform.gameObject.renderer.material.SetColor("_Color", color);
    }
}

Upvotes: 0

Views: 6267

Answers (2)

osbor
osbor

Reputation: 51

I did attach this script to my simple camera following the player. It might help you. It can actually manage more than one obstructing the view and also you see I pass it a mask which I target with its name instead of checking for the collider name tag. Have fun.

using UnityEngine;

public class followPlayer : MonoBehaviour
{
    public Transform player;
    public Vector3 offset;
    public Transform[] obstructions;

    private int oldHitsNumber;

    void Start()
    {
        oldHitsNumber = 0;
    }

    private void LateUpdate()
    {
        viewObstructed();
    }

    void Update()
    {
        transform.position = player.TransformPoint(offset);
        transform.LookAt(player);
    }

    void viewObstructed()
    {
        float characterDistance = Vector3.Distance(transform.position, player.transform.position);
        int layerNumber = LayerMask.NameToLayer("Walls");
        int layerMask = 1 << layerNumber;
        RaycastHit[] hits = Physics.RaycastAll(transform.position, player.position - transform.position, characterDistance, layerMask);
        if (hits.Length > 0)
        {   // Means that some stuff is blocking the view
            int newHits = hits.Length - oldHitsNumber;

            if (obstructions != null && obstructions.Length > 0 && newHits < 0)
            {
                // Repaint all the previous obstructions. Because some of the stuff might be not blocking anymore
                for (int i = 0; i < obstructions.Length; i++)
                {
                    obstructions[i].gameObject.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
                }
            }
            obstructions = new Transform[hits.Length];
            // Hide the current obstructions 
            for (int i = 0; i < hits.Length; i++)
            {
                Transform obstruction = hits[i].transform;
                obstruction.gameObject.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
                obstructions[i] = obstruction;
            }
            oldHitsNumber = hits.Length;
        }
        else
        {   // Mean that no more stuff is blocking the view and sometimes all the stuff is not blocking as the same time
            if (obstructions != null && obstructions.Length > 0)
            {
                for (int i = 0; i < obstructions.Length; i++)
                {
                    obstructions[i].gameObject.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
                }
                oldHitsNumber = 0;
                obstructions = null;
            }
        }
    }
}

Upvotes: 1

lisovaccaro
lisovaccaro

Reputation: 33956

This is my final code. Note it only makes transparent one object at a time, but the same implementation can easily be done with RaycastAll and using an array for oldHits.

public class Camara : MonoBehaviour {
    RaycastHit oldHit;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void FixedUpdate() {
        XRay ();
    }

    // Hacer a los objetos que interfieran con la vision transparentes
    private void XRay() {

        float characterDistance = Vector3.Distance(transform.position, GameObject.Find("Character").transform.position);
        Vector3 fwd = transform.TransformDirection(Vector3.forward);

        RaycastHit hit;
        if (Physics.Raycast(transform.position, fwd, out hit, characterDistance)) {
            if(oldHit.transform) {

                // Add transparence
                Color colorA = oldHit.transform.gameObject.renderer.material.color;
                colorA.a = 1f;
                oldHit.transform.gameObject.renderer.material.SetColor("_Color", colorA);
            }

            // Add transparence
            Color colorB = hit.transform.gameObject.renderer.material.color;
            colorB.a = 0.5f;
            hit.transform.gameObject.renderer.material.SetColor("_Color", colorB);

            // Save hit
            oldHit = hit;
        }
    }
}

Upvotes: 1

Related Questions