Hi, I’m a beginner in Unity and i met with a problem.
So I have an object - pigeon. It has two animations - ‘fly’ and ‘turn’. ‘fly’ is a default animation that should play on loop, while ‘turn’ is an animation that should play when, well, player moves in the opposite direction(its basically the animation of pigeon performing the U-turn).
Now, the problem is my lack of C# knowledge and extreme tiredness. I need my bird to turn in the opposite direction when ‘turn’ animation ends, but i dont know how. Right now it turns immediatly and at the end of ‘turn’ animation it turns another time. English isn’t my main language so please excuse me for whatever mistakes i made, thanks.
here’s image if that helps
here’s a code:
using UnityEngine;
using System.Collections;
public class moving : MonoBehaviour {
public float movementSpeed = 10;
private bool canTurn = true;
private bool canMove = true;
//something i took from tutorial
Animator myAnim;
int turnHash = Animator.StringToHash("turn");
void Start ()
{
myAnim = GetComponent<Animator>();
}
void Update()
{
//check if 'turn' animation is playing. If it does, pigeon cant move
if (myAnim.GetCurrentAnimatorStateInfo (0).IsName ("turning")) {
canMove = false;
} else {
canMove = true;
}
//check if... uhhh... I think it checks if 'turn' animation is finished playing
if (myAnim.GetCurrentAnimatorStateInfo (0).normalizedTime > 1 && !myAnim.IsInTransition (0) && myAnim.GetCurrentAnimatorStateInfo (0).IsName ("turning")) {
canTurn = false;
} else {
canTurn = true;
}
if (canMove == true) {
if (Input.GetKey ("right") && canMove == true && !Input.GetKey ("left")) {
//if pigeon is facing the left and he can turn, play 'turn' animation
if (transform.eulerAngles.y == 0 && canTurn == true) {
myAnim.SetTrigger (turnHash);
}
//if it can move, it moves(and rotates)
//if (myAnim.GetCurrentAnimatorStateInfo (0).normalizedTime > 1 && !myAnim.IsInTransition (0)) {
//}
transform.Translate (Vector3.back * movementSpeed * Time.deltaTime);
transform.eulerAngles = new Vector3 (0, 180, 0);
}
//pretty much the same, but for left key press
else if (Input.GetKey ("left") && canMove == true && !Input.GetKey ("right")) {
if (transform.eulerAngles.y == 180 && canTurn == true) {
myAnim.SetTrigger (turnHash);
}
//if (canTurn == false) {
//}
transform.eulerAngles = new Vector3 (0, 0, 0);
transform.Translate (Vector3.back * movementSpeed * Time.deltaTime);
}
}
}
}