前提
題名の通りです。
UnityでRigidbodyを使った移動で坂を下るときに浮いてしまいます。
https://www.youtube.com/watch?v=AVCt2Y3YhU0
坂から降りるときすこしふわぁってなってしまいます。
こちらの画像はCharacterスクリプトの詳細です。
また、カプセルコライダーの下のスクリプトはRigidbodyの重力を切って、Unity側の重力を上げているスクリプトです。
実現したいこと
坂にぴったり張り付いておりたい
該当のソースコード
C#
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; [RequireComponent(typeof(PlayerInput))] [RequireComponent(typeof(Rigidbody))] public class Character : MonoBehaviour { private Rigidbody rb; private CapsuleCollider capsuleCollider; private InputAction moveAction, jumpAction, CameraAction, dashAction ,squatActiont; //_これ(アンダーヴァー)を識別で着けたりする。unity道場では突けてる public float Speed = 500; //スピードを1.0とインスペクター上で仮定して public float JumpSpeed = 500.0f; public float LimitSpeed = 10.0f; Quaternion targetRotation; private int Jumpflg; public float Stamina = 100; public float Multiplier = 1f; Vector3 normalVector = Vector3.zero; private void Awake() { var input = GetComponent<PlayerInput>(); var actionMap = input.currentActionMap; rb = GetComponent<Rigidbody>(); capsuleCollider = GetComponent<CapsuleCollider>(); moveAction = actionMap["Move"]; moveAction.canceled += OnMoveStop; jumpAction = actionMap["Jump"]; jumpAction.started += Onjump; dashAction = actionMap["Dash"]; dashAction.canceled += DashMoveStop; dashAction.performed += DashMove; squatActiont = actionMap["Squat"]; squatActiont.performed += SquatStart; squatActiont.canceled += SquatStop; targetRotation = transform.rotation; } void OnCollisionEnter(Collision col) { Jumpflg = 0; //Jumpflgを0にする } private void OnCollisionStay(Collision collision) { // 衝突した面の、接触した点における法線を取得 normalVector = collision.contacts[0].normal; Debug.Log(normalVector); } private void Start() { Jumpflg = 0; } void OnMoveStop(InputAction.CallbackContext obj) { Debug.Log("離した"); rb.velocity = new Vector3(0, 0, 0); } void Onjump(InputAction.CallbackContext obj) { if(Jumpflg <= 1) { rb.AddForce(new Vector3(0.0f, JumpSpeed, 0.0f)); Jumpflg++; } } void DashMove(InputAction.CallbackContext obj) { Debug.Log("抑えて"); Speed = 15f; LimitSpeed = 100.0f; } void DashMoveStop(InputAction.CallbackContext obj) { Debug.Log("STOP"); Speed = 10f; LimitSpeed = 10.0f; } void SquatStart(InputAction.CallbackContext obj) { capsuleCollider.height = 1.0f; Speed = 2.5f; Debug.Log("しゃがみなう"); } void SquatStop(InputAction.CallbackContext obj) { capsuleCollider.height = 2.0f; Speed = 5f; Debug.Log("しゃがみ解除"); } void FixedUpdate() { var Dirction = moveAction.ReadValue<Vector2>(); Vector3 inputVector = Vector3.zero; inputVector.x = Dirction.x; inputVector.z = Dirction.y; Vector3 onPlane = Vector3.ProjectOnPlane(inputVector, normalVector); Vector3 cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized; //Scale は乗算 〇〇,new Vector 2 or 3 で左のほう掛ける右のほうになる、この場合XとZを1をかけて取り出してにゼロを掛けて取り出さないようにしている。 Vector3 moveForward = cameraForward * Dirction.y + Camera.main.transform.right * Dirction.x; //カメラのトランスフォームの赤軸(X) if (moveForward != Vector3.zero) { targetRotation = Quaternion.LookRotation(moveForward); } transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * 600); rb.velocity = moveForward * Speed + new Vector3(0, rb.velocity.y * onPlane.y , 0); if (rb.velocity.magnitude > LimitSpeed) { rb.velocity = new Vector3(rb.velocity.x / 1.1f, rb.velocity.y / 1.1f, rb.velocity.z / 1.1f); } rb.AddForce((Multiplier - 1f) * Physics.gravity, ForceMode.Acceleration); } }
試したこと
原因は動かしている対象の下にかかる力が弱いため浮いてるのだと思っています。けれども重力やAddforceを使うと逆に坂を登れなくなってしまいます。
まだ回答がついていません
会員登録して回答してみよう