- Home /
Player and door collision issue
I have a script from youtube that would simply open and close the door using the 'F' key.
// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
private var open : boolean;
private var enter : boolean;
private var defaultRot : Vector3;
private var openRot : Vector3;
function Start(){
defaultRot = transform.eulerAngles;
openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}
//Main function
function Update (){
if(open){
//Open door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
}else{
//Close door
transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
}
if(Input.GetKeyDown("f") && enter){
open = !open;
}
}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 30), "Press 'F'");
}
}
//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}
the problem is that everytime I open/close the door, it passes through the character but when I assign a rigid body to the door, everything works just fine except for the door getting removed from the pivot. :(
I even tried to add a hinge joint component but it just keeps jittering everytime I press the 'F' key. Any idea ?
Answer by STARS · Aug 27, 2013 at 12:10 AM
I figured it out. Hinge joint is very sensetive to colliders. it was overlapping with ground. unfortunally there's still a little problem and it's the joint shaking a bit whenever it collides with the character.
Answer by TheKusabi · Aug 25, 2013 at 02:39 PM
The door is moving through the player because it is being rotated without the use of physics functions.
With the rigid-body attached, try setting it's position restraints in the inspector. If you freeze it's x,y and z position, it should stop it from moving off the hinges, however, I do not know if it will push the player out of the way, or if the player will block it from opening.
Good luck.
Freezing the position axis didn't make any difference but thanks anyway :)