Example #1
0
    private void Update()
    {
        RotateShip();
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            cursorVisible = !cursorVisible;
            SetCursor();
        }
        float currentSpeed = shipSpeed;


        if (Input.GetKey(KeyCode.LeftShift))
        {
            currentSpeed *= shipBoostMultiplier;
            boosterEffect.Play();
            rayCam.fieldOfView = Mathf.Lerp(rayCam.fieldOfView, boostFov, Time.deltaTime);
        }
        else
        {
            boosterEffect.Stop();
            rayCam.fieldOfView = Mathf.Lerp(rayCam.fieldOfView, baseFOV, Time.deltaTime);
        }
        Vector3 dir           = transform.TransformDirection(Vector3.forward);
        float   forwardMotion = Input.GetAxisRaw("Vertical");

        //variables set when hyperspeeding
        if (GlobalVariables.isHyperspeed)
        {
            forwardMotion      = 1f;
            currentSpeed       = hyperdriveSpeed;
            rayCam.fieldOfView = Mathf.Lerp(rayCam.fieldOfView, hyperdriveFov, Time.deltaTime);
        }
        currentSpeed *= forwardMotion;
        GlobalVariables.playerSpeed = currentSpeed;
        if (GlobalVariables.CurrentPlanet == null)
        {
            currentSpeed *= Time.deltaTime;
        }
        Vector3d posDelta = new Vector3d(dir) * currentSpeed;

        if (GlobalVariables.CurrentPlanet == null)
        {
            GlobalVariables.AddPlayerWorldPos(posDelta);
        }
        else
        {
            Vector3 gravityUp = (transform.position - GlobalVariables.CurrentPlanet.transform.position).normalized;
            rigidbody.velocity = (Vector3)posDelta;
            if (rotationContrained)
            {
                Quaternion targetRot = Quaternion.FromToRotation(transform.up, gravityUp) * transform.rotation;
                transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotationSmoothness * Time.deltaTime);
                float mouseY = Mathf.Abs(Input.GetAxis("Mouse Y"));
                if (mouseY > 0.8f)
                {
                    rotationContrained  = false;
                    constraintThreshold = 0;
                }
            }
            else
            {
                if (Vector3.Dot(transform.up, gravityUp) > 0.95)
                {
                    constraintThreshold += Time.deltaTime;
                }
                else
                {
                    constraintThreshold = 0;
                }
                if (constraintThreshold >= 2)
                {
                    rotationContrained  = true;
                    constraintThreshold = 0;
                }
            }

            GlobalVariables.AddPlayerWorldPos(transform.position);
        }
        if (GlobalVariables.isHyperspeed)
        {
            if (hyperSpeedTarget.transform.position.magnitude < hyperSpeedTarget.AtmosphereLevel)
            {
                StopHyperSpeed();
            }
        }
        cameraRay = new Ray(rayCam.transform.position, rayCam.transform.forward);

        if (Physics.Raycast(cameraRay, out hit, rayDistance, planetLayer))
        {
            Planet planet = hit.transform.parent.GetComponent <Planet>();
            UiHandler.instance.ShowPlanetPanel(planet);
            if (Input.GetKeyDown(KeyCode.F) && GlobalVariables.CurrentPlanet == null)
            {
                if (!GlobalVariables.isHyperspeed)
                {
                    GlobalVariables.isHyperspeed = true;
                    hyperSpeedTarget             = planet;

                    hyperDriveEffect.Play();
                    CameraShake.instance.Shake(x, y, hyperdriveShakeMagnitude, hyperdriveSmoothness);
                }
                else
                {
                    StopHyperSpeed();
                }
            }
        }
        else
        {
            UiHandler.instance.HidePlanetPanel();
        }
    }