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.
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.
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.
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.
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.
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).
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.
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
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?