物体运动学习笔记(二)

跟着CatLike大神的教程敲了一遍,记录下学习心得。地址:https://catlikecoding.com/unity/tutorials/ 

通过刚体移动。

一、复合运算符赋值 desiredJump  ,使 desiredJump在接受到按键输入后为True,直到主动赋值为false,它才为false;

desiredJump |= Input.GetButtonDown("Jump");
        if (desiredJump) {
            desiredJump = false;
            Jump(gravity);
        }

二、通过状态计数来判断状态

    int groundContactCount, steepContactCount;

    bool OnGround => groundContactCount > 0;

    bool OnSteep => steepContactCount > 0;

三、捕捉到地面防止物体飞跃

运动物体飞跃时给物体施加一个相对地面向下的速度

        float dot = Vector3.Dot(velocity, hit.normal);
        if (dot > 0f) {
            velocity = (velocity - hit.normal * dot).normalized * speed;
        }

四、跳跃速度v=sqrt(-2*grivaty*height)

float jumpSpeed = Mathf.Sqrt(2f * gravity.magnitude * jumpHeight);
原文地址:https://www.cnblogs.com/DazeJiang/p/14324771.html