敵が、プレイヤーに踏まれた場合はやられアニメーションが再生されて消えるのですが、プレイヤーが発射した弾に当たっても、やられアニメーションの最後の画像のまま、移動し続けて消えません。
追記(5/16)
似た問題を抱えている方などのために、解決後のスクリプトを載せておきます。
字数制限のため、質問投稿時のスクリプトは消しましたが、それに書き加えただけなので大丈夫です。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Junkai_Zako7 : MonoBehaviour { #region//インスペクターで設定する [Header("重力")] public float gravity; [Header("移動経路")] public GameObject[] movePoint; [Header("速さ")] public float speed = 1.0f; [Header("やられた時に鳴らすSE")] public AudioClip deadSE; #endregion #region//プライベート変数 private Rigidbody2D rb = null; private SpriteRenderer sr = null; private Animator anim = null; private ObjectCollision oc = null; private BoxCollider2D col = null; private int nowPoint = 0; private bool returnPoint = false; private Vector2 oldPos = Vector2.zero; private Vector2 myVelocity = Vector2.zero; private bool isDead = false; private float deadTimer = 0.0f; #endregion private bool flag = false; //New private void Start() { rb = GetComponent<Rigidbody2D>(); if (movePoint != null && movePoint.Length > 0 && rb != null) { rb.position = movePoint[0].transform.position; oldPos = rb.position; } sr = GetComponent<SpriteRenderer>(); anim = GetComponent<Animator>(); oc = GetComponent<ObjectCollision>(); col = GetComponent<BoxCollider2D>(); } public Vector2 GetVelocity() { return myVelocity; } private void FixedUpdate() { if (flag == true) { return; } //New if (!oc.playerStepOn) { if (movePoint != null && movePoint.Length > 1 && rb != null) { //通常進行 if (!returnPoint) { int nextPoint = nowPoint + 1; //目標ポイントとの誤差がわずかになるまで移動 if (Vector2.Distance(transform.position, movePoint[nextPoint].transform.position) > 0.1f) { //現在地から次のポイントへのベクトルを作成 Vector2 toVector = Vector2.MoveTowards(transform.position, movePoint[nextPoint].transform.position, speed * Time.deltaTime); //次のポイントへ移動 rb.MovePosition(toVector); } //次のポイントを1つ進める else { rb.MovePosition(movePoint[nextPoint].transform.position); ++nowPoint; //現在地が配列の最後だった場合 if (nowPoint + 1 >= movePoint.Length) { returnPoint = true; } } } //折返し進行 else { int nextPoint = nowPoint - 1; //目標ポイントとの誤差がわずかになるまで移動 if (Vector2.Distance(transform.position, movePoint[nextPoint].transform.position) > 0.1f) { //現在地から次のポイントへのベクトルを作成 Vector2 toVector = Vector2.MoveTowards(transform.position, movePoint[nextPoint].transform.position, speed * Time.deltaTime); //次のポイントへ移動 rb.MovePosition(toVector); } //次のポイントを1つ戻す else { rb.MovePosition(movePoint[nextPoint].transform.position); --nowPoint; //現在地が配列の最初だった場合 if (nowPoint <= 0) { returnPoint = false; } } } myVelocity = (rb.position - oldPos) / Time.deltaTime; oldPos = rb.position; } } else { if (!isDead) { anim.Play("zako-7_dead"); rb.velocity = new Vector2(0, -gravity); isDead = true; col.enabled = false; if (GManager.instance != null) { GManager.instance.PlaySE(deadSE); } } else { if (deadTimer > 1.0f) { Destroy(this.gameObject); } else { deadTimer += Time.deltaTime; } } } } // 弾オブジェクトと接触したときに呼び出される関数 void OnCollisionEnter2D(Collision2D collision) { // もしもtagがbulletであるオブジェクトと接触したら if (collision.gameObject.tag == "Bullet") { flag = true; //New if (!isDead) { anim.Play("zako-7_dead"); rb.velocity = new Vector2(0, -gravity); isDead = true; col.enabled = false; if (GManager.instance != null) { GManager.instance.PlaySE(deadSE); } Destroy(gameObject, 1.0f);//1秒後に削除 } } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/13 04:44
退会済みユーザー
2020/05/13 06:04
2020/05/16 07:38