63151


Unity3Dです。斜面を上り下りする時に地面に沿って移動したいです。use gravtiyや
常にY軸に-50など数字を入れておくという方法など試しましたが上手くいきません。

OnCollisionStay()関数部のように壁ずりベクトルを計算して平面に沿って移動しているなずなのですがなぜ斜面を下る時に空中に浮いてしまうのでしょか？

[![画像の説明をここに入力](https://i.stack.imgur.com/o4csU.png)](https://i.stack.imgur.com/o4csU.png)

[code]

    using UnityEngine;
    
    public class Controller : MonoBehaviour
    {
    
        /* Ik 関係
        Vector3 rightFootPos = new Vector3(0, 0, 0);
        Vector3 leftFootPos = new Vector3(0, 0, 0);
    
        Quaternion rightFootRot = new Quaternion(0, 0, 0, 0);
        Quaternion leftFootRot = new Quaternion(0, 0, 0, 0);
    
        bool isRightFootIK = false;
        bool isLeftFootIK = false;
    
        float RightIkWeight = 0;
        float LeftIkWeight = 0;
    
        Vector3 rayPositionOffset = new Vector3(0, 0, 0);
        float rayRange = 1.0f;
        float offset = 0.1f;
    
        */
    
    
        private float input_h;
        private float input_v;
        private Vector3 move;
        private Rigidbody rb;
        private float walk_speed;//移動速度
        private Animator ani;//移動速度
        private Vector3 gravity;
        private bool isJump = false;//ジャンプしてるかどうかを管理する
        private Vector3 v;
        private bool isDush;
        private Vector3 move_direction;//移動方向
        private bool c = false;
      //  private CharacterController cc;
    
        private Vector3 planeVec;//平面ベクトル
    
        GameObject ground_ray;
    
        /*坂道計算*/
        GameObject forward_ray;
    
        [SerializeField]
        float slope_range;
        [SerializeField]
        Vector3 slope_direc = new Vector3(0, 0, 0);
        [SerializeField]
        Vector3 slope_direc_b = new Vector3(0, 0, 0);
    
        GameObject contacts;
    
      //  [SerializeField]
        Vector3 slope_pos;
        // Use this for initialization
        void Start()
        {
    
            ground_ray = GameObject.Find("GroundCheck");
            forward_ray = GameObject.Find("SlopeCheck");
            contacts = GameObject.Find("contacts");
            move = new Vector3(0, 0, 0);
            //    cc = GetComponent<CharacterController>();
            forward_ray.transform.position = slope_pos + forward_ray.transform.position;
            walk_speed = 100;
            gravity = Vector3.zero;
            rb = GetComponent<Rigidbody>();
            ani = GetComponent<Animator>();
    
    
        }
    
    
    
        void Gravity_Mng()//ジャンプ管理
        {
        }
    
    
    
    
        void Move_Mng()
        {
    
            input_h = Input.GetAxis("Horizontal");
            input_v = Input.GetAxis("Vertical");
            Vector3 move_z = new Vector3();
            Vector3 move_x = new Vector3();
            move_z = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized * input_v;
            move_x = Camera.main.transform.right * input_h;
            move_direction = move_x + move_z;
            move_direction.y = 0;
    
            move.x = move_direction.x * walk_speed;
            move.z = move_direction.z * walk_speed;
    
            if (move_direction != Vector3.zero)
            {
                move_direction.y = 0;
                transform.rotation = Quaternion.LookRotation(move_direction.normalized);
            }
    
    
    
    
        }
    
    
        float getSpeed()
        {
            return Mathf.Abs(Mathf.Sqrt( Mathf.Abs(move.x * move.x) + Mathf.Abs(move.z * move.z)));
        }
    
    
    
    
        // Update is called once per frame
        void Update()
        {
    
    //        Debug.Log("move_direction: " + move_direction);
     //       Debug.Log("move :          " + move);
    
    
       //     Debug.Log(" update move.y: " + move.y);
    
    
        }
    
    
        //////////////////////////////////////////////////////////////////////////////////////////////    
        void FixedUpdate()
        {
    
            Gravity_Mng();
            Move_Mng();
    
    
    //        rb.AddForce(move.x, move.y, move.z);
            rb.AddForce(move.x,move.y,move.z);
    
    
    
    
        }
        //////////////////////////////////////////////////////////////////////////////////////////////
    
    
        /**/
        private void OnCollisionStay(Collision collision)
        {
    
            // Debug.Log("OnCollisionStay: " + collision.contacts[0].point);
            contacts.transform.position = collision.contacts[0].point;
            //Debug.Log(collision.contacts[0].point);
            // Debug.Log("あああああ");
    
    
            Vector3 f = transform.forward;
            Vector3 vv = collision.contacts[0].normal;
            Vector3 dir = f - Vector3.Dot(f, vv) * vv;
    
    //        Debug.Log("進行ベクトル: " + f);
      //      Debug.Log("法線ベクトル: " + vv);
        //    Debug.Log("壁ずりベクトル: " + dir);
    
          //  Debug.Log("平面ベクトル" + dir);
    
    
            //        f = dir.normalized * walk_speed;//ｙ軸だけ使う
            //        planeVec = dir.normalized * walk_speed;
            //        planeVec = dir.normalized * getSpeed();
    
    
            Debug.Log("getSpeed(): " + getSpeed());
            planeVec = dir.normalized * getSpeed();
    
            //move.y = planeVec.y;
            move = planeVec;
    
    
            Debug.Log("move.y: " + move.y);
            Debug.Log(rb.velocity.y);
    
    
        }
    
        private void OnCollisionExit(Collision c)
        {
        //    move.y = -200.0f;
        }
    
    
    
    }
    
[/code]


CC BY-SA 4.0
2020-02-16T08:07:53.423
63086
2020-02-18T09:13:52.137
2020-02-17T16:33:05.050
32986
question
c# unity3d
壁ずりベクトルを使って斜面を平面に沿って上り下りしたい。


重力関係がどうなっているのかわからないのですが、仮に働いていないとすると、斜面を下るときは地形との衝突判定は発生しないのではないでしょうか。


CC BY-SA 4.0
2020-02-18T09:13:52.137
63151
2020-02-18T09:13:52.137
20885
63086
answer