IEnumerator ResetRotation() { yield return(new WaitForFixedUpdate()); /*tr.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0); * tr.Translate(Vector3.up, Space.World); * rb.velocity = Vector3.zero; * rb.angularVelocity = Vector3.zero;*/ //DEVNOOB EDIT tr.position = VehicleDebug.spawnPos; tr.eulerAngles = VehicleDebug.spawnRot; tr.Translate(Vector3.up, Space.World); rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; float randVal = Random.value; if (randVal < 0.5f) { target = TargetAfterReset1; } else { target = TargetAfterReset2; } targetWaypoint = targetWaypoint.nextPoint; VehicleDebug.fallLimit = -0.8f; }
public void InitializeTarget() { if (target) { //if target is a vehicle targetBody = target.GetTopmostParentComponent <Rigidbody>(); //if target is a waypoint targetWaypoint = target.GetComponent <VehicleWaypoint>(); if (targetWaypoint) { prevSpeed = targetWaypoint.speed; } } }
void FixedUpdate() { if (target) { if (target != targetPrev) { InitializeTarget(); } targetPrev = target; //Is the target a waypoint? targetIsWaypoint = target.GetComponent <VehicleWaypoint>(); //Can I see the target? targetVisible = !Physics.Linecast(tr.position, target.position, viewBlockMask); if (targetVisible || targetIsWaypoint) { targetPoint = targetBody ? target.position + targetBody.velocity : target.position; } if (targetIsWaypoint) { //if vehicle is close enough to target waypoint, switch to the next one if ((tr.position - target.position).sqrMagnitude <= targetWaypoint.radius * targetWaypoint.radius) { target = targetWaypoint.nextPoint.transform; targetWaypoint = targetWaypoint.nextPoint; prevSpeed = speed; speed = Mathf.Clamp01(targetWaypoint.speed * initialSpeed); brakeTime = prevSpeed / speed; if (brakeTime <= 1) { brakeTime = 0; } } } brakeTime = Mathf.Max(0, brakeTime - Time.fixedDeltaTime); //Is the distance to the target less than the follow distance? close = (tr.position - target.position).sqrMagnitude <= Mathf.Pow(followDistance, 2) && !targetIsWaypoint; dirToTarget = (targetPoint - tr.position).normalized; lookDot = Vector3.Dot(vp.forwardDir, dirToTarget); steerDot = Vector3.Dot(vp.rightDir, dirToTarget); //Attempt to reverse if vehicle is stuck stoppedTime = Mathf.Abs(vp.localVelocity.z) < 1 && !close && vp.groundedWheels > 0 ? stoppedTime + Time.fixedDeltaTime : 0; if (stoppedTime > stopTimeReverse && reverseTime == 0) { reverseTime = reverseAttemptTime; reverseAttempts++; } //Reset if reversed too many times if (reverseAttempts > resetReverseCount && resetReverseCount >= 0) { StartCoroutine(ReverseReset()); } reverseTime = Mathf.Max(0, reverseTime - Time.fixedDeltaTime); if (targetVelocity > 0) { speedLimit = Mathf.Clamp01(targetVelocity - vp.localVelocity.z); } else { speedLimit = 1; } //Set vehicle inputs vp.SetAccel(!close && (lookDot > 0 || vp.localVelocity.z < 5) && vp.groundedWheels > 0 && reverseTime == 0 ? speed * speedLimit : 0); vp.SetBrake(reverseTime == 0 && brakeTime == 0 && !(close && vp.localVelocity.z > 0.1f) ? (lookDot < 0.5f && lookDot > 0 && vp.localVelocity.z > 10 ? 0.5f - lookDot : 0) : (reverseTime > 0 ? 1 : (brakeTime > 0 ? brakeTime * 0.2f : 1 - Mathf.Clamp01(Vector3.Distance(tr.position, target.position) / Mathf.Max(0.01f, followDistance))))); vp.SetSteer(reverseTime == 0 ? Mathf.Abs(Mathf.Pow(steerDot, (tr.position - target.position).sqrMagnitude > 20 ? 1 : 2)) * Mathf.Sign(steerDot) : -Mathf.Sign(steerDot) * (close ? 0 : 1)); vp.SetEbrake((close && vp.localVelocity.z <= 0.1f) || (lookDot <= 0 && vp.velMag > 20) ? 1 : 0); } rolledOverTime = va.rolledOver ? rolledOverTime + Time.fixedDeltaTime : 0; //Reset if stuck rolled over if (rolledOverTime > rollResetTime && rollResetTime >= 0) { StartCoroutine(ResetRotation()); } }
//DEVNOOB ONTRIGGERENTEREDIT private void OnTriggerEnter(Collider other) { if (other.tag == "Teleport1") { transform.position = new Vector3(-25f, 60, -32); transform.rotation = Quaternion.Euler(0, 90, 0); GetComponent <Rigidbody>().velocity = Vector3.zero; GetComponent <Rigidbody>().angularVelocity = Vector3.zero; target = TargetAfterTeleport; targetWaypoint = targetWaypoint.nextPoint; prevSpeed = speed; speed = Mathf.Clamp01(targetWaypoint.speed * initialSpeed); brakeTime = prevSpeed / speed; if (brakeTime <= 1) { brakeTime = 0; } VehicleDebug.fallLimit = 30; } if (other.tag == "Teleport2") { transform.position = new Vector3(-36, 1, 46); transform.rotation = Quaternion.Euler(0, -90, 0); GetComponent <Rigidbody>().velocity = Vector3.zero; GetComponent <Rigidbody>().angularVelocity = Vector3.zero; target = TargetAfterTeleport2; targetWaypoint = targetWaypoint.nextPoint; prevSpeed = speed; speed = Mathf.Clamp01(targetWaypoint.speed * initialSpeed); brakeTime = prevSpeed / speed; if (brakeTime <= 1) { brakeTime = 0; } VehicleDebug.fallLimit = -0.8f; } if (other.tag == "Checkpoint1") { VehicleDebug.spawnPos = other.transform.position; VehicleDebug.spawnRot = transform.rotation.eulerAngles; TargetAfterReset1 = other.GetComponent <Checkpoints>().nextWayPoint1; TargetAfterReset2 = other.GetComponent <Checkpoints>().nextWayPoint2; LatestCheckpoint = other.gameObject; } if (other.tag == "Bullet") { StartCoroutine(VehicleDebug.ResetRotation()); Destroy(other.gameObject); } if (other.tag == "lego") { StartCoroutine(VehicleDebug.ResetRotation()); Destroy(other.gameObject); } if (other.tag == "boobyTrap") { StartCoroutine(VehicleDebug.ResetRotation()); Destroy(other.gameObject); } }