以下の写真のように、Z軸方向に走らせる3Dキャラクターがゴールに到達した時点で、CLEARのテキストがゲームビューにも、シーンビューにも表示されません。
以下の写真のようにGoalTagは作成して、Goalオブジェクトにタグ付けしています。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UnityChanController_2 : MonoBehaviour { //アニメーションするためのコンポーネントを入れる private Animator myAnimator; //Unityちゃんを移動させるコンポーネントを入れる private Rigidbody myRigidbody; private float forwardForce = 800.0f; private float turnForce = 500.0f; private float movableRange = 3.4f; private float jumpForce = 500.0f; private float coefficient = 0.95f; private bool isEnd = false; private GameObject stateText; // Start is called before the first frame update void Start() { //Animatorコンポーネントの取得 this.myAnimator = GetComponent< Animator > (); //走るアニメーションを開始 this.myAnimator.SetFloat("Speed", 1); //Rigitbobyコンポーネントの取得 this.myRigidbody = GetComponent<Rigidbody>(); this.stateText = GameObject.Find("GameResultText_2"); } // Update is called once per frame void Update() { if (this.isEnd) { //Unityちゃんにかかっている力を落としている this.forwardForce *= this.coefficient; this.turnForce *= this.coefficient; this.jumpForce *= this.coefficient; this.myAnimator.speed *= this.coefficient; } //Unityちゃんに前方向の力を加える //AddForce関数の引数(力の方向×力) //transform.forward=ワールド空間の青軸(Z軸)方向、privateで定義した値(力)= forwardForce this.myRigidbody.AddForce(this.transform.forward * this.forwardForce); //Unityちゃんを矢印キーまたはボタンに応じて左右に移動させる(追加) if (Input.GetKey("left") && -this.movableRange < this.transform.position.x) { //左に移動(追加) this.myRigidbody.AddForce(-this.turnForce, 0, 0); } else if (Input.GetKey("right") && this.movableRange > this.transform.position.x ) { //右に移動(追加) this.myRigidbody.AddForce(this.turnForce, 0, 0); } //Jumpステートの場合は、Jumpにfalseをセットする if (this.myAnimator.GetCurrentAnimatorStateInfo(0).IsName("Jump")) { this.myAnimator.SetBool("Jump", false); } //ジャンプしていないときにスペースが押されたらジャンプする if (Input.GetKeyDown(KeyCode.UpArrow) && this.transform.position.y < 0.5f) { //ジャンプアニメを再生 this.myAnimator.SetBool("Jump", true); //Unityちゃんに上方向の力を加える this.myRigidbody.AddForce(this.transform.up * this.jumpForce); } } void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "CarTag" || other.gameObject.tag == "TrafficConeTag") { this.isEnd = true; this.stateText.GetComponent<Text>().text = "GAMEOVER!"; } if(other.gameObject.tag == "GoalTag") { this.isEnd = true; this.stateText.GetComponent<Text>().text = "CLEAR!!"; } if(other.gameObject.tag == "CoinTag"){ GetComponent<ParticleSystem>().Play(); Destroy(other.gameObject); } } }
上記のように、this.isEndで3Dキャラクターに減速する値を掛けていますが、ゴールしてもキャラクターは走り続けています。
どなたか、アドバイスよろしくお願いいたします。
何か他に必要な情報があれば、コメントよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/25 22:22