Question by
Creationary · Jul 12, 2016 at 03:29 PM ·
collisioncollidercollision detectiondoor
Collision doesnt work
im using a script i found on the internet for opening and clousing doors. The collision works when the door doesnt move but as soon as the door moves the player character can move straight through it just as if it didnt have a rigidbody. I have no idea why this happens since the door itself doesnt have any scripts. the script that moves the door is attached to a "pivot" or "hinge" object that is unrendered, doesnt have a rigidbody and is a parent to the door object.
the first sample is of the open/close script attached to the hinge object and the second is from the player object which dictates if the door should or shouldnt be opened. using UnityEngine; using System.Collections;
public class Door : MonoBehaviour {
public bool open = false;
public float doorOpenAngle = 90f;
public float doorCloseAngle = 0f;
public float smooth = 2f;
void Start ()
{
}
public void ChangeDoorState ()
{
open = !open;
}
void Update ()
{
if (open)
{
Quaternion targetRotation = Quaternion.Euler (0, doorOpenAngle, 0);
transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotation, smooth * Time.deltaTime);
}
else
{
Quaternion targetRotation2 = Quaternion.Euler (0, doorCloseAngle, 0);
transform.localRotation = Quaternion.Slerp (transform.localRotation, targetRotation2, smooth * Time.deltaTime);
}
}
}
using UnityEngine; using System.Collections;
public class Interact : MonoBehaviour {
public float interactDistance = 5f;
void Update ()
{
if (Input.GetKeyDown (KeyCode.E))
{
Ray ray = new Ray (transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactDistance))
{
if (hit.collider.CompareTag ("Door"))
{
hit.collider.transform.parent.GetComponent<Door> ().ChangeDoorState ();
}
}
}
}
}
Comment