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

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

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

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

Unity3D

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

Q&A

1回答

581閲覧

3D モデルの表面に沿って歩かせたいが90度を超えると落下してしまう

RyotaOkamoto

総合スコア6

C#

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

Unity3D

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

0グッド

0クリップ

投稿2023/03/29 15:52

編集2023/04/07 00:17

実現したいこと

3Dモデルの表面(長い円錐のようないびつなモデル)をキャラクターに歩かせたい。
イメージ的にはスーパーマリオギャラクシーのような感じ。

前提

表面に垂直なベクトルをraycastで取得しquaternion.rotationで立たせ、addforceで地面と垂直なベクトルに力を加えることで重力を再現しています。
ところがキャラクターがxまたはy方向に90度以上傾くとキャラクターの角度が0度に戻りそのまま下へと落下していってしまいます。

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class agent_controller : MonoBehaviour 6{ 7 [SerializeField] float m_MovingTurnSpeed = 360; 8 [SerializeField] float m_StationaryTurnSpeed = 180; 9 [SerializeField] float m_JumpPower = 12f; 10 [Range(1f, 4f)][SerializeField] float m_GravityMultiplier = 2f; 11 [SerializeField] float m_RunCycleLegOffset = 0.2f; //specific to the character in sample assets, will need to be modified to work with others 12 [SerializeField] float m_MoveSpeedMultiplier = 1f; 13 [SerializeField] float m_AnimSpeedMultiplier = 1f; 14 [SerializeField] float m_GroundCheckDistance = 0.1f; 15 16 public Rigidbody m_Rigidbody; 17 Animator m_Animator; 18 public bool m_IsGrounded; 19 float m_OrigGroundCheckDistance; 20 const float k_Half = 0.5f; 21 float m_TurnAmount; 22 float m_ForwardAmount; 23 Vector3 m_GroundNormal; 24 float m_CapsuleHeight; 25 Vector3 m_CapsuleCenter; 26 CapsuleCollider m_Capsule; 27 bool m_Crouching; 28 public Transform m_Transform; 29 30 void Start () { 31 32 } 33 34 Vector3 inputVector; 35 void Update () { 36 CheckGroundStatus(); 37 VerticalGravity(); 38 Controller(); 39 } 40 41 public float speed; 42 public Camera camera; 43 44 void Controller(){ 45 if (Input.GetKey(KeyCode.W)) 46 { 47 transform.position += transform.forward * speed * Time.deltaTime; 48 } 49 if (Input.GetKey(KeyCode.D)) 50 { 51 transform.position += transform.right * speed * Time.deltaTime; 52 } 53 if (Input.GetKey(KeyCode.A)) 54 { 55 transform.position -= transform.right * speed * Time.deltaTime; 56 } 57 if (Input.GetKey(KeyCode.S)) 58 { 59 transform.position -= transform.forward * speed * Time.deltaTime; 60 } 61 62 transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X") * 5, 0)); 63 camera.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") * 1, 0, 0)); 64 } 65 66 void VerticalGravity(){ 67 //進行方向を検知 68 inputVector = m_Transform.forward; 69 //地面に平行な進行方向へのベクトル 70 Vector3 onplane = Vector3.ProjectOnPlane(inputVector, m_GroundNormal); 71 //そっちをみる 72 m_Transform.rotation = Quaternion.LookRotation(onplane, m_GroundNormal); 73 m_Rigidbody.AddForce(-1 * m_GroundNormal * 10); 74 } 75 76 void CheckGroundStatus(){ 77 RaycastHit hitInfo; 78 // helper to visualise the ground check ray in the scene view 79 Debug.DrawRay(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance), Color.red, 100); 80 81 // 0.1f is a small offset to start the ray from inside the character 82 // it is also good to note that the transform position in the sample assets is at the base of the character 83 if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance)) 84 { 85 m_GroundNormal = hitInfo.normal; 86 m_IsGrounded = true; 87 } 88 else 89 { 90 m_IsGrounded = false; 91 m_GroundNormal = Vector3.up; 92 } 93 } 94} 95

試したこと

CheckGroundStatus()をunityのサンプルから直接引用しており、その中のRaycastの第2引数が絶対的な下方向であるVector3.downを使っていたので、代わりに相対的な下方向の-transform.upにしてみましたがやはり90度を超えた段階で落下してしまいます。
お力添えしていただけると助かります。

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

unity 2021.3.16f

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

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

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

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

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

guest

回答1

0

Raycastの第2引数が絶対的な下方向であるVector3.downを使っていたので、代わりに相対的な下方向の-transform.upにしてみましたが

第1引数も同様にtransform.upにする必要があると思いますが、それをしていないのでは。(90°を越えると上方向は地形の裏)

投稿2023/04/06 15:17

ikadzuchi

総合スコア3047

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問