[SOLVED]Finding when a raycast has left an Object (C#)

Hi all,

How do I check if a ray is not on an object?

I have a scene which only has 5 or 6 GameObjects that need to be ‘Rolled-Over’. So most of the time the ray is hitting nothing.

I have a Physics Ray which is constructed thus:

// Calculate NPC Ray
        if (Physics.Raycast(NPCray, out hit, 100.0f, NPCplayerMask))
        {
            //stores the object hit            
            collider1 = hit.collider;

            Debug.Log(collider1.name);
        }

and a function that returns the hit collider:

// Update is called once per frame
	void Update () {

        if (RayCastAndCameraEffects.collider1 == null)
            return;

        // Grab the NPC GameObject from the Collider
        Collider1Object = GameObject.Find(RayCastAndCameraEffects.collider1.name);

        // Find the position of the NPC
	    pivotPosition = Collider1Object.transform.position;

        // Rotate the Camera Pivot and position on top of NPC
	    transform.RotateAround(Vector3.up, CameraRotationSpeed * Time.deltaTime);
	    transform.position = pivotPosition;
	}

This is all working fine BUT when the ray hits an object for the very first time, the collider gets stored and remembered until the ray hits another object. What I want is for the collider to return nothing as soon as the ray moves off it?

I need a rollover effect.

Cheers,
Matt.

I’ve manage to get around the problem. I am using a second camera to show the object on a RenderTexture, so made this camera only see the required layer. The end result is what i required, so i’m happy.

Matt.