예제 #1
0
    void FixedUpdate()
    {
        if (floatToSleep)
        {
            if (body.IsSleeping())
            {
                floatDelay = 0f;
                return;
            }

            if (body.velocity.sqrMagnitude < 0.0001f)
            {
                floatDelay += Time.deltaTime;
                if (floatDelay >= 1f)
                {
                    return;
                }
            }
            else
            {
                floatDelay = 0f;
            }
        }

        body.AddForce(
            CustomGravity.GetGravity(body.position), ForceMode.Acceleration
            );
    }
    void FixedUpdate()
    {
        if (isAbleToSleep)
        {
            // prevent PhysicX to overwork.
            if (rb.IsSleeping())
            {
                delay = 0f;
                return;
            }

            // check if the object is moving.
            if (rb.velocity.magnitude < 0.001f)
            {
                delay += Time.deltaTime;
                if (delay >= 1f)
                {
                    return;
                }
            }
            else
            {
                delay = 0f;
            }
        }

        // add force following the current custom gravity direction.
        rb.AddForce(CustomGravity.GetGravity(rb.position), ForceMode.Acceleration);
    }
예제 #3
0
    void FixedUpdate()
    {
        Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis);

        UpdateState();
        AdjustVelocity();

        if (desiredJump)
        {
            desiredJump = false;
            Jump(gravity);
        }

        if (Climbing)
        {
            velocity -= contactNormal * (maxClimbAcceleration * 0.9f * Time.deltaTime);
        }
        else if (OnGround && velocity.sqrMagnitude < 0.01f)
        {
            velocity += contactNormal * (Vector3.Dot(gravity, contactNormal) * Time.deltaTime);
        }
        else if (desiredClimbing && OnGround)
        {
            velocity += (gravity - contactNormal * (maxClimbAcceleration * 0.9f)) * Time.deltaTime;
        }
        else
        {
            velocity += gravity * Time.deltaTime;
        }

        body.velocity = velocity;
        ClearState();
    }
예제 #4
0
    void FixedUpdate()
    {
        Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis);

        velocity = body.velocity;
        UpdateState();
        AdjustVelocity();

        if (desiredJump)
        {
            desiredJump = false;
            Jump(gravity);
        }
        if (Climbing)
        {
            velocity -= contactNormal * (maxClimbAcceleration * 0.9f * Time.deltaTime); //0.9f so that climbing around corners is possible
        }
        else if (OnGround && velocity.sqrMagnitude < 0.01f)                             //for standing still on slopes
        {
            velocity += contactNormal * (Vector3.Dot(gravity, contactNormal) * Time.deltaTime);
        }
        else if (desiresClimbing && OnGround)// slows you down if youre not climing
        {
            velocity += (gravity - contactNormal * (maxClimbSpeed * 0.9f)) * Time.deltaTime;
        }
        else
        {
            velocity += gravity * Time.deltaTime;
        }
        body.velocity = velocity;
        ClearState();
    }
예제 #5
0
    private void FixedUpdate()
    {
        //upAxis = -Physics.gravity.normalized;
        //5.2.2 自定义重力
        Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis);

        //velocity = body.velocity;
        UpdateState();
        AgjustVelocity();

        ////float maxSpeedChange = maxAcceleration * Time.deltaTime;
        //float acceleration = onGround ? maxAcceleration : maxAirAcceleration;
        //float maxSpeedChange = acceleration * Time.deltaTime;

        ////通过当前加速度逼近目标速度
        //velocity.x = Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
        //velocity.z = Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);


        if (desiredJump)
        {
            desiredJump = false;
            Jump(gravity);
        }

        velocity += gravity * Time.deltaTime;

        //控制刚体速度
        body.velocity = velocity;

        //onGround = false;
        ClearState();
    }
