- Home /
Open door on input key
I'm trying to let the character be able to open a door on mousing over it and pressing e. I have tried a script which rotates and moves the door which successfully opens it but on clicking e a second time the door moves forward from the doorway.
var door : GameObject;
private var x : float;
x = 2.6;
private var y : float;
y = 1.4;
private var rot : int;
rot = 240;
private var mouse : boolean = false;
function OnMouseOver ()
{
mouse = true;
}
function OnMouseExit ()
{
mouse = false;
}
function Update () {
if(Input.GetKeyDown("e") && mouse == true)
{
door.transform.Translate(x,y,0);
door.transform.Rotate(0,0,rot);
rot = -1 * rot;
}
}
I have also tried using two door and open and a closed one and simply translating them. The open door moves into place on clicking e and the closed door moves out of sight but the process is not reversing properly
var y : float;
y = 5;
private var mouse: boolean;
mouse = false;
var initial : GameObject;
var attached : GameObject;
function OnMouseEnter
{
mouse = true;
}
function OnMouseExit
{
mouse = false;
}
function Update
{
if (mouse = true && input.GetKeyDown (āeā))
{
initial.Tranform.translate (0,y,0);
attached.Transform.translate (0,y,0);
y = y * -1;
thanks for the help
Answer by LightSource · Mar 12, 2013 at 08:42 PM
Try this: Just attach the animation for the door opening...
var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray.origin, ray.direction, hit, 100))
{
if(hit.collider.tag == "yourObject") && (Input.GetKeyDown ("e"))
{
animation.Play("yourAnimation");
}
hey thanks for the advice... but im new to unity what should i use to make an animation.
Can one be made with in unity or will i need to use a program like blender.
Take a look at this video for beginners in animation. I also just explained a step by step process in the second link.
Video: http://www.youtube.com/watch?v=HrTlFSztcOw
Second Link: http://answers.unity3d.com/questions/414940/can-i-scale-an-animation-with-a-model.html
Thanks I have been able to get the door to open with an animation but can I make the door close if the player hits e on an open door
I tried this code to no avail...
var attached : GameObject;
private var state : int = 1;
private var mouse : boolean = false;
function On$$anonymous$$ouseEnter ()
{
mouse = true;
}
function On$$anonymous$$ouseExit ()
{
mouse = false;
}
function Update ()
{
if (Input.Get$$anonymous$$eyDown("e") && state == 1 && mouse == true)
{
animation.Play("Open");
state = state * -1;
}
if (Input.Get$$anonymous$$eyDown("e") && state == -1 && mouse == true)
{
animation.Play("Shut");
state = state * -1;
}
}
You need to detect if the door is in front of you. Use that raycast component in my previous script
Your answer
![](https://koobas.hobune.stream/wayback/20220613094308im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help with door opening and closing script/animation 3 Answers
Targetting error please help 0 Answers