Пример #1
0
    public void LateUpdate()
    {
        if (wheelSkidState)
        {
            Vector3 skidPoint = gameObject.transform.position;
            skidPoint.y = 0.01f;

            if (intensity < maxIdensity)
            {
                float dt = Time.deltaTime / skid_time;
                intensity = intensity + dt;
            }

            lastSkid = skidmarksController.AddSkidMark(skidPoint, Vector3.up, intensity, lastSkid);
        }
        else
        {
            if (intensity > fadeIdensity)
            {
                Vector3 skidPoint = gameObject.transform.position;
                skidPoint.y = 0.01f;

                float dt = Time.deltaTime / skid_time;
                intensity = intensity - dt;

                lastSkid = skidmarksController.AddSkidMark(skidPoint, Vector3.up, intensity, lastSkid);
            }
            else
            {
                lastSkid = -1;
            }
        }
    }
Пример #2
0
    protected void Update()
    {
        if (rb == null)
        {
            return;
        }

        if (wheelCollider.GetGroundHit(out wheelHitInfo))
        {
            // Check sideways speed
            // Gives velocity with +Z being our forward axis
            Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
            float   skidSpeed     = Mathf.Abs(localVelocity.x);
            float   intensity     = -1;
            Vector3 skidPoint;

            float skidSpeedZ = Mathf.Abs(localVelocity.z);

            if (Mathf.Abs(wheelHitInfo.forwardSlip) >= SKID_FORWARD_FX_SPEED)
            {
                //Debug.Log("forwardSlip" + Mathf.Abs(wheelHitInfo.forwardSlip));

                intensity = Mathf.Clamp01(skidSpeedZ / MAX_FORWARD_SKID_INTENSITY);

                skidPoint = wheelHitInfo.point + (rb.velocity * (Time.fixedDeltaTime));
                lastSkid  = skidmarksController.AddSkidMark(skidPoint, wheelHitInfo.normal, intensity, lastSkid);
            }

            /*else if (forceBrake && skidSpeedZ >= 0.1f)
             * {
             *  Debug.Log("skidSpeed" + skidSpeedZ);
             *  intensity = Mathf.Clamp01(skidSpeedZ / 42.00f);
             *
             *  skidPoint = wheelHitInfo.point + (rb.velocity * (Time.fixedDeltaTime));
             *  lastSkid = skidmarksController.AddSkidMark(skidPoint, wheelHitInfo.normal, intensity, lastSkid);
             * }*/
            else if (skidSpeed >= SKID_SLIP_FX_SPEED || Mathf.Abs(wheelHitInfo.forwardSlip) >= 0.5f)
            {
                // MAX_SKID_INTENSITY as a constant, m/s where skids are at full intensity
                intensity = Mathf.Clamp01(skidSpeed / MAX_SKID_INTENSITY);
                //Debug.Log("intensity" + intensity);
                skidPoint = wheelHitInfo.point + (rb.velocity * (Time.fixedDeltaTime));
                lastSkid  = skidmarksController.AddSkidMark(skidPoint, wheelHitInfo.normal, intensity, lastSkid);
            }
            else
            {
                lastSkid = -1;
            }
        }
    }
Пример #3
0
    void FixedUpdate()                         //This has to be in fixed update or it wont get time to make skidmesh fully.
    {
        WheelHit GroundHit;                    //variable to store hit data

        wheel_col.GetGroundHit(out GroundHit); //store hit data into GroundHit
        var wheelSlipAmount = Mathf.Abs(GroundHit.sidewaysSlip);

        if (wheelSlipAmount > startSlipValue) //if sideways slip is more than desired value
        {
            /*Calculate skid point:
             * Since the body moves very fast, the skidmarks would appear away from the wheels because by the time the
             * skidmarks are made the body would have moved forward. So we multiply the rigidbody's velocity vector x 2
             * to get the correct position
             */

            Vector3 skidPoint = GroundHit.point + 2 * (skidCaller.GetComponent <Rigidbody>().velocity) * Time.deltaTime;

            //Add skidmark at the point using AddSkidMark function of the Skidmarks object
            //Syntax: AddSkidMark(Point, Normal, Intensity(max value 1), Last Skidmark index);
            lastSkidmark = skidmarks.AddSkidMark(skidPoint, GroundHit.normal, wheelSlipAmount / 2.0f, lastSkidmark);
        }
        else
        {
            //stop making skidmarks
            lastSkidmark = -1;
        }
    }
