- Home /
door open/close on mouse click
hi i know there are many variations of this question but none of the answers work for me i want to open and close a door when you click on the door handle unfortunately while it opens i struggle to trigger the closed. heres my code hopefully just needs tweaking. ive had it working using keyboard functions but cant seem to use it for mouse click. many thanks
var door : GameObject;
var knob : GameObject;
var smooth : float = 1.0;
var open : boolean = false;
var closed : boolean = true;
function Start (){
door = GameObject.Find("door");
knob = GameObject.Find("Sphere");
}
function Update () {
//to rotate the door to the open angle//
if (open){
var doorOpen = Quaternion.Euler(0, -80, 0);
door.transform.rotation = Quaternion.Slerp(transform.rotation, doorOpen, Time.deltaTime * smooth);
}
//to rotate the door to the closed angle//
if (closed){
var doorClosed = Quaternion.Euler(0, 0, 0);
door.transform.rotation = Quaternion.Slerp(transform.rotation, doorClosed, Time.deltaTime * smooth);
}
if (Input.GetMouseButtonDown(0)){
var ray : Ray = Camera.main.ScreenPointToRay
(Input.mousePosition);
var hit : RaycastHit;
//raytrace on door knob//
if (knob.collider.Raycast (ray, hit, 10.0)&&closed ==true) {
open = true;
}
if (knob.collider.Raycast (ray, hit, 10.0)&&open ==true) {
closed = true;
}
//this should make sure that open and closed are opposites but doesnt seem to//
if (open==true){
closed = (open == false);
}
}
}
Are you sure, that when the door opens, it's not too far away for the distance of the ray, to reach it?
First i think you shoud remove the last piece of code if (open == true) {....}
that might be breaking you'r functionality. Second, you can just use one variable (boolean) ins$$anonymous$$d of 2. Just use Open and put it to true or false, depending if the door is open or closed. This way if you want to make the opposite you can just write
open = !open;
But you actually won't need that, if you use one variable...
Third: I think the Raycast condition have to be If - Else, ins$$anonymous$$d of If - If. Because when you open the door it immediately starts the other if and this won't happen if you use if-else condition. The one will skip the other...
I'll take a look at the actual code in Unity to see how it can be optimised..
Answer by instruct9r · Sep 17, 2013 at 12:35 PM
#pragma strict
var door : GameObject;
var knob : GameObject;
var smooth : float = 1.0;
var open : boolean = false;
function Start ()
{
door = GameObject.Find("door");
knob = GameObject.Find("Sphere");
}
function Update () {
//to rotate the door to the open angle//
if (open) // open = true
{
var doorOpen = Quaternion.Euler(0, 80, 0); // Get the Euler of the angle that we want to reach
var angle = Quaternion.Angle(transform.rotation, doorOpen); // Convert it to angle
door.transform.rotation = Quaternion.Slerp(transform.rotation, doorOpen, smooth * Time.deltaTime);
// When we come near the angle, that we want to reach, we snap the door to that angle
// cuz otherwise we will never reach it, because of the smoothed lerp of the Quaternion.
// If the snap is too obvious try replacing the "smooth" with 0.5 for example.
if (angle <= smooth)
door.transform.rotation = doorOpen;
}
//to rotate the door to the closed angle//
else // open = false
{
doorOpen = Quaternion.Euler(0, 0, 0);
angle = Quaternion.Angle(transform.rotation, doorOpen);
door.transform.rotation = Quaternion.Slerp(transform.rotation, doorOpen, smooth * Time.deltaTime);
if (angle <= smooth)
door.transform.rotation = doorOpen;
}
if (Input.GetMouseButtonDown(0))
{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
//raytrace on door knob//
if (knob.collider.Raycast (ray, hit, 10.0) && open)
{
open = false;
}
else
{
open = true;
}
}
}
just found a small problem. the knob collider is only effective for true or false not both. so when else = true a click anywhere on the screen opens the door and a click on the knob closes it. i dont suppose theres a workaround where the collider is the effector for both ways?
ok this is because the script is checking if the ray hit's the collider of the knob only to make the open false, otherwise it just executes it. $$anonymous$$y bad :)
Here's fixed version of the Input.Get$$anonymous$$ousebutton... code..
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
//raytrace on door knob//
if (knob.collider.Raycast (ray, hit, 10.0))
{
open = !open;
}
}
}
open = !open - means that everytime you press the knob, open will become the opposite of open. If it's true = !true (false) and vice versa :)
cheers
Your answer
Follow this Question
Related Questions
Door Sound Script Help 1 Answer
How to trigger a object on Mouse Enter and Down? 2 Answers
Game Script for Mouse Click Scoring. 1 Answer
Trying to move a instantiate gameobject with mousedown 1 Answer
Causing animaton on mouseclick? 1 Answer