What I want my script to do.

I want my script to be able to see my player object through a wall by 50% opacity when ever the player’s view is obstructed by a wall

How I wanted my script work

  1. my camera would cast a ray facing then have the ray face the player(working)
  2. based on q and e input my camera would change rotation(working)
  3. if the ray hit a wall obstructing the path of the ray the wall in-front of it would turn transparent(Only works on some walls of the walls ;-:wink:
  4. once the ray stops colliding with the wall the wall returns to 0% transparent(not working at all)

problems I am facing

  1. Some walls turn 50% transparent and some don’t(they have the exact same settings and the exact same tags and the exact same components

  2. The walls that do turn 50% transparent stay 50% transparent once they become transparent and don’t change back

  3. “far” which is for my debug log for telling me a ray cast isn’t hitting anything isn’t triggering.

Could you help me with my script? I have to turn in my game for g.t by tommrow and this is one of the last things I need fixed ;-;

camera script

using UnityEngine;
using System.Collections;

public class CameraControll : MonoBehaviour
{
    Vector3 Campos;
    public Transform player;
    public GameObject Player;
    int count = 0;
    public float Distance;
    private GameObject block;
    

    public void Update()
    {
        Vector3 Direction = Player.transform.position - transform.position;
        RaycastHit hit;
        Ray LandingRay = new Ray(transform.position, Vector3.forward);
        Debug.DrawRay(transform.position, Direction * Distance);
        if (Physics.Raycast(LandingRay, out hit, Distance))
            
        {
            if(hit.collider.tag == "Wall")
            {
                
                    Debug.Log("close");
                    block = hit.collider.gameObject;
                    block.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 0.5f);
                
             }
            else
            {  
                Debug.Log("Far");
                 block = hit.collider.gameObject;
                block.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 1);
            }
        }

        if (Input.GetKeyDown("q"))
        {
            count++;
        }
        else
        {
            if (Input.GetKeyDown("e"))
            {
                count--;
            }
        }
        transform.position = Player.transform.position + Campos;
        transform.LookAt(player);


       
    }

    public void LateUpdate()
    {
        if (count == 0)
        {
            Campos = new Vector3(0f, 5f, -7f);
            Debug.Log(count);
        }
        else
        {
            if (count == 1)
            {
                Campos = new Vector3(7f, 5f, 0f);
                Debug.Log(count);
            }
            else
            {
                if (count == 2)
                {
                    Campos = new Vector3(0f, 5f, 7f);
                    Debug.Log(count);
                }
                else
                {
                    if (count == 3)
                    {
                        Campos = new Vector3(-7f, 5f, 0);
                        Debug.Log(count);
                    }
                    else
                    {
                        if (count <= -1)
                        {
                            count = 3;
                        }
                    else
                        {
                            if (count <= 4)
                            {
                                count = 0;
                            }
                        }
                    }
                }
            }
        }
    }
}

I would “reset” all the gameobject’s transparency to 100% DIRECTLY before you set the specific object’s transparency that you need. This could be the following script (placed at the very top of the update script

GameObject[] Walls = GameObject.FindGameObjectsWithTag("Wall");
foreach (GameObject item in Walls) {
     item.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
}

This resets all the walls before the camera sets the specific ones that it needs to be transparent

(This is the problem that was occuring) The Camera’s “hit” tag would ALWAYS either be the player or a wall that NEEDED to be set to 50% transparency, so the following script would only come up when the camera has a direct line of sight to the player, in which the “hit” would always be the player

Script portion that would only occur if the camera had direct line of sight to player, and only affected the “hit” object of the player

else
             {  
                 Debug.Log("Far");
                  block = hit.collider.gameObject;
                 block.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 1);
             }

As of the ray casting and only SOME walls changing problem

Your “Debug Ray” and your “Lading Ray” are hitting TWO different positions

Ray LandingRay = new Ray(transform.position, Vector3.forward);
// Landing Ray is showing directly forward of the camera


Debug.DrawRay(transform.position, Direction * Distance);
// Debug Ray is going between the player and the camera

You need to set them to the SAME ray, which could be the following code

Ray LandingRay = new Ray(transform.position,player.transform.position - transform.position);

This way the script is going by the same ray that is being displayed by the Debug Ray

sounds like at all-nighter :wink: i would be careful with the material property “Returns the first instantiated Material assigned to the renderer.”
so setting the color can result in a new instance of the material on that render.
prepare the wall mats in a Start() so you’re sure they dont share materials.