Пример #4
0
    private void UpdateSkidmarks(Wheel wheel, bool isTouchingGround, WheelHit hit)
    {
        const float minSkidSpeed = 3.0f;

        // Check if the wheel is on the ground and skidding enough to draw a skidmark.
        float slipSpeed = new Vector2(hit.forwardSlip, hit.sidewaysSlip).magnitude;
        bool  isWheelCurrentlySlipping = isTouchingGround && slipSpeed > minSkidSpeed;

        isSkidding |= isWheelCurrentlySlipping;

        if (isWheelCurrentlySlipping)
        {
            // Draw the mark slightly above the ground to avoid clipping with the
            // road surface at long draw distances. Increase the intensity of the
            // texture with the slip speed.
            wheel.lastSkidmarkIndex = skidmarks.AddSkidMark(
                hit.point + rigidbody.velocity * Time.fixedDeltaTime + 0.1f * hit.normal,
                hit.normal, Mathf.Clamp01((slipSpeed - minSkidSpeed) / 10),
                wheel.lastSkidmarkIndex);
            // TODO: Add smoke effect.
        }
        else
        {
            wheel.lastSkidmarkIndex = -1;
        }
    }
Пример #5
0
    // Update is called once per frame
    void LateUpdate()
    {
        float intensity = playerCar.SlideSlipAmount();

        if (intensity < 0)
        {
            intensity = -intensity;
        }

        if (intensity > 0.2f)
        {
            lastSkidId = skidMarkController.AddSkidMark(transform.position, transform.up, intensity * intensityModifer, lastSkidId);
            if (particleSystem != null && !particleSystem.isPlaying)
            {
                particleSystem.Play();
            }
        }
        else
        {
            lastSkidId = -1;
            if (particleSystem != null && particleSystem.isPlaying)
            {
                particleSystem.Stop();
            }
        }
    }
Пример #6
0
 void Update()
 {
     if (Input.GetMouseButton(0))
     {
         lastSkid = skidmarksController.AddSkidMark(transform.position, transform.position, 1, lastSkid);
         //tr.enabled = true;
         if (audioData)
         {
             audioData.Stop();
         }
     }
     if (Input.GetMouseButtonUp(0))
     {
         lastSkid = -1;
         // StartCoroutine(stopTrail());
         if (audioData)
         {
             if (!audioData.isPlaying)
             {
                 audioData.Play(0);
             }
         }
     }
     if (Input.GetMouseButton(1))
     {
         lastSkid = skidmarksController.AddSkidMark(transform.position, transform.position, 1, lastSkid);
         //tr.enabled = true;
         if (audioData)
         {
             audioData.Stop();
         }
     }
     if (Input.GetMouseButtonUp(1))
     {
         lastSkid = -1;
         //StartCoroutine(stopTrail());
         if (audioData)
         {
             if (!audioData.isPlaying)
             {
                 audioData.Play(0);
             }
         }
     }
 }
