First create a little set, where you can find a wall with a door in it.
D’abord il faut créer un petit décor avec un mur percé d’une porte.
The door is made from a cube. So the pivot is right in the middle of the cube. So, the door should rotate around its pivot, jute like if it was spinning.
La porte est faite à partir d’un cube. Or, le pivot est juste au milieu du cube. La porte va donc tourner autour de son axe médian.
Create an Empty Object and place it where the hinges are. Parent this empty with the door.
Créer un Empty Object et le placer à la place des gonds. Puis parenter l’empty avec la porte.
Create un collider with a cube.
Créer un collider avec un cube.
Remove the Mesh Renderer tick and declare it as trigger
Enlever la coche Mesh Renderer et cocher « is trigger ».
Apply the following stript on the Hinge
Appliquer le script suivant sur l’axe (le Empty Object qui sert d’axe).
Script comes from : Le script vient de :
http://answers.unity3d.com/questions/294194/how-to-make-a-solid-door-open-and-close.html (thanks !!)
/*
Instruction:
create a cube to use as hinge put where is needed, the withe cube
press f to meet it position
create a cube to use as door end re-size, the brown cube
centre the hinge at the door__________________________pic door1
parent the hinge at the door
in the inspector, centre the collider at the door re size as your need
and enable trigger___________________________________pic door2
Assign this script at the hinge
Press "f" to open and close the door
if whant change in, if(Input.GetKeyDown("f")at line 46
Make sure the main character is tagged "player"
when all work can remove Mash render and Mash Filter
*/
// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;
//Main function
function Update ( ){
if(open == true){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
Time.deltaTime * smooth);
}
if(open == false){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
Time.deltaTime * smooth);
}
if(enter == true){
if(Input.GetKeyDown("f")){
open = !open;
}
}
}
//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;
}
} |
Test : when pressing f key the door opens or closes.
Tester : en appuyant sur la touche f, la porte s’ouvre ou se ferme.
Pourquoi quand on et pas dans le « Collider » on peux quand même ouvrir la porte ?
et pourquoi toute les porte s’ouvre au lieu de une quand j’appuie sur le bouton ?