- Home /
Question by
SterlingStudios · May 10, 2014 at 03:44 PM ·
shaderraycasthighlight
Raycast MouseOver: How to get it to go away when mouse is not over
Hi. I am making an indie horror game and I want the player puts their mouse over the object, it makes it highlighted. That part is fine. But I can't figure out why when I take my mouse off of the object, it goes back to a regular diffuse. Here is the code:
function Update()
{
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if ( Physics.Raycast( ray, hit, 2 ) )
{
if ( hit.collider.gameObject.tag == "Highlightable" )
hit.collider.gameObject.renderer.material.shader = Shader.Find ("Self-Illumin/Diffuse");
}
}
Comment
Answer by jolo309 · May 10, 2014 at 04:31 PM
function Update()
{
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if ( Physics.Raycast( ray, hit, 2 ) )
{
if ( hit.collider.gameObject.tag == "Highlightable" )
{
hit.collider.gameObject.renderer.material.shader = Shader.Find ("Self-Illumin/Diffuse");
}
else
{
hit.collider.gameObject.renderer.material.shader = Shader.Find ("Diffuse");
}
}
}
Try that
EDIT: Now that i think it might not work, i can't think straight right now
No, it didn't work :(. It did the same exact thing.
Answer by ulissesparola · Oct 05, 2018 at 08:46 PM
Pretty late, but I did it this way in a 2D game and it's working:
public class MouseController : MonoBehaviour
{
private GameObject _actualObject;
void Update()
{
Vector2 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(point , Vector2.zero, 0f);
if (hit.collider != null)
{
if (_actualObject == null)
{
_actualObject = hit.collider.gameObject;
_actualObject.GetComponent<MouseBehaviour>().MouseEnter();
}
else if (_actualObject != hit.collider.gameObject)
{
_actualObject.GetComponent<MouseBehaviour>().MouseExit();
_actualObject = hit.collider.gameObject;
_actualObject.GetComponent<MouseBehaviour>().MouseEnter();
}
}
else if (_actualObject != null)
{
_actualObject.GetComponent<MouseBehaviour>().MouseExit();
_actualObject = null;
}
}
}