Help: My doors won't work properly ><
void Start () {
anim = GetComponent<Animation> ();
anim ["GenDoorOpen"].speed = -1.0f;
open = true;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.E)) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, distance)) {
if (!anim.isPlaying) {
open = !open;
anim ["GenDoorOpen"].speed = anim ["GenDoorOpen"].speed * -1.0f;
if (open) {
anim ["GenDoorOpen"].time = anim ["GenDoorOpen"].length;
} else {
anim ["GenDoorOpen"].time = 0.0f;
}
} else {
open = !open;
anim ["GenDoorOpen"].speed = anim ["GenDoorOpen"].speed * -1.0f;
}
anim.Play ("GenDoorOpen");
}
}
}
}
But it would trigger all doors in the scene, not just the one I was looking at. I tried using hit.transform.gameObject as such:
void Start () {
anim = GetComponent<Animation> ();
anim ["GenDoorOpen"].speed = -1.0f;
open = true;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.E)) {
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, distance)) {
if (hit.transform.gameObject.tag == "door") {
currentDoor = hit.collider.gameObject;
anim = currentDoor.GetComponent<Animation> ();
if (!anim.isPlaying) {
open = !open;
anim ["GenDoorOpen"].speed = anim ["GenDoorOpen"].speed * -1.0f;
if (open) {
anim ["GenDoorOpen"].time = anim ["GenDoorOpen"].length;
} else {
anim ["GenDoorOpen"].time = 0.0f;
}
} else {
open = !open;
anim ["GenDoorOpen"].speed = anim ["GenDoorOpen"].speed * -1.0f;
}
anim.Play ("GenDoorOpen");
}
}
}
}
}
But now the animation won't play at all....
Any solutions?
And you've made sure that the doors all have colliders and the "door" tag, yes? The second script is something you should attach to a single object, like the main camera, rather than every door, or else it will be executing that script for however many doors you have which could also be interfering with the process.
Yep, they're tagged "door" and have colliders.
How can I attach it the the main camera and get it to tell the object it's looking at to play in the fashion I had it?
You would do it in the same way, just without the Start function stuff. You're casting a ray from the main camera in the first place, which tells you the door you are looking at and gives you that door's animation component, and as such don't need it attached to every door.