- Home /
Question by
programer717 · Nov 12, 2018 at 03:04 PM ·
c#animationraycast
Animating an automatic door
Hello, I am trying to make a door that opens on click. Right now I have 4 animations, two idles (one open and one closed), and two others (one opening and one closing). I have made a couple of scripts and now every time I "click" the button it moves on to the next animation, but it is supposed to play the next animation and then move onto the next animation (Either closed or opened).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Animatorirator : MonoBehaviour {
private Animator Anim;
public float Animate;
// Use this for initialization
void Start () {
Anim = GetComponent<Animator>();
Animate = 0;
}
// Update is called once per frame
void Update () {
if (Animate > 3)
{
Animate = 0;
}
if (Animate == 0) {
Anim.SetBool("closed", true);
Anim.SetBool("opening", false);
Anim.SetBool("finished", false);
Anim.SetBool("closing", false);
}
if (Animate == 1)
{
Anim.SetBool("closed", false);
Anim.SetBool("opening", true);
Anim.SetBool("finished", false);
Anim.SetBool("closing", false);
MyMethod();
}
if (Animate == 2)
{
Anim.SetBool("closed", false);
Anim.SetBool("opening", false);
Anim.SetBool("finished", true);
Anim.SetBool("closing", false);
}
if (Animate == 3)
{
Anim.SetBool("closed", false);
Anim.SetBool("opening", false);
Anim.SetBool("finished", false);
Anim.SetBool("closing", true);
MyMethod();
}
}
IEnumerator MyMethod()
{
Debug.Log("Before");
yield return new WaitForSeconds(1f);
Debug.Log("after");
Animate = Animate + 1;
}
}
This is my script to change animations.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class code : MonoBehaviour {
Animatorirator script;
public LayerMask detectionLayer;
float TheDistance;
void Start()
{
script = GameObject.FindWithTag("Bars").GetComponent<Animatorirator>();
Vector3 forward = transform.TransformDirection(Vector3.forward) * 3f;
}
// Update is called once per frame
void Update () {
RaycastHit hit;
if (Input.GetKeyDown(KeyCode.K))
{
Vector3 forward = transform.TransformDirection(Vector3.forward) * 3f;
Debug.DrawRay(transform.position, forward, Color.green);
if (Physics.Raycast(transform.position, (forward), out hit))
{
TheDistance = hit.distance;
print(TheDistance + " " + hit.collider.gameObject.layer);
script.Animate = script.Animate + 1;
if (hit.collider.gameObject.layer == detectionLayer)
{
script.Animate = script.Animate+1;
}
}
}
}
}
And this one makes it so when I shoot a raycast it flips to the next animation. Any help will be appreciated, thanks!
Comment
Your answer