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

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

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

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

1951閲覧

Unity3Dでの<Rigidbody>().velocityを用いた斜め前ジャンプについて

mikaduki444

総合スコア6

C#

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2018/05/28 11:26

編集2022/01/12 10:55

前提・実現したいこと

unity3DでGetCompornent<Rigidbody>().velocityを用いてキャラクターの向いている方向の正面斜め上(0,1,1)にジャンプさせたいのですが、以下のソースコードでは何故か上方向にしか力がかからずその場ジャンプになってしまいます。
エラー等は発生せず実行は可能です。

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Jumper : MonoBehaviour 6{ 7 8 Vector3 force; 9 new Rigidbody rigidbody; 10 11 void OnTriggerExit(Collider other) 12 { 13 14 Player.animator.SetBool("Walking", false); 15 Player.animator.SetBool("Running", false); 16 Player.animator.SetBool("Jumping", true); 17 StartCoroutine("Jump"); 18 } 19 20 private IEnumerator Jump() 21 { 22 rigidbody = MazeStageGeneral.utc.GetComponent<Rigidbody>(); 23 MazeStageGeneral.utc.GetComponent<Player>().enabled = false; 24 MazeStageGeneral.utc.transform.localEulerAngles = transform.localEulerAngles; 25 force =(Vector3.up + Vector3.forward) * 3; 26 27 yield return new WaitForSeconds(0.4f); 28 29 GetComponent<ParticleSystem>().Play(); 30 rigidbody.velocity = force; 31 32 33 yield return new WaitForSeconds(1f); 34 Player.animator.SetBool("Jumping", false); 35 MazeStageGeneral.utc.GetComponent<Player>().enabled = true; 36 } 37} 38

