How to check if the reycast has stoped hitting a object

52 Views Asked by At

I Want it to set the material back to the material i setup when the reycast isint hitting the object anymore i only want it to be the hightlight material when its hitting it, Thanks in advance

private void OnMouseDrag()
{
    

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    Vector3 rayPoint = ray.GetPoint(distance);
    rayPoint.y = 2;
    transform.position = rayPoint;


    //  Debug.Log("m: " + Input.mousePosition); 
    //  Debug.Log("r: " + rayPoint);

    RaycastHit hit;

    Ray BoardCheck = new Ray(transform.position, Vector3.down);

    if (Physics.Raycast(BoardCheck, out hit))
    {


        if (hit.collider.tag == "BoardBlock")
        {
           // Debug.Log("Hit" + hit.collider.gameObject);
            RaycastHitObject = hit.collider.gameObject;


            RaycastHitObject.GetComponent<Renderer>().material = Hilight;
        }

    }

    
}

//IF MORE INFO IS NEEDED JUST ASK

}

2

There are 2 best solutions below

1
anjelomerte On

A simple if else should do. If you hit your object, change its appearance, if not, change it back. All you need to do is to keep a reference to the object. Assign it via the inspector or inside the if on first hit. You already seem to be doing it (RaycastHitObject) but don't seem to make use of it.

// Target gameobject to change material of
GameObject target;

RaycastHit hit;
Ray BoardCheck = new Ray(transform.position, Vector3.down);

// Perform raycast
if (Physics.Raycast(BoardCheck, out hit))
{
    // Compare collider tag
    if (hit.collider.tag == "BoardBlock")
    {
        // Keep reference
        target = hit.collider.gameObject;

        // Change material
        target.GetComponent<Renderer>().material = highlightMat;
    }
    else
    {
        // Change back material
        target.GetComponent<Renderer>().material = nonHighlightMat;
    }
}
0
derHugo On

You need to store the hit target

Before coming to that personally I would use a dedicated component like e.g.

public class Focusable : MonoBehaviour
{
    [SerializeField] private Renderer _renderer;

    private Material idleMaterial;
    [SerializeField] private Material focusedMaterial;

    private void Awake ()
    {
        if(!_renderer) _renderer = GetComponent<Renderer>();

        idleMaterial = _renderer.material;
    }

    private bool isFocused;
    public bool IsFocused
    {
        get => _isFocused;
        set
        {
            _isFocused = value;
            _renderer.material = value ? focusedMaterial : idleMaterial;
        }
    }
}

And then in your controller do

// Stores the currently hit object
private Focusable currentTarget;

private void OnMouseDrag()
{
    // I will just trust that you know what you do with those rays
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var rayPoint = ray.GetPoint(distance);
    rayPoint.y = 2;
    transform.position = rayPoint;

    var BoardCheck = new Ray(transform.position, Vector3.down);
  
    if (Physics.Raycast(BoardCheck, out var hit) 
        // instead of the tag you can now directly check for the component
        // as this is optional you could of course also just stick to the tag if you want to 
        && hit.collider.TryGetComponent<Focusable>(out var focusable))
    {
        // -> we are hitting one of our objects 

        // if it is still the same as before we don't need to do anything 
        if (currentTarget != focusable)
        {
            if(currentTarget)
            {
                // did we hit  another one before? -> unfocus
                currentTarget.IsFocused = false;
            }

            // now focus and store this hit target once
            currentTarget = focusable;
            currentTarget.IsFocused = true;
        }
    }
    else
    {
        // we don't hit a focusable at all 
    
        if (currentTarget)
        {
            // if there was a previous hit unfocus it once
            currentTarget.IsFocused = false;
            currentTarget = null;
        }
    }
}