質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

0回答

277閲覧

Unityでキャラのアクションの実装をしていますが、思うようにキャラが動いてくれません

Hydra_G

総合スコア10

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

1クリップ

投稿2019/01/17 05:43

編集2019/01/17 08:17

Unityでキャラのアクションをスクリプトに実装して再生ボタンを押したら
本来ならカーソルキーを押すと横移動をするようにしたいのですが、Macのキーボードで左右のボタンを押してもアニメーションは動いてるのですが、
同じ場所に止まったまま移動してくれません
エラーメッセージも出ていないので何が原因なのか分かりません
一緒にみてもらえないでしょうか。
よろしくお願いします

PlayerMain.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMain : MonoBehaviour { // === キャッシュ=============================== PlayerController playerCtrl; // ===コード(Monobehaviour基本機能の実装) ========== void Awake() { playerCtrl = GetComponent<PlayerController> (); } // Update is called once per frame void Update () { //操作可能か? if(!playerCtrl.activeSts){ return; } //パッド処理 float joyMv = Input.GetAxis("Horizontal"); playerCtrl.ActionMove (joyMv); //ジャンプ if(Input.GetButtonDown("Jump")) { playerCtrl.ActionJump (); } } }

PlayerController.cs

using

1using System.Collections.Generic; 2using UnityEngine; 3 4public class PlayerController : BaseCharacterController { 5 //===外部パラメータ(Inspector表示)==================== 6 public float initHpMax = 20.0f; 7 [Range(0.1f, 100.0f)] public float initSpeed = 12.0f; 8 9 //===内部パラメータ================= 10 int jumpCount = 0; 11 bool breakEnabled = true; 12 float groundFriction = 0.0f; 13 14 //===コード(Monobehaviour基本機能の実装)=========== 15 protected override void Awake() { 16 base.Awake (); 17 18 //パラメータ初期化 19 speed = initSpeed; 20 SetHP (initHpMax, initHpMax); 21 } 22 23 protected override void FixedUpdateCharacter() { 24 //接地チェック 25 if(jumped) { 26 if ((grounded && !groundedPrev) || (grounded && Time.fixedTime > jumpStartTime + 1.0f)) { 27 Animator.SetTrigger ("Idle"); 28 jumped = false; 29 jumpCount = 0; 30 } 31 } 32 if(!jumped) { 33 jumpCount = 0; 34 GetComponent<Rigidbody2D> ().gravityScale = gravityScale; 35 } 36 37 //キャラの方向 38 transform.localScale = new Vector3(basScaleX * dir, transform.localScale.y, transform.localScale.z); 39 40 //ジャンプ中の横移動減速 41 if(jumped && !grounded) { 42 if (breakEnabled) { 43 breakEnabled = false; 44 speedVx *= 0.9f; 45 } 46 } 47 48 //移動停止(減速)処理 49 if(breakEnabled) { 50 speedVx *= groundFriction; 51 } 52 53 //カメラ 54 Camera.main.transform.position = transform.position - Vector3.forward; 55 } 56 57 //===コード(基本アクション)=========================== 58 public override void ActionMove(float n) { 59 if (!activeSts) { 60 return; 61 } 62 63 //初期化 64 float dirOld = dir; 65 breakEnabled = false; 66 67 //アニメーション指定 68 float moveSpeed = Mathf.Clamp(Mathf.Abs(n), -1.0f, +1.0f); 69 Animator.SetFloat ("MoveSpeed", moveSpeed); 70 //Animator.speed = 1.0f + moveSpeed; //移動速度でアニメーションを変える実験用) 71 72 //移動チェック 73 if (n != 0.0f) { 74 //移動 75 dir = Mathf.Sign (n); 76 moveSpeed = (moveSpeed < 0.5f) ? (moveSpeed * (1.0f / 0.5f)) : 1.0f; 77 speedVx = initSpeed * moveSpeed * dir; 78 } else { 79 //移動停止 80 breakEnabled = true; 81 } 82 83 //その場振り向きチェック 84 if(dirOld != dir) { 85 breakEnabled = true; 86 } 87 } 88 89 public void ActionJump() { 90 switch (jumpCount) { 91 case 0: 92 if (grounded) { 93 Animator.SetTrigger ("Jump"); 94 GetComponent<Rigidbody2D> ().velocity = Vector2.up * 30.0f; 95 jumpStartTime = Time.fixedTime; 96 jumped = true; 97 jumpCount++; 98 } 99 break; 100 case 1: 101 if (!grounded) { 102 Animator.Play ("Player_Jump", 0, 0.0f); 103 GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, 20.0f); 104 jumped = true; 105 jumpCount++; 106 } 107 break; 108 } 109 } 110} 111

BaseCharacterController.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BaseCharacterController : MonoBehaviour { 6 //===外部パラメータ(Inspector表示) ================= 7 public Vector2 velocityMin = new Vector2(-100.0f, -100.0f); 8 public Vector2 velocityMax = new Vector2(+100.0f, +50.0f); 9 10 //===外部パラメータ====================================== 11 [System.NonSerialized] public float hpMax = 10.0f; 12 [System.NonSerialized] public float hp = 10.0f; 13 [System.NonSerialized] public float dir = 1.0f; 14 [System.NonSerialized] public float speed = 6.0f; 15 [System.NonSerialized] public float basScaleX = 1.0f; 16 [System.NonSerialized] public bool activeSts = false; 17 [System.NonSerialized] public bool jumped = false; 18 [System.NonSerialized] public bool grounded = false; 19 [System.NonSerialized] public bool groundedPrev = false; 20 21 //===キャッシュ=========================================== 22 [System.NonSerialized] public Animator Animator; 23 protected Transform groundCheck_L; 24 protected Transform groundCheck_C; 25 protected Transform groundCheck_R; 26 27 //===内部パラメータ======================================= 28 protected float speedVx = 0.0f; 29 protected float speedVxAddPower = 0.0f; 30 protected float gravityScale = 10.0f; 31 protected float jumpStartTime = 0.0f; 32 33 protected GameObject groundCheck_OnRoadObject; 34 protected GameObject groundCheck_OnMoveObject; 35 protected GameObject groundCheck_OnEnemyObject; 36 37 //===コード(Monobehaviour基本機能の実装) ================== 38 protected virtual void Awake() { 39 Animator = GetComponent <Animator> (); 40 groundCheck_L = transform.Find ("GroundCheck_L"); 41 groundCheck_C = transform.Find ("GroundCheck_C"); 42 groundCheck_R = transform.Find ("GroundCheck_R"); 43 44 dir = (transform.localScale.x > 0.0f) ? 1 : -1; 45 basScaleX = transform.localScale.x * dir; 46 transform.localScale = new Vector3 (basScaleX, transform.localScale.y, transform.localScale.z); 47 48 activeSts = true; 49 gravityScale = GetComponent<Rigidbody2D> ().gravityScale; 50 } 51 52 protected virtual void Start () { 53 54 } 55 56 protected virtual void Update () { 57 58 } 59 60 protected virtual void FixedUpdate() { 61 //落下チェック 62 if(transform.position.y < -30.0f) { 63 Dead (false); //死亡 64 } 65 66 //地面チェック 67 groundedPrev = grounded; 68 grounded = false; 69 70 groundCheck_OnRoadObject = null; 71 groundCheck_OnMoveObject = null; 72 groundCheck_OnEnemyObject = null; 73 74 Collider2D[][] groundCheckCollider = new Collider2D[3][]; 75 groundCheckCollider [0] = Physics2D.OverlapPointAll (groundCheck_L.position); 76 groundCheckCollider [1] = Physics2D.OverlapPointAll (groundCheck_C.position); 77 groundCheckCollider [2] = Physics2D.OverlapPointAll (groundCheck_R.position); 78 79 foreach (Collider2D[] groundCheckList in groundCheckCollider) { 80 foreach (Collider2D groundCheck in groundCheckList) { 81 if (groundCheck != null) { 82 if (!groundCheck.isTrigger) { 83 grounded = true; 84 if (groundCheck.tag == "Road") { 85 groundCheck_OnRoadObject = groundCheck.gameObject; 86 } else if (groundCheck.tag == "MoveObject") { 87 groundCheck_OnMoveObject = groundCheck.gameObject; 88 } else if (groundCheck.tag == "Enemy") { 89 groundCheck_OnEnemyObject = groundCheck.gameObject; 90 } 91 } 92 } 93 } 94 } 95 //キャラクター個別の処理 96 FixedUpdateCharacter(); 97 98 //移動計算 99 GetComponent<Rigidbody2D> ().velocity = new Vector2(speedVx, GetComponent<Rigidbody2D>().velocity.y); 100 101 //Velocityの値をチェック 102 float vx = Mathf.Clamp(GetComponent<Rigidbody2D> ().velocity.x, velocityMin.x, velocityMax.x); 103 float vy = Mathf.Clamp(GetComponent<Rigidbody2D>().velocity.y, velocityMin.y, velocityMax.y); 104 GetComponent<Rigidbody2D> ().velocity = new Vector2(vx, vy); 105 } 106 107 protected virtual void FixedUpdateCharacter() { 108 } 109 110 //===コード(基本アクション)========================= 111 public virtual void ActionMove(float n) { 112 if (n != 0.0f) { 113 dir = Mathf.Sign (n); 114 speedVx = speed * n; 115 Animator.SetTrigger ("Run"); 116 } else { 117 speedVx = 0; 118 Animator.SetTrigger ("Idle"); 119 } 120 } 121 122 //===コード(その他) ============================= 123 public virtual void Dead(bool gameOver) { 124 if (!activeSts) { 125 return; 126 } 127 activeSts = false; 128 Animator.SetTrigger ("Dead"); 129 } 130 131 public virtual bool SetHP(float _hp, float _hpMax) { 132 hp = _hp; 133 hpMax = _hpMax; 134 return (hp <= 0); 135 } 136}

画像

補足情報

Unity2017 4.1f1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakura_hana

2019/01/17 07:01

「本来ならカーソルキーを押すと横移動をするようにした」というのは、Mac以外の環境で試してみると正常動作するということですか?それとも「このような動作をさせたい」という希望ですか? とりあえず確認として、 PlayerController.csの「public override void ActionMove(float n) {」の下に Debug.Log(activeSts+":"+n); と記載、 speedVx = initSpeed * moveSpeed * dir; の下に Debug.Log(initSpeed+"/"+moveSpeed+"/"+dir); と記載して実行、コンソールに何が表示されるか見てみてください。(何かしらが想定外の値になってると思います)
izmktr

2019/01/17 09:58

transform.localScale = new Vector3(basScaleX * dir, transform.localScale.y, transform.localScale.z); ここ、localPositionじゃないですか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問