static void CreateBehavior() { if (!Selection.activeGameObject.GetComponent <RCC_CarControllerV3>()) { GameObject pivot = new GameObject(Selection.activeGameObject.name); pivot.transform.position = RCC_GetBounds.GetBoundsCenter(Selection.activeGameObject.transform); pivot.transform.rotation = Selection.activeGameObject.transform.rotation; pivot.AddComponent <RCC_CarControllerV3>(); pivot.GetComponent <Rigidbody>().mass = 1350f; pivot.GetComponent <Rigidbody>().drag = .05f; pivot.GetComponent <Rigidbody>().angularDrag = .5f; pivot.GetComponent <Rigidbody>().interpolation = RigidbodyInterpolation.Interpolate; Selection.activeGameObject.transform.SetParent(pivot.transform); Selection.activeGameObject = pivot; if (EditorUtility.DisplayDialog("Build Your Car With Default Prefabs and Settings", "Would You Like To Build Your Car With Default Prefabs and Settings?", "Yes", "No")) { firstInit = true; } EditorUtility.DisplayDialog("RCC Initialized", "Drag and drop all your wheel models in to ''Wheel Models'' from hierarchy.", "Ok"); } else { EditorUtility.DisplayDialog("Your Gameobject Already Has Realistic Car ControllerV2", "Your Gameobject Already Has Realistic Car ControllerV2", "Ok"); } }
static void CreateBehavior() { if (!Selection.activeGameObject.GetComponentInParent <RCC_CarControllerV3>()) { GameObject pivot = new GameObject(Selection.activeGameObject.name); pivot.transform.position = RCC_GetBounds.GetBoundsCenter(Selection.activeGameObject.transform); pivot.transform.rotation = Selection.activeGameObject.transform.rotation; pivot.AddComponent <RCC_CarControllerV3>(); pivot.AddComponent <HR_ModApplier>(); pivot.AddComponent <HR_PlayerHandler>(); pivot.GetComponent <Rigidbody>().mass = 1350f; pivot.GetComponent <Rigidbody>().drag = .05f; pivot.GetComponent <Rigidbody>().angularDrag = .5f; pivot.GetComponent <Rigidbody>().interpolation = RigidbodyInterpolation.Interpolate; Selection.activeGameObject.transform.SetParent(pivot.transform); Selection.activeGameObject = pivot; firstInit = true; EditorUtility.DisplayDialog("RCC Initialized", "Drag and drop all your wheel models in to ''Wheel Models'' from hierarchy.", "Ok"); } else { EditorUtility.DisplayDialog("Your Gameobject Already Has Realistic Car ControllerV3", "Your Gameobject Already Has Realistic Car ControllerV3", "Ok"); } }
static void CreateBehavior() { if (!Selection.activeGameObject.GetComponentInParent <SCC_Drivetrain>()) { GameObject pivot = new GameObject(Selection.activeGameObject.name); pivot.transform.position = RCC_GetBounds.GetBoundsCenter(Selection.activeGameObject.transform); pivot.transform.rotation = Selection.activeGameObject.transform.rotation; pivot.transform.localScale = Vector3.one; pivot.AddComponent <SCC_Drivetrain>(); pivot.AddComponent <SCC_Inputs>(); pivot.AddComponent <SCC_Audio>(); pivot.AddComponent <SCC_Particles>(); pivot.AddComponent <SCC_AntiRoll>(); pivot.AddComponent <SCC_RigidStabilizer>(); pivot.GetComponent <Rigidbody>().mass = 1350f; pivot.GetComponent <Rigidbody>().drag = .05f; pivot.GetComponent <Rigidbody>().angularDrag = .5f; pivot.GetComponent <Rigidbody>().interpolation = RigidbodyInterpolation.Interpolate; pivot.GetComponent <Rigidbody> ().collisionDetectionMode = CollisionDetectionMode.Continuous; Selection.activeGameObject.transform.SetParent(pivot.transform); Selection.activeGameObject = pivot; GameObject COM = new GameObject("COM"); COM.transform.SetParent(pivot.transform, false); pivot.GetComponent <SCC_Drivetrain> ().COM = COM.transform; EditorUtility.DisplayDialog("SCC Initialized", "SCC Initialized. Select all of your wheels and create theirs wheel colliders.", "Ok"); } else { EditorUtility.DisplayDialog("Your Gameobject Already Has Simple Car Controller", "Your Gameobject Already Has Simple Car Controller", "Ok"); } }
/// <summary> /// Creates the wheel colliders. /// </summary> public void CreateWheelColliders(RCC_CarControllerV3 carController) { // Creating a list for all wheel models. List <Transform> allWheelModels = new List <Transform>(); allWheelModels.Add(carController.FrontLeftWheelTransform); allWheelModels.Add(carController.FrontRightWheelTransform); allWheelModels.Add(carController.RearLeftWheelTransform); allWheelModels.Add(carController.RearRightWheelTransform); // If we have additional rear wheels, add them too. if (carController.ExtraRearWheelsTransform.Length > 0 && carController.ExtraRearWheelsTransform[0]) { foreach (Transform t in carController.ExtraRearWheelsTransform) { allWheelModels.Add(t); } } // If we don't have any wheelmodels, throw an error. if (allWheelModels != null && allWheelModels[0] == null) { Debug.LogError("You haven't choosen your Wheel Models. Please select all of your Wheel Models before creating Wheel Colliders. Script needs to know their sizes and positions, aye?"); return; } // Holding default rotation. Quaternion currentRotation = transform.rotation; // Resetting rotation. transform.rotation = Quaternion.identity; // Creating a new gameobject called Wheel Colliders for all Wheel Colliders, and parenting it to this gameobject. GameObject WheelColliders = new GameObject("Wheel Colliders"); WheelColliders.transform.SetParent(transform, false); WheelColliders.transform.localRotation = Quaternion.identity; WheelColliders.transform.localPosition = Vector3.zero; WheelColliders.transform.localScale = Vector3.one; // Creating WheelColliders. foreach (Transform wheel in allWheelModels) { GameObject wheelcollider = new GameObject(wheel.transform.name); wheelcollider.transform.position = RCC_GetBounds.GetBoundsCenter(wheel.transform); wheelcollider.transform.rotation = transform.rotation; wheelcollider.transform.name = wheel.transform.name; wheelcollider.transform.SetParent(WheelColliders.transform); wheelcollider.transform.localScale = Vector3.one; wheelcollider.AddComponent <WheelCollider>(); Bounds biggestBound = new Bounds(); Renderer[] renderers = wheel.GetComponentsInChildren <Renderer>(); foreach (Renderer render in renderers) { if (render != GetComponent <Renderer>()) { if (render.bounds.size.z > biggestBound.size.z) { biggestBound = render.bounds; } } } wheelcollider.GetComponent <WheelCollider>().radius = (biggestBound.extents.y) / transform.localScale.y; wheelcollider.AddComponent <RCC_WheelCollider>(); JointSpring spring = wheelcollider.GetComponent <WheelCollider>().suspensionSpring; spring.spring = 40000f; spring.damper = 1500f; spring.targetPosition = .5f; wheelcollider.GetComponent <WheelCollider>().suspensionSpring = spring; wheelcollider.GetComponent <WheelCollider>().suspensionDistance = .2f; wheelcollider.GetComponent <WheelCollider>().forceAppPointDistance = 0f; wheelcollider.GetComponent <WheelCollider>().mass = 40f; wheelcollider.GetComponent <WheelCollider>().wheelDampingRate = 1f; WheelFrictionCurve sidewaysFriction; WheelFrictionCurve forwardFriction; sidewaysFriction = wheelcollider.GetComponent <WheelCollider>().sidewaysFriction; forwardFriction = wheelcollider.GetComponent <WheelCollider>().forwardFriction; forwardFriction.extremumSlip = .3f; forwardFriction.extremumValue = 1; forwardFriction.asymptoteSlip = .8f; forwardFriction.asymptoteValue = .6f; forwardFriction.stiffness = 1.5f; sidewaysFriction.extremumSlip = .3f; sidewaysFriction.extremumValue = 1; sidewaysFriction.asymptoteSlip = .5f; sidewaysFriction.asymptoteValue = .8f; sidewaysFriction.stiffness = 1.5f; wheelcollider.GetComponent <WheelCollider>().sidewaysFriction = sidewaysFriction; wheelcollider.GetComponent <WheelCollider>().forwardFriction = forwardFriction; } RCC_WheelCollider[] allWheelColliders = new RCC_WheelCollider[allWheelModels.Count]; allWheelColliders = GetComponentsInChildren <RCC_WheelCollider>(); carController.FrontLeftWheelCollider = allWheelColliders[0]; carController.FrontRightWheelCollider = allWheelColliders[1]; carController.RearLeftWheelCollider = allWheelColliders[2]; carController.RearRightWheelCollider = allWheelColliders[3]; carController.ExtraRearWheelsCollider = new RCC_WheelCollider[carController.ExtraRearWheelsTransform.Length]; for (int i = 0; i < carController.ExtraRearWheelsTransform.Length; i++) { carController.ExtraRearWheelsCollider[i] = allWheelColliders[i + 4]; } transform.rotation = currentRotation; }
void Start() { carController = GetComponentInParent <RCC_CarControllerV3>(); rigid = carController.GetComponent <Rigidbody> (); // Getting all WheelColliders attached to this vehicle (Except this). allWheelColliders = carController.GetComponentsInChildren <RCC_WheelCollider>().ToList(); // allWheelColliders.Remove(this); GetTerrainData(); // Are we going to use skidmarks? If we do, get or create SkidmarksManager on scene. if (!RCCSettings.dontUseSkidmarks) { if (GameObject.FindObjectOfType(typeof(RCC_Skidmarks))) { skidmarks = GameObject.FindObjectOfType(typeof(RCC_Skidmarks)) as RCC_Skidmarks; } else { skidmarks = (RCC_Skidmarks)Instantiate(RCCSettings.skidmarksManager, Vector3.zero, Quaternion.identity); } } // Increasing WheelCollider mass for avoiding unstable behavior. Only in Unity 5. if (RCCSettings.useFixedWheelColliders) { wheelCollider.mass = rigid.mass / 20f; // wheelCollider.ConfigureVehicleSubsteps(1000f, 10, 10); } // Getting friction curves. forwardFrictionCurve = wheelCollider.forwardFriction; sidewaysFrictionCurve = wheelCollider.sidewaysFriction; // Proper settings for selected behavior type. switch (RCCSettings.behaviorType) { case RCC_Settings.BehaviorType.SemiArcade: forwardFrictionCurve = SetFrictionCurves(forwardFrictionCurve, .2f, 2f, 2f, 2f); sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .25f, 2f, 2f, 2f); wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, .35f, 1f); break; case RCC_Settings.BehaviorType.Drift: forwardFrictionCurve = SetFrictionCurves(forwardFrictionCurve, .3f, 1f, .8f, 1f); sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .3f, 1f, .5f, 1f); wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, .1f, 1f); if (carController._wheelTypeChoise == RCC_CarControllerV3.WheelType.FWD) { Debug.LogWarning("Current behavior mode is ''Drift'', but your vehicle named " + carController.name + " was FWD. You have to use RWD, AWD, or BIASED to rear wheels. Setting it to *RWD* now. "); carController._wheelTypeChoise = RCC_CarControllerV3.WheelType.RWD; } break; case RCC_Settings.BehaviorType.Fun: forwardFrictionCurve = SetFrictionCurves(forwardFrictionCurve, .2f, 2f, 2f, 2f); sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .25f, 2f, 2f, 2f); wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, .35f, 1f); break; case RCC_Settings.BehaviorType.Racing: forwardFrictionCurve = SetFrictionCurves(forwardFrictionCurve, .2f, 1f, .8f, .75f); sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .3f, 1f, .25f, .75f); wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, .25f, 1f); break; case RCC_Settings.BehaviorType.Simulator: forwardFrictionCurve = SetFrictionCurves(forwardFrictionCurve, .2f, 1f, .8f, .75f); sidewaysFrictionCurve = SetFrictionCurves(sidewaysFrictionCurve, .25f, 1f, .5f, .75f); wheelCollider.forceAppPointDistance = Mathf.Clamp(wheelCollider.forceAppPointDistance, 0f, 1f); break; } // Assigning new frictons if one of the behavior preset selected above. wheelCollider.forwardFriction = forwardFrictionCurve; wheelCollider.sidewaysFriction = sidewaysFrictionCurve; // Creating audiosource for skid SFX. if (RCCSettings.useSharedAudioSources) { if (!carController.transform.Find("All Audio Sources/Skid Sound AudioSource")) { audioSource = RCC_CreateAudioSource.NewAudioSource(carController.gameObject, "Skid Sound AudioSource", 5f, 50f, 0f, audioClip, true, true, false); } else { audioSource = carController.transform.Find("All Audio Sources/Skid Sound AudioSource").GetComponent <AudioSource>(); } } else { audioSource = RCC_CreateAudioSource.NewAudioSource(carController.gameObject, "Skid Sound AudioSource", 5f, 50f, 0f, audioClip, true, true, false); audioSource.transform.position = transform.position; } // Creating all ground particles, and adding them to list. if (!RCCSettings.dontUseAnyParticleEffects) { for (int i = 0; i < RCCGroundMaterials.frictions.Length; i++) { GameObject ps = (GameObject)Instantiate(RCCGroundMaterials.frictions [i].groundParticles, transform.position, transform.rotation) as GameObject; emission = ps.GetComponent <ParticleSystem> ().emission; emission.enabled = false; ps.transform.SetParent(transform, false); ps.transform.localPosition = Vector3.zero; ps.transform.localRotation = Quaternion.identity; allWheelParticles.Add(ps.GetComponent <ParticleSystem> ()); } } GameObject newPivot = new GameObject("Pivot_" + wheelModel.transform.name); newPivot.transform.position = RCC_GetBounds.GetBoundsCenter(wheelModel.transform); newPivot.transform.rotation = transform.rotation; newPivot.transform.SetParent(wheelModel.transform.parent, true); wheelModel.SetParent(newPivot.transform, true); wheelModel = newPivot.transform; }
void Start (){ // Getting essentials. carController = GetComponentInParent<RCC_CarControllerV3>(); rigid = carController.GetComponent<Rigidbody> (); // Getting all WheelColliders attached to this vehicle (Except this). allWheelColliders = carController.GetComponentsInChildren<RCC_WheelCollider>().ToList(); GetTerrainData (); // Getting terrain datas on scene. CheckBehavior (); // Checks selected behavior in RCC Settings. // Are we going to use skidmarks? If we do, get or create SkidmarksManager on scene. if (!RCCSettings.dontUseSkidmarks) { if (GameObject.FindObjectOfType<RCC_SkidmarksManager> ()) skidmarksManager = GameObject.FindObjectOfType<RCC_SkidmarksManager> (); else skidmarksManager = GameObject.Instantiate (RCCSettings.skidmarksManager, Vector3.zero, Quaternion.identity); } // Increasing WheelCollider mass for avoiding unstable behavior. if (RCCSettings.useFixedWheelColliders) wheelCollider.mass = rigid.mass / 15f; // Creating audiosource for skid SFX. audioSource = NewAudioSource(RCCSettings.audioMixer, carController.gameObject, "Skid Sound AudioSource", 5f, 50f, 0f, audioClip, true, true, false); audioSource.transform.position = transform.position; // Creating all ground particles, and adding them to list. if (!RCCSettings.dontUseAnyParticleEffects) { for (int i = 0; i < RCCGroundMaterials.frictions.Length; i++) { GameObject ps = (GameObject)Instantiate (RCCGroundMaterials.frictions [i].groundParticles, transform.position, transform.rotation) as GameObject; emission = ps.GetComponent<ParticleSystem> ().emission; emission.enabled = false; ps.transform.SetParent (transform, false); ps.transform.localPosition = Vector3.zero; ps.transform.localRotation = Quaternion.identity; allWheelParticles.Add (ps.GetComponent<ParticleSystem> ()); } } // Creating pivot position of the wheel at correct position and rotation. GameObject newPivot = new GameObject ("Pivot_" + wheelModel.transform.name); newPivot.transform.position = RCC_GetBounds.GetBoundsCenter (wheelModel.transform); newPivot.transform.rotation = transform.rotation; newPivot.transform.SetParent (wheelModel.transform.parent, true); // Settings offsets. if (newPivot.transform.localPosition.x > 0) wheelModel.transform.position += transform.right * offset; else wheelModel.transform.position -= transform.right * offset; // Assigning temporary created wheel to actual wheel. wheelModel.SetParent (newPivot.transform, true); wheelModel = newPivot.transform; // Override wheels automatically if enabled. if (carController.overrideWheels) { // Overriding canPower, canSteer, canBrake, canHandbrake. if (this == carController.FrontLeftWheelCollider || this == carController.FrontRightWheelCollider) { canSteer = true; canBrake = true; brakingMultiplier = 1f; } if (this == carController.RearLeftWheelCollider || this == carController.RearRightWheelCollider) { canHandbrake = true; canBrake = true; brakingMultiplier = .5f; } } }