예제 #6
0
    private void FixedUpdate()
    {
        float r = !rb.IsSleeping() && floatDelay <= 0.0001f ? 1 : 0;
        float g = !rb.IsSleeping() && floatDelay <= 1f && floatDelay > 0.0001f ? 1 : 0;
        float b = rb.IsSleeping() ? 1 : 0;

        mat.color = new Color(r, g, b);
        if (rb.IsSleeping())
        {
            floatDelay  = 0;
            lastPos     = transform.position;
            rb.velocity = Vector3.zero;
            return;
        }

        if (Vector3.Distance(lastPos, transform.position) < 0.01f)
        {
            floatDelay += Time.deltaTime;
            if (floatDelay > 1f)
            {
                rb.velocity = Vector3.zero;
                return;
            }
        }
        else
        {
            floatDelay = 0f;
            lastPos    = transform.position;
        }

        rb.AddForce(CustomGravity.GetGravity(rb.position), ForceMode.Acceleration);
    }
    /// <summary>
    /// 按照固定时长被调用,在被推迟之后仍然会调用本应调用的次数
    /// </summary>
    void FixedUpdate()
    {
        // 1. 记录当前重力及反方向
        Vector3 gravity = CustomGravity.GetGravity(m_body.position, out m_upAxis);

        // 2. 更新当前物体物理状态为Update做准备
        UpdateState();
        // 3. 更新速度
        AdjustVelocity();
        // 4. 如果之前(Update中)按下了跳跃键
        if (m_desiredJump)
        {
            // 5. 将“妄图跳跃”重置代表已经进行跳跃
            m_desiredJump = false;
            // 6. 进行跳跃
            Jump(gravity);
        }

        // 7. 在速度变化上叠加重力影响
        m_velocity += gravity * Time.deltaTime;
        // 8. 更新rigidbody速度
        m_body.velocity = m_velocity;

        // 9. 清空为当前帧其他函数记录的状态
        ClearState();
    }
예제 #8
0
    void FixedUpdate()
    {
        if (floatToSleep)
        {
            if (body.IsSleeping())
            {
                floatDelay = 0f;
                return;
            }

            if (body.velocity.sqrMagnitude < 0.0001f) // Stop the rigidbody if its velocity is tiny (so that body.IsSpleeping() can be called)
            {
                floatDelay += Time.deltaTime;
                if (floatDelay >= 1f)   // Wait for 1 second just to be sure the rigidbody is not momentarily hovering in place for some reasons
                {
                    return;
                }
            }
            else
            {
                floatDelay = 0f;
            }
        }

        body.AddForce(CustomGravity.GetGravity(body.position), ForceMode.Acceleration);
    }
예제 #9
0
    void FixedUpdate()
    {
        if (floatToSleep)
        {
            if (body.IsSleeping())
            {
                floatDelay = 0f;
                return;
            }

            if (body.velocity.sqrMagnitude < 0.0001f)
            {
                floatDelay += Time.deltaTime;
                if (floatDelay >= 1f)
                {
                    return;
                }
            }
        }

        gravity = CustomGravity.GetGravity(body.position);
        if (submergence > 0f)
        {
            float drag = Mathf.Max(0f, 1 - waterDrag * submergence * Time.deltaTime);
            body.velocity        *= drag;
            body.angularVelocity *= drag;
            body.AddForceAtPosition(gravity * -(buoyancy * submergence), transform.TransformPoint(buoyancyOffset), ForceMode.Acceleration);

            submergence = 0f;
        }

        body.AddForce(gravity, ForceMode.Acceleration);
    }
