Problem with RaycastHit !

Hi , I am learning unity with VTC tutorial , and in on of tutorial there is door and when character goes near the door , the door will open automatically … here is the code :

function update ()
{
	var hit : RaycastHit;
	
	if(Physics.Raycast(transform.position, transform.forward, hit, 5))
	{
		
		if(hit.collider.gameObject.tag == "door")
		{
			hit.collider.gameObject.animation.Play("door_open");
		}
	}
}

I am done exactly what the author did ! but when the character goes near the door , nothing happens !

break it down

print(Physics.Raycast(transform.position, transform.forward));
print(Physics.Raycast(transform.position, transform.forward, 5));
var hit : RaycastHit;
print(Physics.Raycast(transform.position, transform.forward, hit, 5));
var hit : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, hit)){
print(hit.collider.gameObject.tag);
}

and so on…

Thank you very much it works fine now !

function Update ()
{
var hit : RaycastHit;
	
	if(Physics.Raycast(transform.position, transform.forward, hit, 5))
	{
		
		if(hit.collider.gameObject.tag == "door")
		{
			print(hit.collider.gameObject.animation.Play("door_open"));
			
		}
	}
}

dont raycast to open a door when youre close to it. use a trigger

probably doing a Tornado Twin tutorial

Most people who are learning don’t really know all of the options you can use to get things done. They work solely to one end and ask questions based on that end. (This is why I answer questions based on the example they give, instead of coming back with “don’t do it that way, this way is better”)

Most of the time people are looking for it to work, rather for it to work fast. A person who gets something to work, even if not great, will likely go on to learn the “right” way to do things, but a person who doesn’t get what they are working on will generally tend to quit.

true. i was just saying the simpler way