Door Opening Script

Hello, I’m new to Unity scripting and am writing a script for opening a door in my FPS game. However, I’m trying to make the door stop opening/ closing if it collides with a human controller (or an AI). I tried using the OnCollisionEnter function, but realize this does not detect a collision if there is no rigidbody involved. I also made the character controller send a message to the door if it hits it, however this only happens if the character runs into the door rather than the door running into the character. I’m wondering what function I could use that would detect a collision of two colliders rather than a collider and a rigidbody or two rigidbodies. Thanks!

Not quite clear what the problem is, but you might try raycasting instead of collisions to activate your door. For one thing, raycasting from your character allows you to detect the door when he/she is a few feet away. Also, the door won’t open unless the character is actually facing it. With colliders, even a glancing hit on the door will open it… The scripting reference gives some good examples, but if you need help, let me know. You might want to take a look at Will Goldstone’s site, unity3dstudent.com, and look over the videos starting with “essentials”. He goes into collisions and raycasting in a very clear way.

Here is my script. You are a bit confused on the functionality of the door. It opens if you are within a certain distance and press the action key. This part works fine. However, if you are standing in the way of the door, the door will move through the human controller. I’m trying to find a script function that will detect this collision and cause the door to pause opening/ closing.

Thanks for the link on the essentials! I am familiar with coding in Java and C, so most of the programming isn’t too new to me (although javascript is different…), but the other stuff on unity functions will be helpful.

var distance = 5.0;
var target : Transform;
private var open : boolean = false;
private var startIncrement = 0.0;
private var hold : boolean = false;

function Start()
{
/if (target == null GameObject.FindWithTag(“Player”))
target = GameObject.FindWithTag(“Player”).transform;
/
// transform.Rotate(0, 90, 0);
}

function Hit()
{
hold = true;
}

function Update () {

if (Input.GetButtonDown (“Action”) Vector3.Distance(transform.position, target.position) < distance target != null)
{
open = !open;
}

if (!hold)
{
if (open == true startIncrement <= 90)
{
transform.Rotate(0, 1, 0);
++startIncrement;
}

if (open == false startIncrement >= 0)
{
transform.Rotate(0, -1, 0);
–startIncrement;
}
}

}

function OnGUI()
{
if (Vector3.Distance(transform.position, target.position) < distance)
{
if (open)
{
GUI.Label (Rect (10, 10, 300, 20), “Press Action to close door.”);
}
if (!open)
{
GUI.Label (Rect (10, 10, 300, 20), “Press Action to open door.”);
}
}
}

/*function OnCollisionEnter (collision : Collision)
{
hold = true;
GUI.Label (Rect (10, 30, 300, 20), “Collision Occured.”);
}

function OnCollisionExit (collision : Collision)
{
hold = false;
}*/

Thanks so much, it works fine. At first I got difficulty for not setting up the keyboard input.