急角度の斜面を移動するときに、下の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
詳細
使用Ver:Unity 2019.4.17f Personal
2D、横スクロールです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/22 03:31