Beispiel #1
0
    protected void FixedUpdate()
    {
        Vector3 groundVelocity = GetGroundVelocity();

        if (ArmCatched)
        {
            var force = Vector2.ClampMagnitude(groundVelocity.XY() + desiredVelocity - body.velocity.XY(), Settings.maxAcceleration);
            body.AddForce(force, ForceMode.VelocityChange);
            legUpDir = Vector3.up;
        }
        else
        {
            Quaternion legRot = GetLegRotation();
            var        xAxis  = legRot * Vector3.right;
            legUpDir = legRot * Vector3.up;
            var xVel  = Vector3.Dot(xAxis, body.velocity);
            var xGVel = Vector3.Dot(xAxis, groundVelocity);
            var force = Mathf.Clamp(xGVel + desiredVelocity.x - xVel, -Settings.maxAcceleration, Settings.maxAcceleration);
            body.AddForce(xAxis * force, ForceMode.VelocityChange);
        }

        body.AddForce(GetArmsCatchForce(), ForceMode.VelocityChange);

        bool jumpStarted = false;

        if (desiredJump)
        {
            if (LegOnGround)
            {
                var jumpForce = Mathf.Sqrt(-2f * Physics.gravity.y * Settings.jumpHeight) - body.velocity.y;
                body.AddForce(0, jumpForce, 0, ForceMode.VelocityChange);
                SendOppositeForceToLegs(new Vector3(0, jumpForce, 0));
                desiredJump = false;
                jumpStarted = true;
                RemoveAllLegs();
            }
            else
            {
                //body.AddForce(0, -maxAcceleration, 0, ForceMode.VelocityChange);
            }
        }
        else
        {
            Vector3 legF = legUpDir * Mathf.Max(GetLegForce(0), GetLegForce(1));
            body.AddForce(legF, ForceMode.VelocityChange);
            SendOppositeForceToLegs(legF);
            float legSF = GetLegSideForce();
            body.AddForce(legSF, 0, 0, ForceMode.VelocityChange);
        }

        if (!jumpStarted)
        {
            body.AddForce(GetDrag());
        }

        if (desiredZMove != 0)
        {
            var p = transform.position;
            if (placeable.CanZMove(p.z + desiredZMove))
            {
                p.z += desiredZMove;
                transform.position = p;
                var ho = GetHoldObject();
                if (ho != null)
                {
                    TryCorrectZPos(ho);
                }
                RemoveAllCatchedLegsArms();
                ActivateSomeLegsArms();
                RecatchHold();
            }
            desiredZMove = 0;
        }

        ApplyHoldForce();
    }