Пример #7
0
    protected void LateUpdate()
    {
        if (rb != null && wheelCollider.GetGroundHit(out wheelHitInfo))
        {
            // Check sideways speed

            // Gives velocity with +z being the car's forward axis
            Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
            float   skidTotal     = Mathf.Abs(localVelocity.x);

            // Check wheel spin as well

            float wheelAngularVelocity = wheelCollider.radius * ((2 * Mathf.PI * wheelCollider.rpm) / 60);
            float carForwardVel        = Vector3.Dot(rb.velocity, transform.forward);
            float wheelSpin            = Mathf.Abs(carForwardVel - wheelAngularVelocity) * WHEEL_SLIP_MULTIPLIER;

            // NOTE: This extra line should not be needed and you can take it out if you have decent wheel physics
            // The built-in Unity demo car is actually skidding its wheels the ENTIRE time you're accelerating,
            // so this fades out the wheelspin-based skid as speed increases to make it look almost OK
            wheelSpin = Mathf.Max(0, wheelSpin * (10 - carForwardVel));

            skidTotal += wheelSpin;


            WheelHit wheelHit;
            if (wheelCollider.GetGroundHit(out wheelHit))
            {
                skidTotal  = Mathf.Abs(wheelHit.forwardSlip) + Mathf.Abs(wheelHit.sidewaysSlip);
                skidTotal *= WHEEL_SLIP_MULTIPLIER;
            }
            else
            {
                skidTotal = 0f;
            }



            // Skid if we should

            if (skidTotal >= SKID_FX_SPEED)
            {
                float   intensity = Mathf.Clamp01(skidTotal / MAX_SKID_INTENSITY);
                Vector3 skidPoint = wheelHitInfo.point + (rb.velocity * Time.fixedDeltaTime);
                lastSkid = skidmarksController.AddSkidMark(skidPoint, wheelHitInfo.normal, intensity, lastSkid);
            }
            else
            {
                lastSkid = -1;
            }
        }
        else
        {
            lastSkid = -1;
        }
    }
Пример #8
0
    void UpdateWheels()
    {
        float handbrakeSlip = handbrake * rigidbody.velocity.magnitude * 0.1f;

        if (handbrakeSlip > 1)
        {
            handbrakeSlip = 1;
        }
        float totalSlip = 0.0f;

        onGround = false;

        foreach (WheelData w in wheels)
        {
            w.rotation += wheelRPM / 60.0f * -rev * 360.0f * Time.fixedDeltaTime;
            w.rotation  = Mathf.Repeat(w.rotation, 360.0f);
            w.graphic.localRotation = Quaternion.Euler(w.rotation, w.maxSteerAngle * steer, 0.0f) * w.originalRotation;
            if (w.coll.isGrounded)
            {
                onGround = true;
            }
            float slip = cornerSlip + (w.powered ? driveSlip : 0.0f) + (w.handbraked ? handbrakeSlip : 0.0f);
            totalSlip += slip;
            WheelHit      hit;
            WheelCollider c;
            c = w.coll;
            if (c.GetGroundHit(out hit))
            {
                Vector3 t = w.graphic.localPosition; t.y -= Vector3.Dot(w.graphic.position - hit.point, transform.up) - w.coll.radius; w.graphic.localPosition = t;
                if (slip > 0.5 && hit.collider.tag == "Dusty")
                {
                    groundDustEffect.position = hit.point;
                    groundDustEffect.particleEmitter.worldVelocity = rigidbody.velocity * 0.5f;
                    groundDustEffect.particleEmitter.minEmission   = (slip - 0.5f) * 3f;
                    groundDustEffect.particleEmitter.maxEmission   = (slip - 0.5f) * 3f;
                    groundDustEffect.particleEmitter.Emit();
                }
                if (slip > 0.75 && skidmarks != null)
                {
                    w.lastSkidMark = skidmarks.AddSkidMark(hit.point, hit.normal, (slip - 0.75f) * 2f, w.lastSkidMark);
                }
                else
                {
                    w.lastSkidMark = -1;
                }
            }
            else
            {
                w.lastSkidMark = -1;
            }
        }
        totalSlip /= wheels.Length;
    }
