Open door?

Sorry for asking again, but how can I open a door using E? Here is the unfinished code I have now:

var SoundOpen : UnityEngine.AudioClip;
var SoundClose : UnityEngine.AudioClip;
var Door : GameObject;
var hit : RaycastHit;
var rayCastLength = 5;
function Update () {

if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))
{
if(hit.collider.gameObject.tag == "Door")
{
print ("Door Opened");
hit.collider.gameObject.animation.Play("door animation_Open");
}
}

}

The problem is that nothing happens when I reach the door, not even the print command. I have copied some of this code from a tutorial, where it worked, and changed a few things for my own door. But the raycast does not seem to work.

Please keep in mind that this is a partially finished code to see if it will work at all. I planned to add all the extras later. I might have made some mistake with the var’s, but please ignore that if it is irrelevant to the script working for now.

Try something like:
hit.collider.gameObject.transform.parent.animation.Play(“Open”);

this because, at least on mine, the collider is actually on the ‘mesh objects’ (don’t know what the real name is) and not on the game object itself and the animation is on the main object.
Also if the collider is actually on the ‘mesh objects’ you have to put the tag on them, not on the main object.

Or you could just add a collider to the main object and be done with it. :slight_smile:

Hope this helps.

Thanks, but still does not seem to work. I have a mesh object with a handle (The handle being the child). I placed the script onto the door… I mean there is no prefab, only a mesh meshes with the script. The tag is correct so it could not be the problem. I have a box collider, so no, it does not solve anything. You do not see a problem with the script? I will try it on a different object.

No, does not work on a cube either.

im searching for that too
hoping that someone will give a full script that works

Have you tried changing the concept to work with triggers instead of physics collisions? So when you enter the trigger it opens the door. You could have 2 triggers, on on either side that opens the door to either side. Just have it timed to close…

OR… you could have a trigger that says, “I can open you now” then have some user interaction to be able to open the door.

Lastly, You can have an event that only happens when the player clicks on the door, and only when they are within a certain range of it.

Using the bump physics method is not the best way IMO to do that.

Ok, I will try something later.

Try debugging your code a little before giving up dude, does “Door Opened” get printed?

If so, try making the door disappear first to make sure it’s not a problem with your animation.

If it’s not, debug out the collider the ray cast is hitting, and make sure the tag is incorrect.

After that, debug out the direction the ray is being sent.

var SoundOpen : UnityEngine.AudioClip;
var SoundClose : UnityEngine.AudioClip;
var Door : GameObject;
var hit : RaycastHit;
var rayCastLength = 5;
function Update () {[INDENT][COLOR=red]Debug.DrawRay(transform.position, transform.forward);[/COLOR]
[/INDENT][INDENT] if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))
{[INDENT][COLOR=red]print(hit.collider.gameObject.name + ", " + hit.collider.gameObject.tag);[/COLOR]

if(hit.collider.gameObject.tag == "Door")
{[INDENT] print ("Door Opened");
[COLOR=red]hit.collider.gameObject.SetActiveRecursively(false);[/COLOR]
[/INDENT]}
[/INDENT][/INDENT][INDENT] }
[/INDENT]}

I am not giving up, but thanks for the advice. I am not too sure, but I think the only requerment to try out the script is a door tag, so feel free to try it.

what I did is this here’s the script, using trigger.

var doorOpenSound : AudioClip;
var doorCloseSound : AudioClip;
var doorIsOpen = false;

function Update()
{
     print(doorIsOpen);
}

function OnTriggerStay(hit : Collider)
{
	 if(hit.gameObject.tag == "Player")
	{
	     var door : GameObject = GameObject.Find ("door");
	     if (Input.GetButtonDown ("Button A"))
		 {
		     if  (doorIsOpen)
			     {
			         audio.PlayOneShot (doorOpenSound);	 
	                 door.animation.Play ("buka");
	                 doorIsOpen = false;
			     } 
			     else
			     {
					 audio.PlayOneShot (doorCloseSound);	 
	                 door.animation.Play ("tutup");
	                 doorIsOpen = true;
			     }
		}
	}
}

@script RequireComponent(AudioSource)

Ok sereda008, I used a cube and tested your original code.
It works perfectly.
So I modified it to work with the keypress

var SoundOpen : UnityEngine.AudioClip;
var SoundClose : UnityEngine.AudioClip;
var Door : GameObject;
var hit : RaycastHit;
var rayCastLength = 5;

function Update () {
        if (Input.GetButtonDown("Interact"))
        {
        open_door();
        }
        }

function open_door()
{
if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))
{
if(hit.collider.gameObject.tag == "Door")
{
print ("Door Opened");
hit.collider.gameObject.animation.Play("opendoor");
}
}
}

Still worked great.
You can try it HERE, press ‘e’ near the cube and it will spin (which would be the opening animation).

Where are you attaching the script?

Could you please tell me what you did? It does not work, even after I modified some of the code for it to do so. I cannnot find what is wrong with what I did.

I pretty much just attached the script to a “First Person Controller” and it worked.

Also at least one of your objects has to have a rigidbody and they both need box colliders (I believe) when I’m troubleshooting collision I first put a rigidbody on both my objects along with a collider on both of em, just to be sure :wink:

by the way did you make sure that you door that you are trying to open has an animation on it? like an animation of opening the door and another one closing it?

Yes, the animation is there. I did not attach it to the player, I will experiment with that and tell you if it works.