- Home /
My camera script is having a lot of problems.....
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
my camera would cast a ray facing then have the ray face the player(working)
based on q and e input my camera would change rotation(working)
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 ;-;)
once the ray stops colliding with the wall the wall returns to 0% transparent(not working at all)
problems I am facing
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
The walls that do turn 50% transparent stay 50% transparent once they become transparent and don't change back
"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;
}
}
}
}
}
}
}
}
Answer by Creeper_Math · Jan 02, 2017 at 06:00 PM
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
wut, why the fuck is it when other people do it it suddenly works... Anyway, thank you for helping me get a working script but before you go could you explain to me how resetting the object's transparency to 100% makes the script work like bread n butter?
GameObject[] Walls = GameObject.FindGameObjectsWithTag("Wall");
foreach (GameObject item in Walls) {
item.GetComponent<Renderer>().material.color = new Color(1,1,1,1);
}
The "Foreach" goes through every wall and resetting the transparency to 100%, so the walls that are no longer needed to be set to 50% transparency are turned to 100% transparency. The script basically changes the bool (100% or 50%) transparency into a "button", where every update, the button is pushed back up to 100% by the spring (This specific script is the spring), and only the ones that need to be pressed down are once more pressed and set to 50%.
Answer by Hyrtwol · Jan 03, 2017 at 10:02 PM
sounds like at all-nighter ;) 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.
Your answer
Follow this Question
Related Questions
Unity 3D: How to make the gameobject speed increase continuously on tapping quickly? 1 Answer
How would I make a ListChangedEvent? 1 Answer
"Follow Target" script with a prefab (C#) Help me please! 1 Answer
Help with ontrigger enter and exit with UI Display appear and disappear 1 Answer
Spawning 3D Objects according to Data at JSON Matrix 0 Answers