Question by
Zirkekelbachlavich · Sep 05, 2018 at 12:14 AM ·
c#animationjump
How to play an animation once and then transition to the same animation
So, I need my character to be able to perform a front flip once when he double jumps but I have found this to be a very difficult task for how simplistic it sounds. My initial jump is divided into 3 stages, jump (animation for when he leaves the ground), Apex (animation when his y velocity is very small and he is at the full height of his jump) and falling (pretty self-explanatory). I want him to be able to transition to the front flip animation from any one of those 3 stages and then transition back to apex. This is my code.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CharacterController : MonoBehaviour {
public float moveSpeed;
public float maxSpeed;
public float accelerate;
public float drag;
public Rigidbody2D MRB;
public float jumpSpeed;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
public bool isGrounded;
public bool secondJump;
public Animator anim;
public bool interruption;
// Use this for initialization
void Start () {
MRB = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
//Basic Movement
if (Input.GetAxisRaw("Horizontal") == 1) {
moveSpeed = Mathf.MoveTowards(moveSpeed, maxSpeed, accelerate * Time.deltaTime);
} else if (Input.GetAxisRaw("Horizontal") == -1f){
moveSpeed = Mathf.MoveTowards(moveSpeed, -maxSpeed, accelerate * Time.deltaTime);
} else {
moveSpeed = Mathf.MoveTowards(moveSpeed, 0f, drag * Time.deltaTime);
}
MRB.velocity = new Vector2(moveSpeed, MRB.velocity.y);
//Face Direction
if (moveSpeed < -0.1f){
transform.localScale = new Vector3(-1f, transform.localScale.y, transform.localScale.z);
}
if (moveSpeed > 0.1f){
transform.localScale = new Vector3(1, transform.localScale.y, transform.localScale.z);
}
//Jump
doJumpAnim = false;
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if(isGrounded){
secondJump = true;
}
if (Input.GetKeyDown(KeyCode.Space) && (isGrounded || secondJump)) {
MRB.velocity = new Vector2(MRB.velocity.x, jumpSpeed);
if(!isGrounded) {
secondJump = false;
}
}
//Animation Parameters
anim.SetFloat("Speed", MRB.velocity.x);
anim.SetFloat("Upward Velocity", MRB.velocity.y);
anim.SetBool("isGrounded", isGrounded);
anim.SetBool("Double Jump", doJumpAnim);
if (MRB.velocity.x != 0f) {
anim.SetBool("isMoving", true);
} else {
anim.SetBool("isMoving", false);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612174055im_/https://answers.unity.com/themes/thub/images/avi.jpg)