unity SetTriggerで移行したアニメーションから前の状態に戻らないのですがどうすればいいでしょうか?
public float speed = 1.0f; public float attackRadius = 5.0f; //private float dashTime; private bool isRun = false; private bool isGround = false; private bool isCombat = false; private bool isJump = false; [HideInInspector]public bool isAttack = false; //private string enemyTag = "Enemy"; public LayerMask enemyLayer; public GroundCheck ground; public Transform AttackPoint; private Rigidbody2D rb; private Animator anim; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); anim = GetComponent<Animator>(); } // Update is called once per frame void FixedUpdate() { SetAnimation(); isGround = ground.IsGround(); //float xSpeed = GetXSpeed(); float x = Input.GetAxis("Horizontal"); if (x < 0) { transform.localScale = new Vector3(1, 1, 1); } if (x > 0) { transform.localScale = new Vector3(-1, 1, 1); } anim.SetFloat("Speed", Mathf.Abs(x)); rb.velocity = new Vector2(x, rb.velocity.y); if (Input.GetMouseButton(2) && !isCombat) { isAttack = false; isCombat = true; isRun = false; SetAnimation(); anim.SetFloat("Speed", Mathf.Abs(x)); } if (Input.GetMouseButton(0) && isCombat && !isAttack) { isAttack = true; Attack(); } else if (Input.GetMouseButton(1) && isCombat) { isCombat = false; SetAnimation(); } if (Input.GetKey(KeyCode.Space) && !isJump) { isJump = true; isGround = false; isRun = false; SetAnimation(); } } void Attack() { Collider2D[] hitEnemys = Physics2D.OverlapCircleAll(AttackPoint.position, attackRadius, enemyLayer); anim.SetTrigger("Attack"); foreach (Collider2D hitEnemy in hitEnemys) { Debug.Log(hitEnemy.gameObject.name + "に攻撃した"); hitEnemy.GetComponent<EnemyBandit>().Damage(); } } /// <summary> /// アニメーションを設定する /// </summary> private void SetAnimation() { anim.SetBool("jump", isJump); anim.SetBool("Ground", isGround); anim.SetBool("Run", isRun); anim.SetBool("Combat", isCombat); } private void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(AttackPoint.position, attackRadius); }
攻撃したあとの最後のアニメーションから動かなくなってしまいますループはしていません
Animator ControllerにはAttackのTriggerだけを移行するときもIdleに戻るときも使用しています
if (Input.GetMouseButton(0) && isCombat && !isAttack) { isAttack = true; Attack(); } else if (Input.GetMouseButton(1) && isCombat) { isCombat = false; SetAnimation(); } if (Input.GetKey(KeyCode.Space) && !isJump) { isJump = true; isGround = false; isRun = false; SetAnimation(); } void Attack() { Collider2D[] hitEnemys = Physics2D.OverlapCircleAll(AttackPoint.position, attackRadius, enemyLayer); anim.SetTrigger("Attack"); /// <summary> /// アニメーションを設定する /// </summary> private void SetAnimation() { anim.SetBool("jump", isJump); anim.SetBool("Ground", isGround); anim.SetBool("Run", isRun); anim.SetBool("Combat", isCombat); }
これが関連したコードです
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/09/28 10:49