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

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

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

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

スクロール

スクロールとは、ディスプレイスクリーン上において連続的にコンテンツが滑っていくことを指します。

Unity

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

Q&A

解決済

1回答

1678閲覧

急斜面を登らせたくない

1000k_k_y_a_n

総合スコア6

C#

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

スクロール

スクロールとは、ディスプレイスクリーン上において連続的にコンテンツが滑っていくことを指します。

Unity

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

0グッド

0クリップ

投稿2021/01/19 13:15

急角度の斜面を移動するときに、下のgifのように角度によって移動する速度が変わってしまいます。
これを解決し、そのうえで登ることが可能な角度を設定できるようにしたいのですが、どのようにすればいいのかが分かりません。
どなたかわかる方、お教えいただけないでしょうか。

c#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5using UnityEngine.Events; 6 7public class PlayerController : MonoBehaviour 8{ 9 public float m_Speed; 10 public float moveForceMultiplier; 11 public float m_Jumpheight; 12 public float playerScale; 13 public float LimitSpeed; 14 public float LimitAngle; 15 public float HP; 16 public int m_JumpCountNumber; 17 18 private bool space; 19 private bool E; 20 private bool interact; 21 22 23 private float tdt; 24 private float horizontal; 25 26 public float Horizontal 27 { 28 get 29 { 30 return horizontal; 31 } 32 set 33 { 34 horizontal = value; 35 } 36 } 37 38 Rigidbody2D m_rigidbody2D; 39 Vector2 m_Vector2; 40 Vector3 selfAngle; 41 42 [SerializeField] UnityEvent unityEvent; 43 44 private int m_JumpCount; 45 public int M_JumpCount 46 { 47 get 48 { 49 return m_JumpCount; 50 } 51 set 52 { 53 m_JumpCount = value; 54 } 55 } 56 57 [System.NonSerialized] 58 public Vector3 groundNormal = Vector3.zero; 59 60 private Vector3 lastGroundNormal = Vector3.zero; 61 62 [System.NonSerialized] 63 public Vector3 lastHitPoint = new Vector3(Mathf.Infinity, 0, 0); 64 65 protected float groundAngle = 0; 66 private void Start() 67 { 68 m_rigidbody2D = GetComponent<Rigidbody2D>(); 69 m_JumpCount = m_JumpCountNumber; 70 71 if(unityEvent == null) 72 { 73 unityEvent = new UnityEvent(); 74 } 75 76 } 77 private void FixedUpdate() 78 { 79 m_Vector2 = new Vector2(horizontal * m_Speed * Time.deltaTime, m_rigidbody2D.velocity.y); 80 m_rigidbody2D.AddForce(moveForceMultiplier * (m_Vector2 - m_rigidbody2D.velocity)); 81 82 if (horizontal > 0) 83 { 84 PlayerTrun(true); 85 } 86 else if(horizontal < 0) 87 { 88 PlayerTrun(false); 89 } 90 } 91 92 private void Update() 93 { 94 selfAngle = transform.localEulerAngles; 95 tdt = Time.deltaTime; 96 horizontal = Input.GetAxis("Horizontal"); 97 space = Input.GetKeyDown(KeyCode.Space); 98 E = Input.GetKeyDown(KeyCode.E); 99 100 if (space) 101 { 102 Jump(); 103 } 104 105 if (E && interact) 106 { 107 unityEvent.Invoke(); 108 } 109 110 if(m_rigidbody2D.velocity.magnitude > LimitSpeed) 111 { 112 float divid = 1.1f; 113 m_rigidbody2D.velocity = new Vector2(m_rigidbody2D.velocity.x / divid, m_rigidbody2D.velocity.y); 114 } 115 116 117 } 118 private void OnTriggerStay2D(Collider2D other) 119 { 120 bool damageZone = other.CompareTag("DamageZone"); 121 122 if(damageZone) 123 { 124 HP -= 1; 125 Debug.Log(HP); 126 } 127 128 if (other.CompareTag("InteractZone")) 129 { 130 interact = true; 131 Debug.Log("Interact"); 132 } 133 } 134 135 private void OnTriggerExit2D(Collider2D other) 136 { 137 if(other.CompareTag("InteractZone")) 138 { 139 interact = false; 140 Debug.Log("not Interact"); 141 } 142 } 143 144 void PlayerTrun(bool LeftORRight) 145 { 146 Vector3 scale = transform.localScale; 147 148 if (LeftORRight) 149 { 150 scale.x = playerScale; 151 } 152 else if(!LeftORRight) 153 { 154 scale.x = -playerScale; 155 } 156 transform.localScale = scale; 157 } 158 159 void Jump() 160 { 161 if (m_JumpCount > 0) 162 { 163 m_JumpCount -= 1; 164 m_rigidbody2D.AddForce(Vector2.up * m_Jumpheight, ForceMode2D.Impulse); 165 } 166 if (space) 167 { 168 Debug.Log(m_JumpCount); 169 } 170 } 171} 172

30度の場合↓
イメージ説明
60度の場合↓
イメージ説明

詳細
使用Ver:Unity 2019.4.17f Personal
2D、横スクロールです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

C#

1   private void FixedUpdate() 2 { 3 m_Vector2 = new Vector2(horizontal * m_Speed * Time.deltaTime, m_rigidbody2D.velocity.y); 4 m_rigidbody2D.AddForce(moveForceMultiplier * (m_Vector2 - m_rigidbody2D.velocity)); 5 6 if (horizontal > 0) 7 { 8 PlayerTrun(true); 9 } 10 else if(horizontal < 0) 11 { 12 PlayerTrun(false); 13 } 14 15     //Updateから移動 16     if(m_rigidbody2D.velocity.magnitude > LimitSpeed) 17 { 18 float divid = 1.1f; 19 m_rigidbody2D.velocity = new Vector2(m_rigidbody2D.velocity.x / divid, m_rigidbody2D.velocity.y); 20 } 21 22     if(grounded)//接地判定 23     { 24        if(m_rigidbody2D.velocity.x > 0) 25        { 26           m_rigidbody2D.velocity.x -= m_rigidbody2D.velocity.y; 27        } 28        else 29        { 30           m_rigidbody2D.velocity.x += m_rigidbody2D.velocity.y; 31        } 32     } 33  }

OnCollisionEnter等を利用して接地判定を実装すれば、上記のようなコードで、斜面での移動速度が一定になると思います。
登れる斜面の角度ですが、

C#

1float currentRatio = m_rigidbody2D.y/Mathf.Abs(m_rigidbody2D.x); 2if(currentRatio > limit) 3   { 4      m_rigidbody2D.velocity.x = 0; 5   }

以上のコードをFixedUpdateの最後に入れていただけると制御できると思います。limitに0から1の値を試してみてください。
私の方で試せていないので、確実ではありませんが、velocityの値を利用すると上手く行くのではないでしょうか。
これ以外にはCharacterControllerの利用が考えられますが、コードの書き換えが大変なのでオススメはしません。
ご参考になれば幸いです。

投稿2021/01/20 14:31

編集2021/01/20 15:07
Zin.Creat

総合スコア42

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

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

1000k_k_y_a_n

2021/01/22 03:31

うまく動かすことが出来ました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問