Ok, here we go.
I have a tiled map for my game. It consists of a couple of boxes, that must elevate on raycast. The question is: How do i stop the ray when it hits any box of my map? The code below is attached to each of the tiles. Is there a better way to achieve my requirements?
C#:
using UnityEngine;
using System.Collections;
public class TileOnMouseOver : MonoBehaviour
{
public float deltaHeight = 0.1f;
public Color customCol;
Color normalCol;
Vector3 normalTransform;
bool once = true;
// Use this for initialization
void Start()
{
normalCol = renderer.material.color;
normalTransform = transform.position;
}
// Update is called once per frame
void Update()
{
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (collider.Raycast(ray, out hitInfo, Mathf.Infinity))
{
if (once)
{
Vector3 deltaVect = transform.position;
deltaVect.y += deltaHeight;
renderer.material.color = customCol;
transform.position = deltaVect;
once = false;
}
}
else
{
renderer.material.color = normalCol;
transform.position = normalTransform;
once = true;
}
}
}
And some screenshots to clarify my question:
(The upper tile blinks)