Ejemplo n.º 1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        HandleDrifting(h, v);

        HandleBoost();

        HandleSlam(ref h, ref v);

        if (Input.GetAxisRaw("SLAM") != 0)
        {
            Slam(h, v);
        }

        // Turning
        if (h != 0)
        {
            if (!drifting)
            {
                if (!stoppedDrifting)
                {
                    wheelsForward = Vector3.Lerp(wheelsForward, transform.right * h, Time.deltaTime * wheelTurnSpeed);
                    wheelsForward = Vector3.ProjectOnPlane(wheelsForward, transform.up);
                }
                else
                {
                    wheelsForward = Vector3.Lerp(wheelsForward, transform.right * h, Time.deltaTime * (wheelTurnSpeed + stoppedDriftingTurnSpeedBoost));
                    wheelsForward = Vector3.ProjectOnPlane(wheelsForward, transform.up);
                }
            }
            else
            {
                wheelsForward = Vector3.Lerp(wheelsForward, transform.right * (h + driftTurn * -Mathf.Sign(h)), Time.deltaTime * wheelTurnSpeed);
                wheelsForward = Vector3.ProjectOnPlane(wheelsForward, transform.up);
            }
        }

        // Tilt
        RaycastHit groundHit, rampHit;

        if (Physics.Raycast(transform.position, -Vector3.up, out groundHit))
        {
            tiltH = Mathf.Lerp(tiltH, h, Time.deltaTime * (drifting ? driftTiltSpeed : tiltSpeed));
            float tiltAmt = drifting ? driftTilt : turnTilt;
            //carBody.localRotation = Quaternion.identity;
            //carBody.up = groundHit.normal;


            int sign = 1;
            if (Physics.Raycast(transform.position + transform.forward * 0.1f, -Vector3.up, out rampHit) && rampHit.point.y > groundHit.point.y)
            {
                sign = -1;
            }

            carBody.localRotation = Quaternion.Euler(0, 0, Mathf.Abs(tiltH) * Vector3.Dot(wheelsForward, transform.right) * tiltAmt);

            transform.localRotation = Quaternion.Lerp(
                transform.localRotation,
                Quaternion.Euler(sign * Vector3.Angle(Vector3.up, groundHit.normal), transform.localRotation.eulerAngles.y, 0),
                Time.deltaTime * normalRotationSpeed);
        }

        //if (Mathf.Sign(tiltH) == Mathf.Sign(Vector3.Dot(wheelsForward, transform.right)))
        //    carBody.localRotation = q;
        //else
        //{
        //    carBody.localRotation = Quaternion.Lerp(carBody.localRotation, Quaternion.identity, Time.deltaTime);
        //}



        if (inAir) // was in air last update
        {
            rbody.drag        = groundDrag.x;
            rbody.angularDrag = groundDrag.y;
            inAir             = false;
        }

        // Ground check
        Debug.DrawLine(transform.position, transform.position + Vector3.down * groundCheckHeight, Color.red);
        RaycastHit hit;

        if (Physics.Linecast(transform.position, transform.position - Vector3.up * groundCheckHeight, out hit))
        {
            rbody.drag        = groundDrag.x + hit.collider.material.dynamicFriction;
            rbody.angularDrag = groundDrag.y + hit.collider.material.staticFriction;
        }
        else
        {
            rbody.drag        = airDrag.x;
            rbody.angularDrag = airDrag.y;
            inAir             = true;

            rbody.AddForce(Vector3.down * fallForce, ForceMode.Acceleration);

            wheelTurner.Turn(wheelsForward);
        }

        // Acceleration
        if (v != 0)
        {
            float s = v > 0 ? speed + currBoost : reverseSpeed;

            if (!drifting)
            {
                rbody.AddForce((wheelsForward * frontWheelDriveAmt + transform.forward * backWheelDriveAmt).normalized * v * s, ForceMode.Acceleration);

                // Turning Torque
                if (h != 0)
                {
                    float bonus = stoppedDrifting ? stoppedDriftingTurnTorqueBoost : 0;
                    rbody.AddTorque(transform.up * h * (turnSpeed + bonus) * Mathf.Sign(v), ForceMode.Acceleration);
                }
            }
            else
            {
                rbody.AddForce((wheelsForward * frontWheelDriveAmt + transform.forward * backWheelDriveAmt).normalized * v * s * driftSpeedMod, ForceMode.Acceleration);

                // Drift Turning Torque
                if (Mathf.Sign(h) == driftDirection)
                {
                    rbody.AddTorque(transform.up * (h * driftTurnTorque) * turnSpeed * Mathf.Sign(v), ForceMode.Acceleration);
                }
                else
                {
                    rbody.AddTorque(transform.up * (h * driftAgainstTurnTorque) * turnSpeed * Mathf.Sign(v), ForceMode.Acceleration);
                }
            }


            wheelsForward = Vector3.Lerp(wheelsForward, transform.forward, Time.deltaTime * wheelCorrectionSpeed);
            wheelsForward = Vector3.ProjectOnPlane(wheelsForward, Vector3.up);

            float dot = Vector3.Dot(wheelsForward, transform.right);
            rbody.AddTorque(transform.up * (1 - dot) * (correctionSpeed * Mathf.Sign(dot)), ForceMode.Acceleration);
        }

        Debug.DrawLine(transform.position, transform.position + wheelsForward * 3, Color.blue);

        if (v >= 0)
        {
            wheelTurner.Turn(wheelsForward);
        }
        else
        {
            wheelTurner.Turn(Vector3.Cross(wheelsForward, transform.up));             // Reverse
        }
        // Jump
        //if (Input.GetKeyDown(KeyCode.Space))
        //	rbody.AddForce(Vector3.up * 10, ForceMode.VelocityChange);
    }
