前提・実現したいこと
横スクロールアクションで梯子を使う時に落下しないようにしたいと考えています。
Rigidbody2DではUse Gravityが使えないようなので、
ladder = true(梯子オブジェクトに接触)状態時に↑のキーを押す事で、
movement_active を true にし、
movement_active = true の間は以下の記述で落下を停止。
GetComponent<Rigidbody2D>().velocity = Vector3.zero; GetComponent<Rigidbody2D>().isKinematic = true;
次回、ジャンプボタンが押された時にmovement_active = falseで
GetComponent<Rigidbody2D>().isKinematic = false;
落下状態を再開するしくみです。
ソースコード
Player.cs
public class Player : MonoBehaviour { bool Lock = true; // === キャッシュ ========================================== PlayerController playerCtrl; void Awake() { playerCtrl = GetComponent<PlayerController>(); } void Update() { // 操作可能か? if (!playerCtrl.activeSts) { return; } // パッド処理 float joyMvX = Input.GetAxis("Horizontal"); float joyMvY = Input.GetAxis("Vertical"); playerCtrl.Horizontal_Move(joyMvX); playerCtrl.Vertical_Move(joyMvY); if (Input.GetButtonDown("Jump"))// ジャンプ { playerCtrl.ActionJump(); return; } if (joyMvY > 0) { playerCtrl.Movement_Object(); } if (joyMvY < 0 & !(Input.GetButtonDown("Jump") )) { playerCtrl.Movement_Object(); } //省略 }
PlayerController.cs
public class PlayerController : BaseCharacterController { // === 外部パラメータ(インスペクタ表示) ===================== public float initHpMax = 20.0f; public float moveSpeed; [Range(0.1f, 100.0f)] public float initSpeed = 5.0f; public float Jumphight = 14.0f; public float Rayoutput1 = 0.0f; // === 内部パラメータ ====================================== public int jumpCount = 0; bool breakEnabled = true; float groundFriction = 0.0f; // === コード(Monobehaviour基本機能の実装) ================ protected override void Awake() { base.Awake(); // パラメータ初期化 speed = initSpeed; SetHP(initHpMax, initHpMax); } public void ActionJump() { ladder_active = false; stair_active = false; movement_active = false; switch (jumpCount) { case 0: if (grounded)// 地面に接触した状態でのジャンプ { animator.SetTrigger("Jump"); GetComponent<Rigidbody2D>().velocity = Vector2.up * Jumphight; jumpStartTime = Time.fixedTime; jumped = true; jumpCount++; } else if (!grounded)// // 空中にいてジャンプを行っていない場合 {//省略} case 1: if (!grounded)// 地面に接触していない状態 { animator.Play("Player_Jump", 0, 0.0f); GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, Jumphight); jumped = true; jumpCount++; } break; } } public void Movement_Object() { if (ladder == true){ladder_active = true; movement_active = true;}; if (stair == true){ stair_active = true; movement_active = true; }; if (true)//はしご状態時等に重力を無効化 { GetComponent<Rigidbody2D>().velocity = Vector3.zero; GetComponent<Rigidbody2D>().isKinematic = true; } else if (movement_active == false)//通常時に重力を有効化 { GetComponent<Rigidbody2D>().isKinematic = false; } }
発生している問題
1.落下の停止は成功しますが、その後ジャンプボタンを押しても落下が解除されず、
壁を貫通したうえで上昇してしまいます。
GetComponent<Rigidbody2D>().isKinematic = false;
が効いていないものと思われますが、画像の様にインスペクター上は
movement_activeのチェックがfalseに解除されています。
2.movement_active を true なった瞬間に、
前方入力以外を受け付けなくなる時間が0.5秒程あるようです。
おそらくPlayer.csのif (joyMvY > 0){playerCtrl.Movement_Object();}が上を押している間常に稼働している
のではないかと予想しますが、実際の切り替え動作はPlayerController.csでmovement_activeの切り替えを行わせている為に解決策が思いつきませんでした。
この2つの問題に関わるエラーメッセージは発生していません。
何か解決策があればご教示お願い致します。
補足情報(FW/ツールのバージョンなど)
Unity 2018.1.1f1
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/02 14:02
2018/07/03 09:26
2018/07/03 14:25