Home > Blockchain >  Stop outlining object when no longer looking at it?
Stop outlining object when no longer looking at it?

Time:06-01

I'm trying to make a pickup system and I thought it would be cool to do an outline around the item when you're looking at it. The issue I'm facing though is when you're no longer looking at the object I need to disable the outline. I ended up doing an odd solution and would like to get some help improving it.

public class PlayerCamera : MonoBehaviour
{
    public Transform playerBody;
    public Transform cameraHolder;

    public float sensitivity;
    public float currentY;

    void Update()
    {
        MoveCamera();
        LookingAtObject();
    }

    Outline objectOutline;
    void LookingAtObject()
    {
        if(Physics.Raycast(cameraHolder.transform.position, cameraHolder.transform.forward, out var hit, Mathf.Infinity))
        {
            var obj = hit.collider.gameObject;
            var outline = obj.GetComponent<Outline>();

            if (obj && outline)
            {
                objectOutline = hit.transform.GetComponent<Outline>();
                if (objectOutline)
                    objectOutline.OutlineWidth = 7;
            }
            else if (objectOutline)
                objectOutline.OutlineWidth = 0;
        }   
    }
}

CodePudding user response:

You can store the outlined object in a variable, and whenever you hit a different outline object or hit nothing, set the outline back to zero.

Outline objectOutline;

void LookingAtObject()
{
    if (Physics.Raycast(...))
    {
        var outline = hit.collider.GetComponent<Outline>();

        // Make sure the hit object is not the same one we already outlined
        //
        if (outline != objectOutline)
        {
            // Remove the outline from our previously viewed object
            //
            if (objectOutline != null)
            {
                objectOutline.OutlineWidth = 0;
            }
            
            // Store the new outline object
            //
            objectOutline = outline;

            // Since outline could be null, we need to check null before outlining
            //
            if (objectOutline != null)
            {
                objectOutline.OutlineWidth = 7;
            }
        }
    }

    // If we have an object we outlined and we didnt hit anything, 
    // remove the outline and reset the variable
    //
    else if (objectOutline != null)
    {
        objectOutline.OutlineWidth = 0;
        objectOutline = null;
    }
}

CodePudding user response:

You need two events to solve the problem. Input frame and ray output frame. This code detects which raycast event is by recording the previous raycastHit frame and comparing it to the current hit, and sets the outline accordingly.

private RaycastHit lastHit;
void Update()
{
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    Physics.Raycast(ray, out var hit);
    //Physics.Raycast(cameraHolder.transform.position, cameraHolder.transform.forward, out var hit, Mathf.Infinity);

    if (hit.transform != lastHit.transform)
    {
        if (hit.transform) // when raycast Begin
        {
            var outline = hit.transform.GetComponent<Outline>();

            outline.OutlineWidth = 7;
        }
        else if (lastHit.transform) // when raycast out
        {
            var outline = lastHit.transform.GetComponent<Outline>();
            outline.OutlineWidth = 0;
        }
    }
    
    lastHit = hit;
}

Hint: I commented on your raycast code for testing. If you want to change the raycast code as before.

  • Related