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);
}
}
}