Esempio n. 1
0
    // Update is called once per frame
    //Split update into smaller methods later.
    void Update()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
            SceneManager.LoadScene("MainMenu");
        }

        deathModifier += Time.deltaTime / 100;
        aDoubleTap.Update();
        bDoubleTap.Update();
        shield.Update();
        blur.Update();
        speedometer.Update();

        if (dropDown)
        {
            DropDown();
            return;
        }

        //death
        if (Mathf.Abs(rb.velocity.z) < deathTresholdCity * deathModifier && desertMode == false)
        {
        }
        else if (Mathf.Abs(rb.velocity.z) < deathTresholdDesert * deathModifier && desertMode == true)
        {
        }
        else
        {
            deathStart = Time.time;
        }

        if (deathStart + 2 < Time.time && readyToDie)
        {
            GameObject.Find("FadeOut").GetComponent <FadeOut>().StartFadeOut();
        }

        //If behind portal
        if (desertMode && portalDesertSpawner.exitPortal != null)
        {
            if (transform.position.z < portalDesertSpawner.newEntracePortalPosition.z - 1000)
            {
                Destroy(gameObject);
            }
        }

        score += (int)(Mathf.Abs(rb.velocity.z) * Time.deltaTime);

        //Get data from UDPRecieve script for X value of acceleromter on app
        float.TryParse(UDPReceive.lastReceivedUDPPacket, out accelData);
        //Debug.Log(accelData);

        //Accelerate the car initally.
        if (rb.velocity.z > -4000)
        {
            rb.AddRelativeForce(Vector3.forward * -1000 * Time.deltaTime * 50);
        }

        //Accelerate car continuously;
        rb.AddRelativeForce(Vector3.forward * -100 * Time.deltaTime * 50);

        //Calculate bonus horizontal speed.
        bonusHorizontalSpeed = CalculateBonusHorizontalSpeed();

        //Lerp back the boost horizontal speed to 0.
        boostHorizontalSpeed = Mathf.Lerp(boostHorizontalSpeed, 0, Time.deltaTime / 0.1f);

        //Input keyboard code.
        if (accelData > 0.19)
        {
            currentHorizontalSpeed = Mathf.Lerp(currentHorizontalSpeed, (accelData * 10000) * -1 + -bonusHorizontalSpeed + -boostHorizontalSpeed, Time.deltaTime / 0.2f);
        }

        if (accelData < -0.19)
        {
            currentHorizontalSpeed = Mathf.Lerp(currentHorizontalSpeed, (accelData * 10000) * -1 + bonusHorizontalSpeed + boostHorizontalSpeed, Time.deltaTime / 0.2f);
        }

        if (Input.GetKey(KeyCode.D))
        {
            currentHorizontalSpeed = Mathf.Lerp(currentHorizontalSpeed, -maxHorizontalSpeed + -bonusHorizontalSpeed + -boostHorizontalSpeed, Time.deltaTime / 0.2f);
        }

        if (Input.GetKey(KeyCode.A))
        {
            currentHorizontalSpeed = Mathf.Lerp(currentHorizontalSpeed, maxHorizontalSpeed + bonusHorizontalSpeed + boostHorizontalSpeed, Time.deltaTime / 0.2f);
        }

        //If none of the buttons are pressed, lerp to 0 on horizontal speed.
        if (!Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
        {
            currentHorizontalSpeed = Mathf.Lerp(currentHorizontalSpeed, 0, Time.deltaTime / 0.1f);
        }


        //Check lane boundaries
        if (transform.position.x < -3800 && !desertMode)
        {
            transform.position     = new Vector3(-3800, transform.position.y, transform.position.z);
            currentHorizontalSpeed = 0;
        }

        if (transform.position.x > -630 && !desertMode)
        {
            transform.position     = new Vector3(-630, transform.position.y, transform.position.z);
            currentHorizontalSpeed = 0;
        }

        //tilt
        //Mb turn more the more ur speed is
        Vector3 rotation = transform.localRotation.eulerAngles;
        float   maxYTilt = Helper.Remap(rb.velocity.z, minimumSpeed, -16000, 20, 40);

        maxYTilt   = Mathf.Clamp(maxYTilt, -40, 40);
        rotation.y = Helper.Remap(currentHorizontalSpeed, (-maxHorizontalSpeed + -bonusHorizontalSpeed + -boostHorizontalSpeed) * -1, -maxHorizontalSpeed + -bonusHorizontalSpeed + -boostHorizontalSpeed, -maxYTilt, maxYTilt);
        rotation.z = Helper.Remap(currentHorizontalSpeed, (-maxHorizontalSpeed + -bonusHorizontalSpeed + -boostHorizontalSpeed) * -1, -maxHorizontalSpeed + -bonusHorizontalSpeed + -boostHorizontalSpeed, -7, 7);
        transform.localRotation = Quaternion.Euler(rotation);

        rb.velocity = new Vector3(currentHorizontalSpeed, rb.velocity.y, rb.velocity.z);

        //Set speed to minimum if less than minimum.
        if (rb.velocity.z > minimumSpeed)   //Remember, the minimum speed is negative.
        {
            rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, minimumSpeed);
        }
        //Debug.Log(currentHorizontalSpeed);
        //Wheel smoke
        if (Mathf.Abs(currentHorizontalSpeed) > 1000)
        {
            foreach (GameObject wheel in wheelSmoke)
            {
                wheel.GetComponent <ParticleSystem>().Play();
            }
        }
        else
        {
            foreach (GameObject wheel in wheelSmoke)
            {
                wheel.GetComponent <ParticleSystem>().Stop();
            }
        }

        _UpdateText();
    }