Пример #9
0
    void OnCollisionStay(Collision coll)
    {
        if (!floatFlag)
        {
//			if(GlobalInfo.isMoving){
            Vector3 aaa = coll.contacts[0].normal;
//				aaa = new Vector3(aaa.x,aaa.y * -1, aaa.z);
            if (wheelCollider[5].isGrounded || wheelCollider[6].isGrounded)
            {
                leftLastIndex = skid.AddSkidMark(leftSkidMarkPosition.position, aaa, skidMarkIntensity, BELTWIDH, leftLastIndex);
            }
            else
            {
                leftLastIndex = -1;
            }
            if (wheelCollider[13].isGrounded || wheelCollider[14].isGrounded)
            {
                rightLastIndex = skid.AddSkidMark(rightSkidMarkPosition.position, aaa, skidMarkIntensity, BELTWIDH, rightLastIndex);
            }
            else
            {
                rightLastIndex = -1;
            }
//			}
            foreach (Transform a in waterFoam)
            {
                a.particleEmitter.emit = false;
            }
        }
        else
        {
            foreach (Transform a in waterFoam)
            {
                a.particleEmitter.emit = true;
            }
            leftLastIndex  = -1;
            rightLastIndex = -1;
        }
    }
Пример #10
0
 void CalcSkidmarks()
 {
     if (slipVelo > slipVeloThreshold)
     {
         slipSkidAmount      = (slipVelo - slipVeloThreshold) / 15;
         skidmarks.markWidth = width;
         lastSkid            = skidmarks.AddSkidMark(hitDown.point, hitDown.normal, slipSkidAmount, lastSkid);
     }
     else
     {
         lastSkid = -1;
     }
 }
Пример #11
0
    protected void LateUpdate()
    {
        if (wheelCollider.GetGroundHit(out wheelHitInfo))
        {
            vertical  = Input.GetAxis("Vertical");
            handbrake = Input.GetKey(KeyCode.LeftShift);
            // Check sideways speed

            // Gives velocity with +z being the car's forward axis
            Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
            float   skidTotal     = Mathf.Abs(localVelocity.x);

            // Check wheel spin as well

            float wheelAngularVelocity = wheelCollider.radius * ((2 * Mathf.PI * wheelCollider.rpm) / 60);
            float carForwardVel        = Vector3.Dot(rb.velocity, transform.forward);
            if (Mathf.Abs(vertical) > 0f)
            {
                float wheelSpin = Mathf.Abs(carForwardVel - wheelAngularVelocity) * WHEEL_SLIP_MULTIPLIER;
                // NOTE: This extra line should not be needed and you can take it out if you have decent wheel physics
                // The built-in Unity demo car is actually skidding its wheels the ENTIRE time you're accelerating,
                // so this fades out the wheelspin-based skid as speed increases to make it look almost OK
                wheelSpin  = Mathf.Max(0, wheelSpin * (3 - Mathf.Abs(carForwardVel)));
                skidTotal += wheelSpin;
            }
            if (handbrake)
            {
                float wheelSpin = Mathf.Abs(carForwardVel - wheelAngularVelocity) * WHEEL_SLIP_MULTIPLIER;
                skidTotal += wheelSpin;
            }


            // Skid if we should
            if (skidTotal >= SKID_FX_SPEED)
            {
                float intensity = Mathf.Clamp01(skidTotal / MAX_SKID_INTENSITY);
                // Account for further movement since the last FixedUpdate
                Vector3 skidPoint = wheelHitInfo.point + (rb.velocity * (Time.time - lastFixedUpdateTime));
                lastSkid = skidmarksController.AddSkidMark(skidPoint, wheelHitInfo.normal, intensity, lastSkid);
            }
            else
            {
                lastSkid = -1;
            }
        }
        else
        {
            lastSkid = -1;
        }
    }
Пример #12
0
    void LateUpdate()
    {
        float intensity = playerCar.SideSlipAmount;

        if (intensity < 0)
        {
            intensity = -intensity;
        }

        if (intensity > 0.3f)
        {
            lastSkidId = skidMarkController.AddSkidMark(transform.position, transform.right, intensity * intensityModifier, lastSkidId);
        }
        else
        {
            lastSkidId = -1;
        }
    }
