I’ve been trying to figure this out for days, trying out various techniques to no success. Note that I am not using a Third Person Character Controller because my object uses ball movement.
Here’s what I currently have for my Third Person Camera. This creates a ray that always points directly at the player. It gets an array of all objects it points at. If these are not player, and they have an OccludeWhenPointed script, it calls the TogglePointed function :
float m_dist;
void Start ()
{
//Create a ray at the object's position and rotation
m_dist = GetComponent<ThirdPersonCamera>().GetDistance();
}
// Update is called once per frame
void Update ()
{
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = Camera.main.ScreenPointToRay(new Vector3(x, y, 0));
RaycastHit hit;
RaycastHit[] allHits = Physics.RaycastAll(ray, m_dist);
foreach (var rayHit in allHits)
{
if (rayHit.collider.gameObject.CompareTag("Player"))
{
}
else if (!rayHit.collider.gameObject.layer.Equals("EditorCameraIgnore") && !rayHit.collider.gameObject.CompareTag("Ground"))
{
//Call HitByRay function
if(rayHit.collider.gameObject.GetComponent<OccludeWhenPointed>() != null)
{
//print("Ready to point");
rayHit.collider.gameObject.GetComponent<OccludeWhenPointed>().TogglePointed();
}
//Material e_mat = rayHit.collider.gameObject.GetComponent<MeshRenderer>().material;
}
}
Here's the pseudocode I've been trying to follow:
//keep the ray in the camera's position and rotation
//if the ray is colliding with something other than the player ball,
//If there is no transparency material of the object
//Add a transparency material to it
//Else
//Set object's alpha to a lower number
//if the ray is not colliding with it
//Set object's alpha to full
}
}
Meanwhile, each object that I want to be transparent has both a material in Transparent rendering mode and this script. When it’s pointed at, it reduces its alpha to 0.5:
using UnityEngine;
using System.Collections;
public class OccludeWhenPointed : MonoBehaviour {
[SerializeField]
bool m_PointedAtByRay = false;
bool m_Switched = false;
float m_OcclusionTime = 1.0f;
float m_Timer;
Color newColor;
Material mat;
void Start()
{
mat = GetComponent<MeshRenderer>().material;
m_Timer = m_OcclusionTime;
}
public void TogglePointed()
{
if (m_PointedAtByRay == false)
m_PointedAtByRay = true;
else
m_PointedAtByRay = false;
m_Switched = false;
}
// Update is called once per frame
void Update ()
{
if(m_PointedAtByRay)
{
print("Pointing is " + m_PointedAtByRay);
print("Switched is " + m_Switched);
if (!m_Switched)
{
print("Turning Transparent");
newColor.a = 0.5f;
mat.color = newColor;
m_Switched = true;
}
if (m_Timer > 0)
{
m_Timer -= Time.deltaTime;
}
else
{
TogglePointed();
m_Timer = m_OcclusionTime;
}
}
else
{
if (m_Switched)
{
print("Un-Turning Transparent");
newColor.a = 1.0f;
mat.color = newColor;
m_Switched = false;
}
}
}
}
The result I do get is that when my camera points at objects, they still look solid.
If anyone knows how to solve this problem, or more efficient alternatives that can be instructed step-by-step, your help would be greatly appreciated.