Help with raycasting

I am using Goldstone’s Unity Essentials book, but I need some help understanding the Raycast. I’ve got a raycast that is opening a door, but I want to continue to check to see if the raycast is still hitting the door so that it won’t close until after the player has gone through it.

The code below works until I try to check to see if the player is still in the door (see bold text below)

var doorIsOpen: boolean = false;
var doorTimer:float = 0;
private var currentDoor:GameObject;
var doorOpenTime: float = 3;
var doorOpenSound:AudioClip;
var doorShutSound:AudioClip;

//end of variable declarations
function Update () 
{
var hit : RaycastHit; if (Physics.Raycast(transform.position, transform.forward, hit, 5))
{
	if(hit.collider.gameObject.tag=="outpostDoor"  doorIsOpen == false)
	{
		currentDoor = hit.collider.gameObject;
		Door(doorOpenSound,true, "dooropen", currentDoor);
	}
}
	if(doorIsOpen) 
	{
		Debug.Log("function Update() got called and doorIsOpen is true");
		Debug.Log(currentDoor + " is the current door. doorIsOpen = " + doorIsOpen);
		doorTimer += Time.deltaTime;
		//if (doorTimer > doorOpenTime)

I changed the above to the following line because I wanted the door to stay open if the player is still in the door way. I think I need to send out another raycast here??

		if (doorTimer > doorOpenTime  hit.collider.gameObject.tag=="outpostDoor")
		{
			//shutDoor();  replaced with line below
			Debug.Log("doorTimer > doorOpenTime.  Shutting Door = " + currentDoor );
			Door(doorShutSound, false, "doorshut", currentDoor);
			doorTimer = 0;
		}
	}	
	else 
	{
		Debug.Log("function Update() got called and doorIsOpen is false");
		Debug.Log("doorTimer = "+ doorTimer+ " and doorOpenTime = "+ doorOpenTime);
	}
}

/*function OnControllerColliderHit (hit : ControllerColliderHit)
{
	if(hit.gameObject.tag == "outpostDoor"  doorIsOpen == false)
	{	//OpenDoor();
		Debug.Log("hit.gameObject = " + hit.gameObject);
		currentDoor =hit.gameObject;
		Door(doorOpenSound, true, "dooropen", currentDoor);
	}
} */
function Door(aClip : UnityEngine.AudioClip, openCheck : boolean, animName : String, thisDoor: GameObject)
{
	Debug.Log("Calling function Door(aClip = " + aClip + "openCheck = " + openCheck+ "animName "  +animName + " thisDoor =  " + thisDoor); 

	audio.PlayOneShot(aClip);
	doorIsOpen = openCheck;
	thisDoor.transform.parent.animation.Play(animName);
	// thisDoor.animation.Play(animName); WON'T work b/c thisDoor does not have an animation
	//the animation belogs to the pareent object, so we must explicitly invoke the parent's animation
}

@script RequireComponent(AudioSource)

I would suggest that an easier way to handle this situation is to use a trigger in the doorway. A trigger is just a normal collider with the Is Trigger property enabled. In this state, it won’t actually collide with incoming objects, but will register their presence with the OnTriggerEnter, OnTriggerExit and OnTriggerStay functions. In your case, you would probably want code in OnTriggerEnter to open the door when the player approaches the doorway. Then, you could use OnTriggerExit to close the door again when the player has passed through.

Yea - that probably is a better approach. Thanks! :smile: