Ejemplo n.º 1
0
    public CritterStatePacket UpdateTick(CritterInputPacket packet)
    {
        bool walking = false;

        rb.velocity += Physics.gravity * config.gravityMult * Time.fixedDeltaTime;

        var flatVel = rb.velocity.WithY(0);
        var fwd     = Head.transform.forward.WithY(0).normalized;
        var right   = Head.transform.right.WithY(0).normalized;

        var desiredVel = Vector3.zero;

        if (packet.forward)
        {
            desiredVel += fwd;
            walking     = true;
        }
        else if (packet.backward)
        {
            desiredVel -= fwd;
            walking     = true;
        }

        if (packet.rightward)
        {
            desiredVel += right;
            walking     = true;
        }
        else if (packet.leftward)
        {
            desiredVel -= right;
            walking     = true;
        }

        desiredVel = desiredVel.normalized * config.maxSpeed;

        if (walking)
        {
            flatVel = Vector3.Lerp(flatVel, desiredVel, 1f / config.accelLag);
        }
        else
        {
            flatVel *= config.autoDecel;
        }

        if (flatVel.sqrMagnitude > config.maxSpeed * config.maxSpeed)
        {
            flatVel = flatVel.normalized * config.maxSpeed;
        }

        rb.velocity = flatVel + Vector3.up * rb.velocity.y;

        if (packet.jump && grounded > 0)
        {
            grounded    = 0;
            rb.velocity = rb.velocity.WithY(config.jumpVelY);
            audioManager.PlayJumpSound();
        }

        var newPosition = rb.position + rb.velocity * Time.fixedDeltaTime;

        if (grounded > 0)
        {
            grounded--;
        }

        var hits    = Physics.SphereCastAll(newPosition, suspensionRadius, Vector3.down, config.extraHeight, LayerMask.GetMask("Default"));
        var hitInfo = findImportantHit(hits);

        if (hitInfo.HasValue)
        {
            var fixedCastPos = newPosition + Vector3.down * hitInfo.Value.distance;
            newPosition  = fixedCastPos + Vector3.up * config.extraHeight;
            rb.velocity  = rb.velocity.WithY(0);
            rb.velocity += (newPosition - rb.position) * 10f;

            grounded = 2;
        }

        launcher.Update(packet.shoot, critter.transform.position, packet.headOrientation * Vector3.forward);

        return(new CritterStatePacket {
            position = newPosition,
            velocity = rb.velocity,
            headOrientation = Head.transform.rotation
        });
    }
Ejemplo n.º 2
0
 void TryPoop(CritterInputPacket packet)
 {
     launcher.Update(packet.shoot, critter.transform.position, -critter.transform.up);
 }