예제 #10
0
    private void FixedUpdate()
    {
        //upAxis = -Physics.gravity.normalized;
        //5.2.2 自定义重力
        Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis);

        //velocity = body.velocity;
        UpdateState();

        if (InWater)
        {
            velocity *= 1f - waterDrag * submergence * Time.deltaTime;
        }

        AdjustVelocity();

        ////float maxSpeedChange = maxAcceleration * Time.deltaTime;
        //float acceleration = onGround ? maxAcceleration : maxAirAcceleration;
        //float maxSpeedChange = acceleration * Time.deltaTime;

        ////通过当前加速度逼近目标速度
        //velocity.x = Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
        //velocity.z = Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);


        if (desiredJump)
        {
            desiredJump = false;
            Jump(gravity);
        }

        if (Climbing)
        {
            velocity -= contactNormal * (maxClimbAcceleration * 0.9f * Time.deltaTime);
        }
        else if (InWater)
        {
            velocity += gravity * ((1f - buoyancy * submergence) * Time.deltaTime);
        }
        else if (OnGround && velocity.sqrMagnitude < 0.01f)
        {
            velocity += contactNormal * (Vector3.Dot(gravity, contactNormal) * Time.deltaTime);
        }
        else if (desiresClimbing && OnGround)
        {
            velocity += (gravity - contactNormal * (maxClimbAcceleration * 0.9f)) * Time.deltaTime;
        }
        else
        {
            velocity += gravity * Time.deltaTime;
        }

        //控制刚体速度
        body.velocity = velocity;

        //onGround = false;
        ClearState();
    }
    /// <summary>
    /// 定时更新
    /// </summary>
    void FixedUpdate()
    {
        // 1. 检查如果物体受力平衡,则直接返回不要打扰
        if (m_body.IsSleeping())
        {
            m_floatDelay = 0f;
            return;
        }
        // 2. 但是如果前一帧AddForce,则不会进入Sleep()状态,所以首先通过检查速度来决定是否AddForce
        if (m_body.velocity.sqrMagnitude < 0.0001f)
        {
            m_floatDelay += Time.deltaTime;
            // 3. 当物体已经静止超过一段时间后,视为静止
            if (m_floatDelay >= 1f)
            {
                //4. 设定休眠颜色并直接返回
                GetComponent <MeshRenderer>().material.SetColor("_Color", m_sleepColor);
                return;
            }
        }
        // 5. 否则设置当前为非休眠颜色
        GetComponent <MeshRenderer>().material.SetColor("_Color", m_initialColor);
        //todo 得到当前位置的重力以及浸入度,并处理受力
        m_gravity = CustomGravity.GetGravity(m_body.position);
        //todo 计算每个受力点的分力(这里所有的受力都需要被不同的受力点分散)
        float dragFactor     = m_waterDrag * Time.deltaTime / m_buoyancyOffsets.Length;
        float buoyancyFactor = -m_buoyancy / m_buoyancyOffsets.Length;

        for (int i = 0; i < m_buoyancyOffsets.Length; i++)
        {
            if (m_submergence[i] > 0f)
            {
                //todo 从水阻入手计算速度的比例系数(水阻对速度的阻碍程度)
                float drag = Mathf.Max(0f, 1f - m_waterDrag * m_submergence[i] * Time.deltaTime);
                //todo 直接*速度
                m_body.velocity        *= drag;
                m_body.angularVelocity *= drag;
                //todo 在浮力应用点(是全局位置)上应用浮力
                m_body.AddForceAtPosition(
                    m_gravity * (buoyancyFactor * m_submergence[i]),
                    transform.TransformPoint(m_buoyancyOffsets[i]),
                    ForceMode.Acceleration
                    );
                m_submergence[i] = 0f; //? 这个直接设置为0不妥了?
            }
        }
        m_body.AddForce(m_gravity, ForceMode.Acceleration);
    }
    void FixedUpdate()
    {
        if (floatToSleep)
        {
            if (body.IsSleeping())
            {
                floatDelay = 0f;
                return;
            }

            if (body.velocity.sqrMagnitude < 0.0001f)
            {
                floatDelay += Time.deltaTime;
                if (floatDelay >= 1f)
                {
                    return;
                }
            }
            else
            {
                floatDelay = 0f;
            }
        }
        gravity = CustomGravity.GetGravity(body.position);
        float dragFactor     = waterDrag * Time.deltaTime / buoyancyOffsets.Length;
        float buoyancyFactor = -buoyancy / buoyancyOffsets.Length;

        for (int i = 0; i < buoyancyOffsets.Length; i++)
        {
            if (submergence[i] > 0f)
            {
                float drag =
                    Mathf.Max(0f, 1f - dragFactor * submergence[i]);
                body.velocity        *= drag;
                body.angularVelocity *= drag;
                body.AddForceAtPosition(
                    gravity * (buoyancyFactor * submergence[i]),
                    transform.TransformPoint(buoyancyOffsets[i]),
                    ForceMode.Acceleration
                    );
                submergence[i] = 0f;
            }
        }
        body.AddForce(
            gravity, ForceMode.Acceleration
            );
    }
