Beispiel #1
0
    // while temple drift is active, adjust velocity of the periscope
    public void TempleDrift()
    {
        if (templeDriftActive)
        {
            int templeBearing = Managers.Landmarks.tier3Landmarks[Managers.Landmarks.tier3Landmarks.Count - 1].bearing;
            int distanceToTempleCW = templeBearing - (int)bearing;
            if (distanceToTempleCW < 0) { distanceToTempleCW = 360 + distanceToTempleCW; }

            if (distanceToTempleCW <= 180)
            {
                if (rotationVelocity < maxVelocity) {
                    rotationVelocity += Time.deltaTime * acc * 0.5f;
                    if (templeDriftDirection == DriftDirection.None)
                    {
                        templeDriftDirection = DriftDirection.Right;
                        Messenger.Broadcast(GameEvent.PERISCOPE_TEMPLEDRIFTON);
                    } else if (templeDriftDirection == DriftDirection.Left)
                    {
                        templeDriftDirection = DriftDirection.Right;
                        Messenger.Broadcast(GameEvent.PERISCOPE_TEMPLEDRIFTDIRECTIONCHANGED);
                    }
                }
            } else if (distanceToTempleCW > 180)
            {
                if (rotationVelocity > -maxVelocity) {
                    rotationVelocity -= Time.deltaTime * acc * 0.5f;
                    if (templeDriftDirection == DriftDirection.None)
                    {
                        templeDriftDirection = DriftDirection.Left;
                        Messenger.Broadcast(GameEvent.PERISCOPE_TEMPLEDRIFTON);
                    } else if (templeDriftDirection == DriftDirection.Right)
                    {
                        templeDriftDirection = DriftDirection.Left;
                        Messenger.Broadcast(GameEvent.PERISCOPE_TEMPLEDRIFTDIRECTIONCHANGED);
                    }
                }
            }

            rotationVelocity = Mathf.Clamp(rotationVelocity, -maxVelocity, maxVelocity);
        }
        else
        {
            templeDriftDirection = DriftDirection.None;
            Messenger.Broadcast(GameEvent.PERISCOPE_TEMPLEDRIFTOFF);
        }
    }
    void FixedUpdate()
    {
        // Capture inputs (x = left/right, y = up/down)
        Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        if (!canControl)
        {
            input = Vector2.zero;
        }

        float amtForward = Vector3.Dot(rigidbody2D.velocity, transform.up);

        // Initiate drifting if turning into a direction and press Drift button and moving forward
        if (Input.GetButtonDown("Drift") && input.x != 0 && !isDrifting && amtForward > 0)
        {
            isDrifting = true;
            driftDir   = input.x < 0 ? DriftDirection.LEFT : DriftDirection.RIGHT;

            sprite.gameObject.transform.localRotation = Quaternion.identity;

            transform.Rotate(0, 0, -input.x * driftRotationAngle);
            sprite.gameObject.transform.Rotate(0, 0, input.x * driftRotationAngle);

            // Adds trail when drifting
            foreach (TrailRenderer trail in trails)
            {
                trail.emitting = true;
            }

            if (disableHurtWhileDrifting)
            {
                hurtColliderTop.enabled    = false;
                hurtColliderBottom.enabled = false;
            }

            PlayDriftingSound();
        }
        if (!isDrifting)
        {
            //Reset sprial meter
            gameManager.meterPercent -= (gameManager.meterRate / 2) * Time.deltaTime;

            //Stop emitter
            sparks.Stop();

            // Accelerate and clamp speed normally if not drifting

            // Forward/backward
            rigidbody2D.AddForce(transform.up * input.y * accelerationSpeed);

            float turningSpeed = Mathf.Lerp(turningSpeedSpeedClamp.x, turningSpeedSpeedClamp.y,
                                            rigidbody2D.velocity.magnitude / (accelerationSpeed / 2f));

            // Rotate with speed and movement magnitude (not moving = no rotate)
            transform.Rotate(0, 0,
                             -input.x * (input.y == 0 ? 1 : input.y) * turningSpeed * (
                                 movementSpeedForMaxTurnSpeed == 0 ? 1 : Mathf.Clamp(
                                     rigidbody2D.velocity.magnitude,
                                     -movementSpeedForMaxTurnSpeed,
                                     movementSpeedForMaxTurnSpeed) / movementSpeedForMaxTurnSpeed)
                             );

            rigidbody2D.velocity = Vector3.ClampMagnitude(rigidbody2D.velocity, maxDrivingSpeed);

            // Rotate sprite back to original if we were out of place
            sprite.gameObject.transform.localRotation = Quaternion.Lerp(
                sprite.gameObject.transform.localRotation, Quaternion.identity,
                driftRotationSpriteRotFactor);
            //Turns off trail when not drifting
            foreach (TrailRenderer trail in trails)
            {
                trail.emitting = false;
            }
        }
        else
        {
            //Increase spiral meter
            gameManager.meterPercent += gameManager.meterRate * Time.deltaTime;

            //Start particles
            sparks.Play();

            // Kill drift if we let go of Drift key
            if (!Input.GetButton("Drift"))
            {
                StopDrifting();
            }

            // Sprite during drift rotates a bit further than direction
            sprite.gameObject.transform.localRotation = Quaternion.Lerp(
                sprite.gameObject.transform.localRotation,
                Quaternion.Euler(
                    0, 0,
                    (driftDir == DriftDirection.LEFT ? 1 : -1) * driftRotationAngle * 0.5f),
                driftRotationSpriteRotFactor
                );

            // Force velocity to be constant and direct car into the drift direction
            float turnAmt = (driftDir == DriftDirection.LEFT ? 1 : -1) * driftAutoTurnSpeed;
            turnAmt += -input.x * driftTurningInfluence;

            Vector3 vel = rigidbody2D.velocity;

            vel = Vec2Rotate(vel.normalized,
                             turnAmt);
            vel.Scale(Vector2.one * driftSpeed);

            rigidbody2D.velocity = vel;

            transform.Rotate(0, 0, turnAmt);
        }

        rigidbody2D.drag = Mathf.Lerp(linearDragSpeedClamp.x, linearDragSpeedClamp.y,
                                      rigidbody2D.velocity.magnitude / accelerationSpeed);
    }