コード例 #1
0
    // Update is called once per frame
    void Update()
    {
#if UNITY_ANDROID || UNITY_IPHONE
        float v = JoystickControler.GetVerticalInput();
        float h = JoystickControler.GetHorizontalInput();
#endif
#if UNITY_STANDALONE_WIN
        float v = -Input.GetAxis("Vertical");
        float h = Input.GetAxis("Horizontal");
#endif

        //限制车的最大速度,调整阻力可能不是最好的做法。但它很简单,而且不会干扰物理系统的运行。
        m_Rigidbody.drag = m_Rigidbody.velocity.magnitude / 250;
        //通过两个轮子的平均rpm,计算引擎rpm,然后切换档位
        EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm) / 2 * GearRatio[CurrentGear];
        ShiftGears();

        //设置换档的声音
        m_Audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1.0f;
        if (m_Audio.pitch > 2.0)
        {
            m_Audio.pitch = 2.0f;
            if (!m_Audio.isPlaying)
            {
                m_Audio.Play();
            }
        }

        //最后设置轮子转动力矩。引擎力矩除以当前档位,乘以用户输入值。
        //轮子力矩提供一个汽车前进的力。轮子的转动又会提高档位。


        BackLeftWheel.motorTorque  = EngineTorgue / GearRatio[CurrentGear] * v;
        BackRightWheel.motorTorque = EngineTorgue / GearRatio[CurrentGear] * v;

        int a = (int)(GearRatio[CurrentGear] * FrontLeftWheel.rpm * 4);
        BackLeftWheel.transform.localEulerAngles  += new Vector3(a, 0, 0);
        BackRightWheel.transform.localEulerAngles += new Vector3(a, 0, 0);

        //转动角度是任意数乘以用户输入值

        FrontLeftWheel.steerAngle  = wheelSpeed * h;
        FrontRightWheel.steerAngle = wheelSpeed * h;

        int b = (int)(GearRatio[CurrentGear] * FrontLeftWheel.rpm * 4);
        FrontLeftWheel.transform.localEulerAngles  += new Vector3(b, 0, 0);
        FrontRightWheel.transform.localEulerAngles += new Vector3(b, 0, 0);
    }
コード例 #2
0
ファイル: GameManager.cs プロジェクト: TIDEXIE/DC
    IEnumerator StopLight()
    {
        yield return(new WaitForSeconds(1.1f));

        //垂直
#if UNITY_ANDROID || UNITY_IPHONE
        float v = -JoystickControler.GetVerticalInput();
#endif
#if UNITY_STANDALONE_WIN
        float v = -Input.GetAxis("Vertical");
#endif
        if (v < 0)
        {
            stopLight.SetActive(true);
        }
        else
        {
            stopLight.SetActive(false);
        }
        yield return(StopLight());
    }