- Home /
Script for a door that opens and closes help!
I am trying to make a door open and close when clicked and I have the following scripts but it doesn't work. Could someone help me out please?? Thanks!
using UnityEngine; using System.Collections;
public class WardrobeDoor: MonoBehaviour {
public bool Open = false;
public float angle = 90f;
public float speed = 500f;
private float zero;
void awake (){
zero = transform.localEulerAngles.y;
}
void update (){
if (Open && (transform.localEulerAngles.y < (zero + angle))) {
transform.Rotate (-transform.up * Time.deltaTime * speed);
}
}
}
and.....
7using UnityEngine; using System.Collections;
public class Ray_test : MonoBehaviour {
RaycastHit rayhit;
float timer = 1f;
private WardrobeDoor d;
void update (){
timer -= Time.deltaTime;
Vector3 fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast(transform.position,fwd, out rayhit)){
Debug.DrawRay(transform.position, transform.forward*10f, Color.red);
d = rayhit.transform.root.GetComponent<WardrobeDoor>();
Debug.Log(d);
if(Input.GetMouseButtonDown (1) &&(timer < 0f)){
if(d.Open){
Debug.Log("User closed door");
}
d.Open = !(d.Open);
timer = 1f;
}
print ("The Ray Hit: " + rayhit.transform.gameObject.name);
}
else{
Debug.DrawRay(transform.position, transform.forward*10f, Color.green);
print ("There is nothing in front of the object");
}
}
}
Comment