protected virtual void Update() { if (inArea && target && senser.transform.Distance(target.position) < distanceLimit) { Vector3 targetPosition = UMTools.SetVector3Axis(target.position, senser.transform.position.y, Axis.y); Vector3 dirToTarget = (targetPosition - senser.transform.position).normalized; float angle = Mathf.Abs(Vector3.Angle(senser.transform.forward, dirToTarget) - ((Vector3.Dot(senser.transform.forward, dirToTarget) < 0) ? 180 : 0)); if (angle < angleLimit && (!oneSide || Vector3.Dot(senser.transform.forward, target.forward) > 0)) { if (!isParked) { OnParked?.Invoke(); } isParked = true; } } }
void FixedUpdate() { ControlInput.DoUpdate(directionSpeed); Lights.DoUpdate(ControlInput.throttle, ControlInput.brake, ControlInput.handbrake > 0.5f ? 1 : 0, ControlInput.turn); float steerValue = ComputeSteerAngle(); foreach (Wheel wheel in wheels) { if (wheel.Steer) { wheel.WheelCollider.steerAngle = steerValue; } if (ControlInput.handbrake > 0.5f) { // Don't zero out this value or the wheel completely lock up wheel.WheelCollider.motorTorque = 0.0001f; wheel.WheelCollider.brakeTorque = brakeForce; } else if (wheel.Drive && ControlInput.brake < 0.5f) { wheel.WheelCollider.motorTorque = EngineCalculations(); //Remember to count drive wheels } wheel.WheelCollider.brakeTorque = brakeForce * ControlInput.brake; VisualWheelsUpdate(wheel); //Suspension Calculations EasySuspension(wheel.WheelCollider); } if (ControlInput.brake > 0.5f) { desireSpeed = Mathf.MoveTowards(desireSpeed, speed > maxSpeed / 2 ? maxSpeed / 2 : 0, Time.deltaTime * speed); } else { desireSpeed = maxSpeed; } //max Speed Counter attachedRigidbody.velocity = attachedRigidbody.velocity.normalized * Mathf.Clamp(attachedRigidbody.velocity.magnitude, 0, desireSpeed); //MaxAngular speed attachedRigidbody.angularDrag = Mathf.Lerp(0.01f, maxAngularDrag, attachedRigidbody.velocity.magnitude / maxSpeed); ApplyAntiRoll(); //Grounded Checking.... if (!IsGrounded) { //Direction changer force and rotation attachedRigidbody.AddForce(Vector3.Cross(UMTools.SetVector3Axis(attachedRigidbody.velocity, 0, Axis.y), Vector3.up) * (-ControlInput.turn * 500)); transform.Rotate(0, 0, Time.deltaTime * 0.1f * -ControlInput.turn); //Simple Gravity attachedRigidbody.AddForce(Physics.gravity * attachedRigidbody.mass); attachedRigidbody.angularVelocity = Vector3.zero; //Downforce DownForceImplementation(); transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, transform.eulerAngles.y, transform.eulerAngles.z), 10 * Time.deltaTime); } else { //Simple Gravity if (speed < 5f && transform.eulerAngles.z > 90) { attachedRigidbody.AddForce(Physics.gravity * attachedRigidbody.mass); } else //Stick Gravity { attachedRigidbody.AddForce(-transform.up * (Physics.gravity.magnitude * attachedRigidbody.mass)); } } if (ControlInput.Drift > 0.5f && isDriftActive) { Vector3 driftForce = -transform.right; driftForce.y = 0.0f; driftForce.Normalize(); if (Math.Abs(ControlInput.turn) > 0.01f) { driftForce *= attachedRigidbody.mass * speed / 7f * ControlInput.throttle * ControlInput.turn / steerAngle; } Vector3 driftTorque = transform.up * (0.1f * ControlInput.turn) / steerAngle; attachedRigidbody.AddForce(driftForce * driftIntensity, ForceMode.Force); attachedRigidbody.AddTorque(driftTorque * driftIntensity, ForceMode.VelocityChange); print("Drift"); } }