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

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

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

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

Unity

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

Q&A

解決済

1回答

4938閲覧

unity 2Dアクションゲーム 坂で滑らないようにしたい

pokkuru

総合スコア8

C#

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

Unity

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

1グッド

0クリップ

投稿2020/11/29 08:31

前提・実現したいこと

unity初心者です。2Dアクションゲームを作っているのですが、
キャラクターが壁にぶつかった時にくっついてしまうので、摩擦を0にしたところ、
壁にくっつくことは無くなりましたが、坂を登ろうとすると滑って登れなくなって
しまったのですが、どうすればいいでしょうか?

発生している問題

2D物理マテリアルの摩擦を0にして、キャラクターに付けたところ、
壁にくっつくことは無くなったが、坂が滑って登れなくなった

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class player2d : MonoBehaviour 7{ 8 public float speed; 9 private Rigidbody2D rb; 10 public float jumpForce; 11 bool jump = false; 12 Animator animator; 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 rb = GetComponent<Rigidbody2D>(); 18 animator = GetComponent<Animator>(); 19 } 20 21 // Update is called once per frame 22 private void Update() 23 { 24 float bottomY = Camera.main.transform.position.y - Camera.main.orthographicSize * 2; 25 26 if (gameObject.transform.position.y < bottomY) 27 { 28 int sceneIndex = SceneManager.GetActiveScene().buildIndex; 29 SceneManager.LoadScene(sceneIndex); 30 } 31 32 if (Input.GetKeyDown("space") && !jump) 33 { 34 rb.AddForce(Vector2.up * jumpForce); 35 jump = true; 36 } 37 38 } 39 40 private void OnCollisionEnter2D(Collision2D other) 41 { 42 if(other.gameObject.CompareTag("Ground")) 43 { 44 jump = false; 45 animator.SetInteger("CharaState", 0); 46 } 47 } 48 49 void FixedUpdate() 50 { 51 float horizontalKey = Input.GetAxis("Horizontal"); 52 53 if(horizontalKey == 0 && jump == false) 54 { 55 rb.velocity = Vector2.zero; 56 } 57 58 if(jump == true) 59 { 60 animator.SetInteger("CharaState",3); 61 } 62 63 if(horizontalKey != 0 && jump == true) 64 { 65 animator.SetInteger("CharaState", 3); 66 } 67 else if(horizontalKey != 0) 68 { 69 70 71 Vector2 liscale = gameObject.transform.localScale; 72 73 if ((liscale.x > 0 && horizontalKey < 0)||(liscale.x < 0 && horizontalKey > 0)) 74 { 75 liscale.x *= -1; 76 gameObject.transform.localScale = liscale; 77 } 78 animator.SetInteger("CharaState", 1); 79 } 80 else if (jump == true) 81 { 82 animator.SetInteger("CharaState", 3); 83 } 84 else 85 { 86 animator.SetInteger("CharaState", 0); 87 } 88 89 Vector2 movement = new Vector2(horizontalKey, 0); 90 rb.AddForce(movement * speed); 91 Debug.Log(GetComponent<Rigidbody2D>().velocity); 92 } 93} 94

###試したこと

摩擦を0.05まで増やし、矢印キーを押した時のキャラクターの移動速度も上げてみましたが
改善されませんでした

yamatoman👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

壁にくっつきたくは無いと思うので、摩擦は0でいいと思います。
普段3Dを触っているので参考になるか分かりませんが、

キャラクターの足元より少し上から下に向かってRayを飛ばす。
地面にRayが当たったらVector2.Angle()で角度を算出する。
角度が一定以上なら…… といった感じで処理を分けるといいと思います。

投稿2020/11/30 03:17

PinoMatcha

総合スコア368

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問