예제 #13
0
    void FixedUpdate()
    {
        Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis);

        UpdateState();
        AdjustVelocity();

        if (desiredJump)
        {
            desiredJump = false;
            Jump(gravity);
        }

        velocity     += gravity * Time.deltaTime;
        body.velocity = velocity;
        ClearState();
    }
예제 #14
0
    private void FixedUpdate()
    {
        gravity = CustomGravity.GetGravity(transform.position, out upAxis);
        UpdateState();
        AdjustRotation();
        AdjustVelocity();
        if (desiredJump)
        {
            desiredJump = false;
            Jump();
        }

        velocity   += gravity * Time.deltaTime;
        rb.velocity = velocity;
        rb.rotation = rotation.normalized;
        ClearState();
    }
예제 #15
0
    void FixedUpdate()
    {
        Vector3 gravity = CustomGravity.GetGravity(body.position, out worldUp);

        UpdateState();
        AdjustVelocity();

        if (desiredJump)
        {
            desiredJump = false;
            Jump(gravity);
        }

        velocity      += gravity * Time.deltaTime;
        model.position = transform.position - (worldUp * 0.3f);

        body.velocity = velocityEnabled ? velocity : Vector3.zero;
        ClearState();
    }
예제 #16
0
    private void FixedUpdate()
    {
        //applies the gravity, changing the up axis
        Vector3 gravity = CustomGravity.GetGravity(_body.position, out _upAxis);

        UpdateState();
        AdjustVelocity();
        AdjustRotationBody();

        //applies the jump if the jump button has been pressed since last FixedUpdate
        if (_desiredJump)
        {
            _desiredJump = false;
            Jump(gravity);
        }

        //applies velocity depending of surface being touched
        if (OnGround && _velocity.sqrMagnitude < 0.01f)
        {
            _velocity += _contactNormal * (Vector3.Dot(gravity, _contactNormal) * Time.deltaTime);
        }
        else
        {
            _velocity += gravity * Time.deltaTime;
        }

        //applies fallMultiplier
        float dotVelocityUp = Vector3.Dot(Vector3.Project(_velocity, _upAxis), _upAxis);

        if (dotVelocityUp < 0 && !OnGround && !OnSteep)
        {
            _velocity += (fallMultiplier - 1) * Time.deltaTime * gravity;
        }
        //applies lowJumpMultiplier
        else if (dotVelocityUp > 0 && !Input.GetButton("Jump") && !OnGround && !OnSteep)
        {
            _velocity += (lowJumpMultiplier - 1) * Time.deltaTime * gravity;
        }

        _body.velocity = _velocity;
        ClearState();
    }