Player.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Player: MonoBehaviour 6{ 7 [SerializeField] Vector3 velocity; //移動方向 8 public static float speed = 2.0f; //移動速度 9 [SerializeField] float rotation = 0.2f; //回転速度 10 public static Animator animator; 11 12 void Start() 13 { 14 animator = GetComponent<Animator>(); 15 } 16 17 void Update() 18 { 19 velocity = Vector3.zero; 20 21 if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.UpArrow)) 22 { 23 animator.SetBool("Walking", true); 24 if (!ForestSound.footsteps.isPlaying) { ForestSound.footsteps.PlayOneShot(ForestSound.footsteps.clip); } 25 26 if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) 27 { 28 speed = 5.0f; 29 if (!ForestSound.footsteps.isPlaying) { ForestSound.footsteps.PlayOneShot(ForestSound.footsteps.clip); } 30 animator.SetBool("Walking", false); 31 animator.SetBool("Running", true); 32 } 33 if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift)) 34 { 35 speed = 2.0f; 36 if (ForestSound.footsteps.isPlaying) { ForestSound.footsteps.Stop(); } 37 animator.SetBool("Running", false); 38 animator.SetBool("Walking", true); 39 } 40 } 41 else 42 { 43 animator.SetBool("Walking", false); 44 animator.SetBool("Running", false); 45 if (ForestSound.footsteps.isPlaying) { ForestSound.footsteps.Stop(); } 46 } 47 48 if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) 49 { 50 //移動(X軸、Y軸、Z軸) 51 velocity.x += 1; 52 } 53 if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) 54 { 55 velocity.x -= 1; 56 } 57 if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) 58 { 59 velocity.z += 1; 60 } 61 if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) 62 { 63 velocity.z -= 1; 64 } 65 if ((Input.GetKey(KeyCode.W)&& Input.GetKey(KeyCode.A)) || (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.LeftArrow))) 66 { 67 velocity.x -= 1; 68 velocity.z += 1; 69 } 70 if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) || (Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.RightArrow))) 71 { 72 velocity.x += 1; 73 velocity.z += 1; 74 } 75 if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A) || (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.LeftArrow))) 76 { 77 velocity.x -= 1; 78 velocity.z -= 1; 79 } 80 if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D) || (Input.GetKey(KeyCode.DownArrow) && Input.GetKey(KeyCode.RightArrow))) 81 { 82 velocity.x += 1; 83 velocity.z -= 1; 84 } 85 86 velocity = velocity.normalized * speed * Time.deltaTime; 87 if(velocity.magnitude > 0) 88 { 89 transform.position += velocity; 90 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(velocity), rotation); 91 } 92 93 } 94 95} 96

試したこと

AddForceなども試しましたが挙動が怪しく、velocityで実装したい所存です。

補足情報(FW/ツールのバージョンなど)

Unity 2017.3.1f1 Personal(64bit)
Microsoft Visual Studio 2016
windows10

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

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

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

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

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

Bongo

2018/05/29 03:53

念のため確認したいのですが、MazeStageGeneral.utcのRigidbodyには「Freeze Position」あたりのチェックは入れていませんよね?その前提で、ご提示のスクリプト以外にMazeStageGeneral.utcの動きに影響を与えるコードがあれば、それもご提示いただけると回答がつきやすくなりそうです。私の見たところ、「キャラクターの向いている方向の正面斜め上にジャンプさせたい」という意図に反して、速度を与える方向がワールド座標系での固定方向になっている疑いがあるのですが、それにしても真上にしかジャンプしてくれないという現象がいまいち説明がつかないのです。他のスクリプトが何か悪さをしてはいないか気になりますが、可能性はあるでしょうか?
mikaduki444

2018/05/29 15:02

確認したところ「Freeze Rotate」ではz,xにチェックを入れていますが、「Freeze Position」にはチェックは入れていませんでした。また、utcに影響を与えていそうなものとして、スクリプト内で無効にはしていますが、念のためPlayer.csを記載します。
guest

回答1

0

キャラクターにアタッチされていると思われる「Animator」コンポーネントのチェックを外して無効化した場合、動きはどうなるでしょうか?
もしチェックを外せば斜めに跳ぶようになるようでしたら、スクリプトから与えたキャラクターの動きをアニメーションが上書きしてしまって正しい方向に跳ばない可能性がありそうです(参考:Unity5でRoot Motionを利用したアニメーションと、その注意点について - テラシュールブログ)。

Jumpメソッドに少々手を加えて、ジャンプコルーチン実行中はapplyRootMotionをオフにしてみました。
ですが、もし他のアニメーションもルートモーション不適用で問題ないなら、コードは元のままにして、インスペクタ上でキャラクターの「Animator」コンポーネントの「Apply Root Motion」をオフにするだけでもいいかと思います。

C#

1 private IEnumerator Jump() 2 { 3 // ルートモーションの適用設定が疑わしい? 4 // 現在のapplyRootMotionを覚えておいて、一旦applyRootMotionは切る 5 var applyRootMotion = Player.animator.applyRootMotion; 6 Player.animator.applyRootMotion = false; 7 8 rigidbody = MazeStageGeneral.utc.GetComponent<Rigidbody>(); 9 MazeStageGeneral.utc.GetComponent<Player>().enabled = false; 10 MazeStageGeneral.utc.transform.localEulerAngles = transform.localEulerAngles; 11 12 // コメントで申し上げた「与える速度がワールド座標に対する固定方向になっている疑い」に関する修正... 13 // 上方向はVector3.upでいいと思いますが、前方向としてVector3.forwardを与えてしまうと 14 // キャラクターの向きにかかわらずZ軸+方向に跳んでしまうのではないでしょうか? 15 // ご質問の文面から想像すると、前方ベクトルとしてはキャラクターのforwardの方が適切かと思います 16 // (Vector3.forwardの方が意図通りの動きであれば、この修正は不要なので元に戻してしまってください) 17 force = (Vector3.up + MazeStageGeneral.utc.transform.forward) * 3; 18 19 yield return new WaitForSeconds(0.4f); 20 GetComponent<ParticleSystem>().Play(); 21 rigidbody.velocity = force; 22 23 yield return new WaitForSeconds(1f); 24 Player.animator.SetBool("Jumping", false); 25 MazeStageGeneral.utc.GetComponent<Player>().enabled = true; 26 27 // applyRootMotionを元に戻す 28 Player.animator.applyRootMotion = applyRootMotion; 29 }

投稿2018/05/29 20:53

Bongo

総合スコア10807

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問