UnityでRigitbodyを使った自機で移動方向のベクトルを坂に沿わせて跳ねないようにして移動したい

前提

初心者です。 

UnityでRigitbodyを使った移動を作っています、で坂を上り下りするときに物理法則にのっとって、跳ねたり跳んだりします。
https://forum.unity.com/proxy.php?image=http%3A%2F%2Fi.imgur.com%2Fp9pqOUb.png%3F1&hash=36e16cf01a6e33a145c02048652d95d5
この図のような感じです。

実現したいこと

図に書いてある通りに物体と坂をきれいにくっつけたいです。

実装方法として、傾斜に沿った進行方向のベクトルを自機に与えたいです。(自機の回転で無理やり向きを変えるのではなくて、あくまでもベクトルの向きです。)

該当のソースコード

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; 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 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>(); float slope = GetSlope(); Vector3 slopeDrection = new Vector3(0, slope, 0); float GetSlope() { if (Physics.Raycast(transform.position, Vector3.down, out RaycastHit hit)) { return Vector3.Angle(Vector3.up, hit.normal); } return 0f; } Debug.Log(slope); 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, 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); } }

試したこと

コードにもある通り、RayCastで自分がいるところの角度を取得して、それを進行方向のY軸ベクトルに入れましたが、自機が坂に乗った瞬間どっかに吹っ飛んでしまいます。slopeの値を0にするコードを書いてもうまくいきません

コメントを投稿

0 コメント