How do I transition among multiple animations?
Hi, everyone,
I have been able to create a character movement script that allows me to trigger some individual animations based on inputs, such as jumping, idle and shooting arrows. I was wondering if there was a way to allow a single input, such as pressing the shoot button, to trigger multiple animations so that my character may shoot his bow, then transition to an "alert" state before going back to idle. Basically I want:
attack animation plays --> alert animation plays --> goes back to idle animation after a few seconds (alert animation would loop a few times) if no other attacks have been performed, but the alert stance can go back to the attack animation if the player decides to attack, starting the cycle all over again
Thank you for any help you can give me.
Answer by theANMATOR2b · Jul 04, 2017 at 12:07 AM
Mecanim is kind of created exactly for this type of set up and others. I'd strongly suggest getting into it and learning how to control the transitions with code. However if you'd still like to keep using your script - you'll have to show/explain how it is currently set up so others can provide input on how to proceed.
Answer by Dalsia · Jul 04, 2017 at 03:25 AM
Thank you for responding. I'm having trouble figuring out how to do things like allowing my "alert" animation to keep being transitioned to if my player has attacked in the past 5 seconds. I have been able to make my character shoot his bow then play the alert animation, but if I walk during this animation and stand still I go back to idle when I'd like to go back to alert for 5 seconds after the character shoots his bow. I'm also having trouble with allowing my player to shoot his bow again once he is in the alert animation.
Here is my current script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player_1_controller : MonoBehaviour {
//movement variables
public float maxSpeed;
//jumping variables
bool grounded = false;
float groundCheckRadius = 0.2f;
public LayerMask groundLayer;
public Transform groundCheck;
public float jumpHeight;
Rigidbody2D myRB;
Animator myAnim;
bool facingRight;
//arrow shooting variables
bool firing = true;
public float arrowShoot;
public Transform arrowTip;
public GameObject arrow;
public float fireRate = 0.5f;
public float nextFire = 0f;
//alert variables
// Use this for initialization
void Start () {
myRB = GetComponent<Rigidbody2D> ();
myAnim = GetComponent<Animator> ();
facingRight = true;
}
// Update is called once per frame
void Update () {
if (grounded && Input.GetButtonDown ("Jump")) {
grounded = false;
myAnim.SetBool ("isGrounded", grounded);
myRB.AddForce (new Vector2 (0, jumpHeight));
}
//player shooting
if (grounded && Input.GetButtonDown ("Fire1")) {
myAnim.SetBool ("arrowShoot", firing);
Invoke ("fireArrow", 1);
}
}
void FixedUpdate() {
//check if we are grounded - if not, then we are falling
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
myAnim.SetBool ("isGrounded", grounded);
myAnim.SetFloat ("verticalSpeed", myRB.velocity.y);
float move = Input.GetAxis ("Horizontal");
myAnim.SetFloat ("speed", Mathf.Abs (move));
myRB.velocity = new Vector2 (move * maxSpeed, myRB.velocity.y);
if (move > 0 && !facingRight) {
flip ();
} else if (move < 0 && facingRight) {
flip ();
}
}
void flip () {
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
void fireArrow() {
if (Time.time > nextFire) {
nextFire = Time.time + fireRate;
if (facingRight) {
Instantiate (arrow, arrowTip.position, Quaternion.Euler (new Vector3 (0, 0, 0)));
} else if (!facingRight) {
Instantiate (arrow, arrowTip.position, Quaternion.Euler (new Vector3 (0, 0, 180)));
}
}
playerAlert ();
}
void playerAlert () {
firing = false;
myAnim.SetBool ("arrowShoot", firing);
}
}