I'm making a game where camera follows the player (isometric view - kind of) and the player is moving around underground rock cave. Sometimes the camera gets behind an obstacle (or inside a model for example) and to still have the player game object visible, I made this script:
public class ObstructionDetectorBehaviour : MonoBehaviour {
public Transform TeamTransform;
public Color TransparentColor;
public void FixedUpdate() {
var direction = (Camera.main.transform.position - TeamTransform.position).normalized;
var hits = Physics.RaycastAll(TeamTransform.position, direction, 100);
foreach (var hit in hits) {
if (hit.collider.gameObject.layer != 8) {
continue;
}
var renderer = hit.collider.gameObject.GetComponent<Renderer>();
renderer.materials[0].shader = Shader.Find("Transparent/Diffuse");
renderer.materials[0].color = TransparentColor;
}
}
}
As you can see it disables the visibility of models between the player and the main camera.
Now, as the player controls where his character is moving by clicking the mouse (scene is divided into tiles), I would also like to be able to know where (on which tile) he clicked, so I did this:
public class PlaneBehaviour : MonoBehaviour, IPointerDownHandler {
public GameObject ClickSymbol;
public void Start() {
var physicsRaycaster = FindObjectOfType<PhysicsRaycaster>();
if (physicsRaycaster == null) {
Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
}
}
public void OnPointerDown(PointerEventData eventData) {
var o = eventData.pointerCurrentRaycast.gameObject;
}
}
There's a problem however, because the OnPointerDown
method is not being invoked all the times it should be. Reason for that is the fact that my underground maze walls have box colliders attached and sometimes the EventSystem
component shows me that the mouse is hovering over a rock (the wall) and not a tile - because the rock collider is partially covering the tile.
So what are my options here?
How to make object behind a collider clickable?