Ejemplo n.º 2
0
    void CarFixedUpdate(float h, float v)
    {
        //HandleDrifting(h, v);
        //HandleBoost();
        //HandleSlam(ref h, ref v);
        //if (Input.GetAxisRaw("Interact") != 0)
        //    Slam(h, v);

        // Turning
        if (h != 0)
        {
            wheelsForward = Vector3.Lerp(wheelsForward, transform.right * h, Time.deltaTime * wheelTurnSpeed);
            wheelsForward = Vector3.ProjectOnPlane(wheelsForward, transform.up);
        }

        // Tilt

        RaycastHit groundHit, rampHit;

        if (Physics.Raycast(transform.position, -Vector3.up, out groundHit))
        {
            tiltH = Mathf.Lerp(tiltH, h, Time.deltaTime * tiltSpeed);
            float tiltAmt = turnTilt;

            int sign = 1;
            if (Physics.Raycast(transform.position + transform.forward * 0.1f, -Vector3.up, out rampHit) && rampHit.point.y > groundHit.point.y)
            {
                sign = -1;
            }

            carBody.localRotation = Quaternion.Euler(0, 0, Mathf.Abs(tiltH) * Vector3.Dot(wheelsForward, transform.right) * tiltAmt);

            transform.localRotation = Quaternion.Lerp(
                transform.localRotation,
                Quaternion.Euler(sign * Vector3.Angle(Vector3.up, groundHit.normal), transform.localRotation.eulerAngles.y, 0),
                Time.deltaTime * normalRotationSpeed);
        }

        //tiltH = Mathf.Lerp(tiltH, h, Time.deltaTime * tiltSpeed);
        //float tiltAmt = turnTilt;
        //carBody.localRotation = Quaternion.Euler(0, 0, Mathf.Abs(tiltH) * Vector3.Dot(wheelsForward, transform.right) * tiltAmt);

        if (inAir) // was in air last update
        {
            rbody.drag        = groundDrag.x;
            rbody.angularDrag = groundDrag.y;
            inAir             = false;
        }

        // Ground check
        Debug.DrawLine(transform.position, transform.position + Vector3.down * groundCheckHeight, Color.red);
        RaycastHit hit;

        if (Physics.Linecast(transform.position, transform.position - Vector3.up * groundCheckHeight, out hit))
        {
            rbody.drag        = groundDrag.x + hit.collider.material.dynamicFriction;
            rbody.angularDrag = groundDrag.y + hit.collider.material.staticFriction;
        }
        else
        {
            rbody.drag        = airDrag.x;
            rbody.angularDrag = airDrag.y;
            inAir             = true;

            rbody.AddForce(Vector3.down * fallForce, ForceMode.Acceleration);

            wheelTurner.Turn(wheelsForward);
        }

        // Acceleration
        if (v != 0)
        {
            float s = v > 0 ? speed + (Mathf.Abs(h) * turnSpeedBoost) : reverseSpeed;

            rbody.AddForce((wheelsForward * frontWheelDriveAmt + transform.forward * backWheelDriveAmt).normalized * v * s, ForceMode.Acceleration);

            // Turning Torque
            if (h != 0)
            {
                rbody.AddTorque(transform.up * h * turnSpeed * Mathf.Sign(v), ForceMode.Acceleration);
            }

            wheelsForward = Vector3.Lerp(wheelsForward, transform.forward, Time.deltaTime * wheelCorrectionSpeed);
            wheelsForward = Vector3.ProjectOnPlane(wheelsForward, Vector3.up);

            float dot = Vector3.Dot(wheelsForward, transform.right);
            rbody.AddTorque(transform.up * (1 - dot) * (correctionSpeed * Mathf.Sign(dot)), ForceMode.Acceleration);
        }

        Debug.DrawLine(transform.position, transform.position + wheelsForward * 3, Color.blue);

        if (v >= 0)
        {
            wheelTurner.Turn(wheelsForward);
        }
        else
        {
            wheelTurner.Turn(Vector3.Cross(wheelsForward, transform.up)); // Reverse
        }
    }