예제 #17
0
    private void FixedUpdate()
    {
        if (body.IsSleeping())
        {
            return;
        }

        if (body.velocity.sqrMagnitude < 0.0001f)
        {
            delayTime += Time.deltaTime;
            if (delayTime >= 1f)
            {
                return;
            }
        }
        else
        {
            delayTime = 0f;
        }

        body.AddForce(CustomGravity.GetGravity(body.position), ForceMode.Acceleration);
    }
    /// <summary>
    /// 按照固定时长被调用,在被推迟之后仍然会调用本应调用的次数
    /// </summary>
    void FixedUpdate()
    {
        // 1. 记录当前重力及反方向
        Vector3 gravity = CustomGravity.GetGravity(m_body.position, out m_upAxis);

        // 2. 更新当前物体物理状态为Update做准备
        UpdateState();

        //todo 如果在水中,则先添加水阻
        if (InWater)
        {
            // 根据浸没率和水阻、时间计算实际水阻对速度的影响
            m_velocity *= 1f - m_waterDrag * m_waterDrag * Time.deltaTime; // 阻力在此添加仍能保证之后允许加速 //? 但是一个绝对数量1和一个时间比例是如何ok的?
        }

        // 3. 更新速度
        AdjustVelocity();
        // 4. 如果之前(Update中)按下了跳跃键
        if (m_desiredJump)
        {
            // 5. 将“妄图跳跃”重置代表已经进行跳跃
            m_desiredJump = false;
            // 6. 进行跳跃
            Jump(gravity);
        }

        // todo 如果攀爬
        if (Climbing)
        {
            //todo 增加一项下压力,用于越过墙壁凸角
            m_velocity -= m_contactNormal * (m_maxClimbAcceleration * 0.9f * Time.deltaTime); // 0.9的原因是免得速度抵消被吸到内叫上
        }
        //todo 如果不攀爬则判断是否在水中(攀爬判定优先)
        else if (InWater)
        {
            //todo 根据浸没率及浮力率计算速度的修正
            m_velocity += gravity * ((1f - m_submergence * m_buoyancy) * Time.deltaTime);
        }
        //todo 如果不在水中则判断是否在地面(水中优先)
        else if (OnGround && m_velocity.sqrMagnitude < 0.01f)
        {
            //todo 增加一项沿斜面法线的下压力,使压力不至于因为这个branch而消失但也不会在很平缓的斜面上缓慢拉动个体
            m_velocity += m_contactNormal * Vector3.Dot(gravity, m_contactNormal) * Time.deltaTime;
        }
        //todo 如果在地面但同时妄图攀爬
        else if (m_desiresClimbing && OnGround)
        {
            //todo 增加一项与墙面凸角相关的压力,用于从Ground进入攀援
            m_velocity += (gravity - m_contactNormal * (m_maxClimbAcceleration * 0.9f)) * Time.deltaTime;
        }
        else
        {
            // 7. 在速度变化上叠加重力影响
            m_velocity += gravity * Time.deltaTime;
        }
        // 8. 更新rigidbody速度
        m_body.velocity = m_velocity;

        // 9. 清空为当前帧其他函数记录的状态
        ClearState();
    }
예제 #19
0
    private void FixedUpdate()
    {
        // _upAxis = -Physics.gravity.normalized;

        Vector3 gravity = CustomGravity.GetGravity(body.position, out upAxis);

        UpdateState();

        if (InWater)
        {
            velocity *= 1f - waterDrag * submergence * Time.deltaTime;
        }

        AdjustVelocity();

        if (desiredJump)
        {
            desiredJump = false;
            Jump(gravity);
        }

        if (Climbing)
        {
            velocity -= contactNormal * (maxClimbAcceleration * 0.8f * Time.fixedDeltaTime);
        }
        else if (InWater)
        {
            velocity += gravity * ((1f - buoyancy * submergence) * Time.deltaTime);
        }
        else if (OnGround && velocity.sqrMagnitude < 0.01f)
        {
            velocity +=
                contactNormal *
                (Vector3.Dot(gravity, contactNormal) * Time.deltaTime);
        }
        else if (desiresClimbing && OnGround)
        {
            velocity += (gravity - contactNormal * (maxClimbAcceleration * 0.8f)) * Time.fixedDeltaTime;
        }
        else
        {
            velocity += gravity * Time.fixedDeltaTime;
        }

        body.velocity = velocity;

        ///////// TODO hacer esto en las BehaviourScripts
        animator.SetBool("TargetLock", lockDir);

        if (body.velocity.sqrMagnitude / maxSpeed > 0.02)
        {
            if (!lockDir)
            {
                maxSpeed       = 10;
                animator.speed = 1;
                animator.SetFloat("VelocityY", body.velocity.magnitude / maxSpeed);
                animator.SetFloat("VelocityX", 0f);
            }
            else
            {
                maxSpeed       = 5;
                animator.speed = 1.5f;
                animator.SetFloat("VelocityY", body.velocity.magnitude / maxSpeed);
                animator.SetFloat("VelocityX", body.velocity.magnitude / maxSpeed);
            }
        }
        else
        {
            animator.SetFloat("VelocityY", 0f);
            animator.SetFloat("VelocityX", 0f);
        }
        //////////////////////////////////////////


        AlignBody();

        ClearState();
    }