- Home /
Debuging Collision detection
I am trying to set up a collision detection so that when the player moves near a door it will open, I have the below set up:
My door set up:
My player set up:
And then the OpenDoors.cs script:
using UnityEngine;
using System.Collections;
public class OpenDoors : MonoBehaviour {
// To open and close doors via Inspector
public bool Open_Doors;
public bool Close_Doors;
// Once the doors has been opened they can be closed again
private bool door_opened;
public GameObject BottomDoors;
public GameObject TopDoors;
// Store original position of top and bottom doors
Vector3 TopDoorsOriginPosition;
Vector3 BottomDoorsOriginPosition;
void Awake()
{
TopDoorsOriginPosition = TopDoors.transform.position;
BottomDoorsOriginPosition = BottomDoors.transform.position;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Open_Doors)
{
OpenSciFiDoors();
}
if(door_opened && Close_Doors)
{
CloseSciFiDoors();
}
}
void OnCollisionEnter(Collision collision)
{
Open_Doors = true;
}
void OpenSciFiDoors()
{
TopDoors.transform.position = Vector3.Lerp (TopDoors.transform.position, new Vector3 (2f, 0,TopDoors.transform.position.z ), 0.45f * Time.deltaTime);
BottomDoors.transform.position = Vector3.Lerp (BottomDoors.transform.position, new Vector3 (2f, 0, BottomDoors.transform.position.z), 0.45f * Time.deltaTime);
// We want to wait till doors reach certain position before they can be closed
if(TopDoors.transform.position.y > 3 && BottomDoors.transform.position.y < -2.5f)
{
door_opened = true;
Open_Doors = false;
}
}
void CloseSciFiDoors()
{
TopDoors.transform.position = Vector3.Lerp (TopDoors.transform.position, TopDoorsOriginPosition, 0.45f * Time.deltaTime);
BottomDoors.transform.position = Vector3.Lerp (BottomDoors.transform.position, BottomDoorsOriginPosition, 0.45f * Time.deltaTime);
}
}
however with the current set up nothing happens when the player moves into the collision box, I know the door script does work if I set Open_Door to true at the start
Doh, the current set updoes not work when the player moves into the collision box nothing happens
Answer by robertbu · Mar 07, 2014 at 04:15 PM
The reason your door don't open is that you have your colliders setup as triggers. To capture trigger events you use OnTriggerEnter() not OnCollisionEnter(). As you change the function, note that OnTriggerEnter() has a collider for a parameter, not a collision.
BTW: Including all the screen shots made it much easier to sport the problem. Normally it would have taken some back and forth.
Should only the door Collider be a trigger, and the player Collider just a BoxCollider (isTrigger = false)?