Пример #13
0
    void FixedUpdate()
    {
        WheelHit GroundHit;

        wheel_col.GetGroundHit(out GroundHit);
        var wheelSlipAmount = Mathf.Abs(GroundHit.sidewaysSlip);

        if (wheelSlipAmount > startSlipValue)
        {
            Vector3 skidPoint = GroundHit.point + 2 * (carParent.velocity) * Time.deltaTime;

            lastSkidmark = skidmarks.AddSkidMark(skidPoint, GroundHit.normal, wheelSlipAmount / 2.0f, lastSkidmark, markWidth);
            //skidmarks.PlaySkidSound (true);
        }
        else
        {
            lastSkidmark = -1;
            ///skidmarks.PlaySkidSound (false);
        }
    }
Пример #14
0
 static public int AddSkidMark(IntPtr l)
 {
     try {
         Skidmarks           self = (Skidmarks)checkSelf(l);
         UnityEngine.Vector3 a1;
         checkType(l, 2, out a1);
         UnityEngine.Vector3 a2;
         checkType(l, 3, out a2);
         System.Single a3;
         checkType(l, 4, out a3);
         System.Int32 a4;
         checkType(l, 5, out a4);
         var ret = self.AddSkidMark(a1, a2, a3, a4);
         pushValue(l, ret);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Пример #15
0
    // Update is called once per frame
    void Update()
    {
        if (skidmarksController != null && wheel.IsGrounded)
        {
            float slipSum = wheel.SlipForward * forwardSlipFactor + wheel.SlipSidewards * sideSlipFactor;

            if (slipSum > slipThreshold)
            {
                float skidAmount = (slipSum - slipThreshold) * slipFactor;

                lastIndex = skidmarksController.AddSkidMark(wheel.GroundHit, wheel.GroundNormal, skidAmount, lastIndex);
            }
            else
            {
                lastIndex = -1;
            }
        }
        else
        {
            lastIndex = -1;
        }
    }
Пример #16
0
    void FixedUpdate()
    {
        Vector3 pos = transform.position;

        up = transform.up;
        RaycastHit hit;
        bool       onGround = Physics.Raycast(pos, -up, out hit, suspensionTravel + radius);

        if (onGround && hit.collider.isTrigger)
        {
            onGround = false; float dist = suspensionTravel + radius;
            RaycastHit[]            hits = Physics.RaycastAll(pos, -up, suspensionTravel + radius);
            foreach (RaycastHit test in hits)
            {
                if (!test.collider.isTrigger && test.distance <= dist)
                {
                    hit      = test;
                    onGround = true;
                    dist     = test.distance;
                }
            }
        }

        if (onGround)
        {
            groundNormal    = transform.InverseTransformDirection(inverseLocalRotation * hit.normal);
            compression     = 1.0f - ((hit.distance - radius) / suspensionTravel);
            wheelVelo       = body.GetPointVelocity(pos);
            localVelo       = transform.InverseTransformDirection(inverseLocalRotation * wheelVelo);
            suspensionForce = SuspensionForce();
            roadForce       = RoadForce();
            body.AddForceAtPosition(suspensionForce + roadForce, pos);
        }
        else
        {
            compression     = 0.0f;
            suspensionForce = Vector3.zero;
            roadForce       = Vector3.zero;
            float totalInertia         = inertia + drivetrainInertia;
            float driveAngularDelta    = driveTorque * Time.deltaTime / totalInertia;
            float totalFrictionTorque  = brakeFrictionTorque * brake + handbrakeFrictionTorque * handbrake + frictionTorque + driveFrictionTorque;
            float frictionAngularDelta = totalFrictionTorque * Time.deltaTime / totalInertia;
            angularVelocity += driveAngularDelta;
            if (Mathf.Abs(angularVelocity) > frictionAngularDelta)
            {
                angularVelocity -= frictionAngularDelta * Mathf.Sign(angularVelocity);
            }
            else
            {
                angularVelocity = 0;
            }
            slipRatio = 0;
            slipVelo  = 0;
        }

        if (skid != null && Mathf.Abs(slipRatio) > 0.2)
        {
            lastSkid = skid.AddSkidMark(hit.point, hit.normal, Mathf.Abs(slipRatio) - 0.2f, lastSkid);
        }
        else
        {
            lastSkid = -1;
        }

        compression = Mathf.Clamp01(compression);
        rotation   += angularVelocity * Time.deltaTime;
        if (model != null)
        {
            model.transform.localPosition = Vector3.up * (compression - 1.0f) * suspensionTravel;
            model.transform.localRotation = Quaternion.Euler(Mathf.Rad2Deg * rotation, maxSteeringAngle * steering, 0);
        }
    }
Пример #17
0
 void SetLeftSkidMarkParamsRPC(Vector3 pos, Vector3 nor)
 {
     leftLastIndex = skid.AddSkidMark(pos, nor, 1.0f, 0.6f, leftLastIndex);
 }
Пример #18
0
    void FixedUpdate()
    {
        Vector3 pos = transform.position;

        up = transform.up;
        RaycastHit hit;

        onGround = Physics.Raycast(pos, -up, out hit, suspensionTravel + radius);
        if (hit.collider != null)
        {
            groundName = hit.collider.gameObject.tag;
            Debug.DrawLine(model.transform.position, hit.point, Color.magenta);
        }

        if (onGround && hit.collider.isTrigger)
        {
            onGround = false; float dist = suspensionTravel + radius;
            RaycastHit[]            hits = Physics.RaycastAll(pos, -up, suspensionTravel + radius);
            foreach (RaycastHit test in hits)
            {
                if (!test.collider.isTrigger && test.distance <= dist)
                {
                    hit      = test;
                    onGround = true;
                    dist     = test.distance;
                }
            }
        }

        if (onGround)
        {
            groundNormal    = transform.InverseTransformDirection(inverseLocalRotation * hit.normal);
            compression     = 1.0f - ((hit.distance - radius) / suspensionTravel);
            wheelVelo       = body.GetPointVelocity(pos);
            localVelo       = transform.InverseTransformDirection(inverseLocalRotation * wheelVelo);
            suspensionForce = SuspensionForce();

            int speed = (int)(GetComponentInParent <Rigidbody>().velocity.magnitude * 3.6f);

            // if car is stopped , do the following to avoid slipping (currently as a whole instead of just sideways , #tbf)
            if (speed == 0)
            {
                suspensionForce = new Vector3(0f, suspensionForce.y, 0f);
            }

            roadForce = RoadForce();


            body.AddForceAtPosition(suspensionForce + roadForce, pos);
        }
        else
        {
            compression     = 0.0f;
            suspensionForce = Vector3.zero;
            roadForce       = Vector3.zero;
            float totalInertia         = inertia + drivetrainInertia;
            float driveAngularDelta    = driveTorque * Time.deltaTime / totalInertia;
            float totalFrictionTorque  = brakeFrictionTorque * brake + handbrakeFrictionTorque * handbrake + frictionTorque + driveFrictionTorque;
            float frictionAngularDelta = totalFrictionTorque * Time.deltaTime / totalInertia;
            angularVelocity += driveAngularDelta;
            if (Mathf.Abs(angularVelocity) > frictionAngularDelta)
            {
                angularVelocity -= frictionAngularDelta * Mathf.Sign(angularVelocity);
            }
            else
            {
                angularVelocity = 0;
            }


            slipRatio = 0;
            slipVelo  = 0;
        }

        if (skid != null && Mathf.Abs(slipRatio) > 0.2)
        {
            if (groundName == "Road")
            {
                lastSkid = skid.AddSkidMark(hit.point, hit.normal, Mathf.Abs(slipRatio) - 0.2f, lastSkid);
            }
        }
        else
        {
            lastSkid = -1;
        }

        compression     = Mathf.Clamp01(compression);
        rotation       += angularVelocity * Time.deltaTime;
        angularVelocity = angularVelocity < -45f ? -45f : angularVelocity;

        if (model != null)
        {
            model.transform.localPosition = Vector3.up * (compression - 1.0f) * suspensionTravel;
            model.transform.localRotation = Quaternion.Euler(Mathf.Rad2Deg * rotation, maxSteeringAngle * steering, 0);
        }
    }
    void OnCollisionStay(Collision col)
    {
        float ang  = 0.0f;
        bool  flag = false;

        for (int i = 0; i < col.contacts.Length; i++)
        {
            if (col.collider.gameObject.layer == 12 || col.collider.gameObject.layer == 14)
            {
                flag = true;
                ang  = Vector3.Angle(transform.right, col.contacts[i].point - transform.position);
                if (ang < 60)
                {
                    ang = Vector3.Angle(transform.forward, col.contacts[i].point - transform.position);
                    if (ang < 90)
                    {
                        forwardRightHitFlag = true;
                    }
                    else
                    {
                        backwardRightHitFlag = true;
                    }
                }
                else if (ang > 120)
                {
                    ang = Vector3.Angle(transform.forward, col.contacts[i].point - transform.position);
                    if (ang < 90)
                    {
                        forwardLeftHitFlag = true;
                    }
                    else
                    {
                        backwardLeftHitFlag = true;
                    }
                }
            }
        }
        if (!flag)
        {
            forwardLeftHitFlag   = false;
            forwardRightHitFlag  = false;
            backwardLeftHitFlag  = false;
            backwardRightHitFlag = false;
        }
        if (!floatFlag)
        {
            if (GlobalInfo.isMoving)
            {
                if (wheelCollider[wheelCount / 2 - 2].isGrounded || wheelCollider[wheelCount / 2 - 1].isGrounded)
                {
                    leftLastIndex = skid.AddSkidMark(leftSkidMarkPosition.position, col.contacts[0].normal, skidMarkIntensity, crawlerWidth, leftLastIndex);
                }
                else
                {
                    leftLastIndex = -1;
                }
                if (wheelCollider[wheelCount - 1].isGrounded || wheelCollider[wheelCount - 2].isGrounded)
                {
                    rightLastIndex = skid.AddSkidMark(rightSkidMarkPosition.position, col.contacts[0].normal, skidMarkIntensity, crawlerWidth, rightLastIndex);
                }
                else
                {
                    rightLastIndex = -1;
                }
            }
        }
        else
        {
            leftLastIndex  = -1;
            rightLastIndex = -1;
        }
    }
Пример #20
0
    void  UpdateWheelGraphics(Vector3 relativeVelocity)
    {
        wheelCount = -1;

        foreach (Wheel w in wheels)
        {
            wheelCount++;
            WheelCollider wheel = w.collider;
            WheelHit      wh    = new WheelHit();

            // First we get the velocity at the point where the wheel meets the ground, if the wheel is touching the ground
            if (wheel.GetGroundHit(out wh))
            {
                w.wheelGraphic.localPosition = wheel.transform.up * (wheelRadius + wheel.transform.InverseTransformPoint(wh.point).y);
                w.wheelVelo   = rigidbody.GetPointVelocity(wh.point);
                w.groundSpeed = w.wheelGraphic.InverseTransformDirection(w.wheelVelo);

                // Code to handle skidmark drawing. Not covered in the tutorial
                if (skidmarks)
                {
                    if (skidmarkTime[wheelCount] < 0.02f && w.lastSkidmark != -1)
                    {
                        skidmarkTime[wheelCount] += Time.deltaTime;
                    }
                    else
                    {
                        float dt = skidmarkTime[wheelCount] == 0.0f ? Time.deltaTime : skidmarkTime[wheelCount];
                        skidmarkTime[wheelCount] = 0.0f;

                        float handbrakeSkidding = handbrake && w.driveWheel ? w.wheelVelo.magnitude * 0.3f : 0;
                        float skidGroundSpeed   = Mathf.Abs(w.groundSpeed.x) - 15;
                        if (skidGroundSpeed > 0 || handbrakeSkidding > 0)
                        {
                            Vector3 staticVel = transform.TransformDirection(skidSmoke.localVelocity) + skidSmoke.worldVelocity;
                            if (w.lastSkidmark != -1)
                            {
                                float emission             = UnityEngine.Random.Range(skidSmoke.minEmission, skidSmoke.maxEmission);
                                float lastParticleCount    = w.lastEmitTime * emission;
                                float currentParticleCount = Time.time * emission;
                                int   noOfParticles        = Mathf.CeilToInt(currentParticleCount) - Mathf.CeilToInt(lastParticleCount);
                                int   lastParticle         = Mathf.CeilToInt(lastParticleCount);

                                for (int i = 0; i <= noOfParticles; i++)
                                {
                                    float particleTime = Mathf.InverseLerp(lastParticleCount, currentParticleCount, lastParticle + i);
                                    skidSmoke.Emit(Vector3.Lerp(w.lastEmitPosition, wh.point, particleTime) + new Vector3(Random.Range(-0.1f, 0.1f), Random.Range(-0.1f, 0.1f), Random.Range(-0.1f, 0.1f)), staticVel + (w.wheelVelo * 0.05f), Random.Range(skidSmoke.minSize, skidSmoke.maxSize) * Mathf.Clamp(skidGroundSpeed * 0.1f, 0.5f, 1), Random.Range(skidSmoke.minEnergy, skidSmoke.maxEnergy), Color.white);
                                }
                            }
                            else
                            {
                                skidSmoke.Emit(wh.point + new Vector3(Random.Range(-0.1f, 0.1f), Random.Range(-0.1f, 0.1f), Random.Range(-0.1f, 0.1f)), staticVel + (w.wheelVelo * 0.05f), Random.Range(skidSmoke.minSize, skidSmoke.maxSize) * Mathf.Clamp(skidGroundSpeed * 0.1f, 0.5f, 1), Random.Range(skidSmoke.minEnergy, skidSmoke.maxEnergy), Color.white);
                            }

                            w.lastEmitPosition = wh.point;
                            w.lastEmitTime     = Time.time;

                            w.lastSkidmark = skidmarks.AddSkidMark(wh.point + rigidbody.velocity * dt, wh.normal, (skidGroundSpeed * 0.1f + handbrakeSkidding) * Mathf.Clamp01(wh.force / wheel.suspensionSpring.spring), w.lastSkidmark);
//						sound.Skid(true, Mathf.Clamp01(skidGroundSpeed * 0.1f));
                        }
                        else
                        {
                            w.lastSkidmark = -1;
//						sound.Skid(false, 0);
                        }
                    }
                }
            }
            else
            {
                // If the wheel is not touching the ground we set the position of the wheel graphics to
                // the wheel's transform position + the range of the suspension.
                w.wheelGraphic.position = wheel.transform.position + (-wheel.transform.up * suspensionRange);
                if (w.steerWheel)
                {
                    w.wheelVelo *= 0.9f;
                }
                else
                {
                    w.wheelVelo *= 0.9f * (1 - throttle);
                }

                if (skidmarks)
                {
                    w.lastSkidmark = -1;
//				sound.Skid(false, 0);
                }
            }
            // If the wheel is a steer wheel we apply two rotations:
            // *Rotation around the Steer Column (visualizes the steer direction)
            // *Rotation that visualizes the speed
            if (w.steerWheel)
            {
                Vector3 ea = w.wheelGraphic.parent.localEulerAngles;
                ea.y = steer * maximumTurn;
                w.wheelGraphic.parent.localEulerAngles = ea;
                w.tireGraphic.Rotate(Vector3.right * (w.groundSpeed.z / wheelRadius) * Time.deltaTime * Mathf.Rad2Deg);
            }
            else if (!handbrake && w.driveWheel)
            {
                // If the wheel is a drive wheel it only gets the rotation that visualizes speed.
                // If we are hand braking we don't rotate it.
                w.tireGraphic.Rotate(Vector3.right * (w.groundSpeed.z / wheelRadius) * Time.deltaTime * Mathf.Rad2Deg);
            }
        }
    }