/** * Transformiert (rotiert) alle übergebenen Räder anhand des Wheelcolliders */ private void ApplyTransformsToWheels(AxleInfo front, AxleInfo rear) { WheelCollider[] wheels = new WheelCollider[4]; wheels[0] = front.leftWheel; wheels[1] = front.rightWheel; wheels[2] = rear.leftWheel; wheels[3] = rear.rightWheel; for (int i = 0; i < 4; i++) { if (wheels[i].transform.childCount == 0) { return; } //get child elem of the wheel collider elem Transform visualWheel = wheels[i].transform.GetChild(0); Vector3 position; Quaternion rotation; //returns the position and rotation of the wheel collider wheels[i].GetWorldPose(out position, out rotation); //out is like a return, a method can only return one value, with out you can get multiple visualWheel.transform.position = position; visualWheel.transform.rotation = rotation; } }
private void AntiRoll(AxleInfo axel) { WheelHit hit; float travelL = 1.0f; float travelR = 1.0f; bool groundedL = axel.leftWheel.GetGroundHit(out hit); if (groundedL) { travelL = ((axel.leftWheel.transform.InverseTransformPoint(hit.point).y + axel.leftWheel.radius) * -1) / axel.leftWheel.suspensionDistance; } bool groundedR = axel.rightWheel.GetGroundHit(out hit); if (groundedR) { travelR = ((axel.rightWheel.transform.InverseTransformPoint(hit.point).y + axel.rightWheel.radius) * -1) / axel.rightWheel.suspensionDistance; } float antiRollForce = (travelL - travelR) * anitRoll; if (groundedL) { _rb.AddForceAtPosition(axel.leftWheel.transform.up * -antiRollForce, axel.leftWheel.transform.position); } if (groundedR) { _rb.AddForceAtPosition(axel.rightWheel.transform.up * antiRollForce, axel.rightWheel.transform.position); } _grounded |= (groundedL || groundedR); }
private void Deceleration(AxleInfo axleInfo) { axleInfo.leftWheelCollider.brakeTorque = decelerationForce; axleInfo.rightWheelCollider.brakeTorque = decelerationForce; axleInfo.leftWheelCollider.motorTorque = 0; axleInfo.rightWheelCollider.motorTorque = 0; }
private void SetTorque(AxleInfo axel) { if (axel.motor) { axel.leftWheel.motorTorque = _currTorque; axel.rightWheel.motorTorque = _currTorque; } }
private void SetSteering(AxleInfo axel) { if (axel.steering) { axel.leftWheel.steerAngle = _currSteering; axel.rightWheel.steerAngle = _currSteering; } }
public void SetTurningStiffness(AxleInfo axleInfo, float newStiffness) { if (axleInfo.turning) { ModifySidewaysStiffness(axleInfo.leftWheel, newStiffness); ModifySidewaysStiffness(axleInfo.rightWheel, newStiffness); } }
public void ApplyAxleToVisuals(AxleInfo axle, Transform visual) { axle.leftWheel.GetWorldPose(out Vector3 pLeft, out Quaternion rotLeft); axle.rightWheel.GetWorldPose(out Vector3 pRight, out Quaternion rotRight); visual.position = Vector3.Lerp(pLeft, pRight, 0.5f); visual.rotation = Quaternion.Lerp(rotLeft, rotRight, 0.5f); }
private void Steering(AxleInfo axleInfo, float steering) { var speedFactor = (rg.velocity.magnitude * 1.1f) / lowestSteerAtSpeed; currentSteerAngle = Mathf.Lerp(lowSpeedSteerAngel, highSpeedSteerAngel, speedFactor); currentSteerAngle *= steering; axleInfo.leftWheelCollider.steerAngle = currentSteerAngle; axleInfo.rightWheelCollider.steerAngle = currentSteerAngle; }
public void ApplyLocalPositionToVisuals(AxleInfo axleInfo) { axleInfo.leftWheelCollider.GetWorldPose(out Vector3 position, out Quaternion rotation); axleInfo.leftWheelMesh.transform.position = position; axleInfo.leftWheelMesh.transform.rotation = rotation; axleInfo.rightWheelCollider.GetWorldPose(out position, out rotation); axleInfo.rightWheelMesh.transform.position = position; axleInfo.rightWheelMesh.transform.rotation = rotation; }
public void ApplyLocalPositionToVisuals(AxleInfo axleInfo) { Vector3 position; Quaternion rotation; axleInfo.leftWheelCollider.GetWorldPose(out position, out rotation); axleInfo.leftWheelMesh.transform.position = position; axleInfo.rightWheelCollider.GetWorldPose(out position, out rotation); axleInfo.rightWheelMesh.transform.position = position; axleInfo.rightWheelMesh.transform.rotation = rotation * Quaternion.AngleAxis(-90, Vector3.forward) * Quaternion.AngleAxis(-90, Vector3.up); axleInfo.leftWheelMesh.transform.rotation = rotation * Quaternion.AngleAxis(-90, Vector3.forward) * Quaternion.AngleAxis(-90, Vector3.up); }
public void ApplyLocalPositionToVisuals(AxleInfo axleInfo) { if (!useJob) { Vector3 position; Quaternion rotation; axleInfo.leftWheelCollider.GetWorldPose(out position, out rotation); axleInfo.leftWheelMesh.transform.position = position; axleInfo.leftWheelMesh.transform.rotation = rotation; axleInfo.rightWheelCollider.GetWorldPose(out position, out rotation); axleInfo.rightWheelMesh.transform.position = position; axleInfo.rightWheelMesh.transform.rotation = rotation; } else { Transform[] _transforms = new Transform[2]; _transforms[0] = axleInfo.leftWheelMesh.transform; _transforms[1] = axleInfo.rightWheelMesh.transform; var transAccessArray = new TransformAccessArray(_transforms); Vector3[] pos = new Vector3[2]; Quaternion[] rot = new Quaternion[2]; Vector3 position; Quaternion rotation; axleInfo.leftWheelCollider.GetWorldPose(out position, out rotation); pos[0] = position; rot[0] = rotation; axleInfo.rightWheelCollider.GetWorldPose(out position, out rotation); pos[1] = position; rot[1] = rotation; var posArray = new NativeArray <Vector3>(pos, Allocator.TempJob); var rotateArray = new NativeArray <Quaternion>(rot, Allocator.TempJob); ApplyLocalPositionJob job0 = new ApplyLocalPositionJob() { position = posArray, rotation = rotateArray }; JobHandle handle0 = job0.Schedule(transAccessArray); transAccessArray.Dispose(); rotateArray.Dispose(); posArray.Dispose(); } }
private void Acceleration(AxleInfo axleInfo, float motor) { if (motor != 0f) { axleInfo.leftWheelCollider.brakeTorque = 0; // axleInfo.rightWheelCollider.brakeTorque = 0; axleInfo.leftWheelCollider.motorTorque = motor; //axleInfo.rightWheelCollider.motorTorque = motor; } else { Deceleration(axleInfo); } }
public PermitView(Services services, NancyContext context) { User = context.CurrentUser as AuthUser; Hauler = User.ToHaulerInfo(); Company = new CompanyInfo(); Insurance = new InsuranceInfo(); Vehicle = new VehicleInfo(); Truck = new TruckInfo(); Axle = new AxleInfo(); Trailer = new TrailerInfo(); Load = new LoadInfo(); Movement = new MovementInfo(); }
private void Acceleration(AxleInfo axleInfo, float motor) { if (motor != 0f) { axleInfo.leftWheelCollider.brakeTorque = 0; axleInfo.rightWheelCollider.brakeTorque = 0; axleInfo.leftWheelCollider.motorTorque = (Mathf.Sign(motor)) * momentByRpm(axleInfo.leftWheelCollider.rpm); //motor axleInfo.rightWheelCollider.motorTorque = (Mathf.Sign(motor)) * momentByRpm(axleInfo.rightWheelCollider.rpm); //motor } else { Deceleration(axleInfo); } }
// -------------------------------------------------------------------------------------------- // Name : ApplyBrakeTorque // Desc : Applies any braking torque if the controller is braking // and applies engine braking when decelerating // --------------------------------------------------------------------------------------------- private void ApplyBrakeTorque(AxleInfo axleInfo) { bool handBreakOn = Input.GetKey(KeyCode.LeftShift); // Handbrake if (handBreakOn) { axleInfo.leftWheel.brakeTorque = 1000f; axleInfo.rightWheel.brakeTorque = 1000f; } else { axleInfo.leftWheel.brakeTorque = 0.0f; axleInfo.rightWheel.brakeTorque = 0.0f; } }
public void ControlCar() { float motor = currentGear.MaxMotorTorque * Input.GetAxis("Vertical"); float steering = currentGear.MaxSteeringAngle * Input.GetAxis("Horizontal"); //speedText.text = "Speed: " + rb.velocity.magnitude.ToString(); if (rb.velocity.magnitude > currentGear.MinimumSpeed) { motor *= 2.0f; } else { motor *= 0.25f; } if (Input.GetKey(KeyCode.Space)) { //if braking you cannot stear?! Brake(); return; } for (int i = 0; i < axleInfos.Length; i++) { AxleInfo axle = axleInfos[i]; //Adjust steering if (axle.Steering) { axle.LeftWheel.steerAngle = steering; axle.RightWheel.steerAngle = steering; } //Adjust motor power if (axle.Motor) { axle.LeftWheel.motorTorque = motor; axle.RightWheel.motorTorque = motor; axle.LeftWheel.brakeTorque = 0.0f; axle.RightWheel.brakeTorque = 0.0f; } ApplyLocalPositionsToVisuals(axle.LeftWheel); ApplyLocalPositionsToVisuals(axle.RightWheel); } }
public void ApplyLocalPositionToVisuals(AxleInfo axleInfo) { Vector3 position; Quaternion rotation; axleInfo.leftWheelCollider.GetWorldPose(out position, out rotation); Transform leftWheelMesh = axleInfo.leftWheelCollider.transform.GetChild(0); leftWheelMesh.transform.position = position; leftWheelMesh.transform.rotation = axleInfo.leftMeshDefaultRotation * rotation; axleInfo.rightWheelCollider.GetWorldPose(out position, out rotation); Transform rightWheelMesh = axleInfo.rightWheelCollider.transform.GetChild(0); rightWheelMesh.transform.position = position; rightWheelMesh.transform.rotation = axleInfo.rightMeshDefaultRotation * rotation; }
private void Acceleration(AxleInfo axleInfo, float motor) { brakeLights.SetActive(false); if (motor > 0f && currentSpeed <= topSpeed) { axleInfo.leftWheelCollider.brakeTorque = 0; axleInfo.rightWheelCollider.brakeTorque = 0; axleInfo.leftWheelCollider.motorTorque = motor * (int)gearStatus; axleInfo.rightWheelCollider.motorTorque = motor * (int)gearStatus; brakeLights.SetActive(false); } else { Deceleration(axleInfo); //brakeLights.SetActive(true); } }
//[SerializeField] //private Text speedText; //[SerializeField] //private Text gearText; // Start is called before the first frame update void Start() { rb = this.GetComponent <Rigidbody>(); visualWheelOffset = axleInfos[0].LeftWheel.transform.GetChild(0).eulerAngles; currentGear = gears[0]; //speedText.text = "Speed: 0"; //gearText.text = "Gear: 1"; for (int i = 0; i < axleInfos.Length; i++) { AxleInfo axle = axleInfos[i]; axle.RightWheel.ConfigureVehicleSubsteps(20, 12, 16); axle.LeftWheel.ConfigureVehicleSubsteps(20, 12, 16); } }
private void Brake() { for (int i = 0; i < axleInfos.Length; i++) { AxleInfo axle = axleInfos[i]; if (axle.Motor) { axle.LeftWheel.motorTorque = 0.0f; axle.RightWheel.motorTorque = 0.0f; axle.LeftWheel.brakeTorque = brakespeed; axle.RightWheel.brakeTorque = brakespeed; } ApplyLocalPositionsToVisuals(axle.LeftWheel); //Don't think this should be here! ApplyLocalPositionsToVisuals(axle.RightWheel); } }
private void ApplyLocalPositionToVisuals(AxleInfo axleInfo) { WheelCollider leftWheel = axleInfo.leftWheel; WheelCollider rightWheel = axleInfo.rightWheel; Transform leftWheelModel = axleInfo.leftWheelModel; Transform rightWheelModel = axleInfo.rightWheelModel; Vector3 position1, position2; Quaternion rotation1, rotation2; leftWheel.GetWorldPose(out position1, out rotation1); leftWheelModel.position = position1; leftWheelModel.rotation = rotation1; rightWheel.GetWorldPose(out position2, out rotation2); rightWheelModel.position = position2; rightWheelModel.rotation = rotation2; }
void StabilizerBars(float antiRoll, AxleInfo axleInfo) { WheelHit hit; float travelL = 1.0f; float travelR = 1.0f; bool groundedL = axleInfo.leftWheelW.GetGroundHit(out hit); //vvv calculates ratio of compression by getting distance between the center of the wheel and the suspension origin and dividing by suspension distance if (groundedL) { travelL = (-axleInfo.leftWheelW.transform.InverseTransformPoint(hit.point).y - axleInfo.leftWheelW.radius) / axleInfo.leftWheelW.suspensionDistance; } else { travelL = 1.0f; } bool groundedR = axleInfo.rightWheelW.GetGroundHit(out hit); if (groundedR) { travelR = (-axleInfo.rightWheelW.transform.InverseTransformPoint(hit.point).y - axleInfo.leftWheelW.radius) / axleInfo.rightWheelW.suspensionDistance; } else { travelR = 1.0f; } float antiRollForce = (travelL - travelR) * antiRoll; if (groundedL) { rb.AddForceAtPosition(axleInfo.leftWheelW.transform.up * -antiRollForce, axleInfo.leftWheelW.transform.position); } if (groundedR) { rb.AddForceAtPosition(axleInfo.rightWheelW.transform.up * antiRollForce, axleInfo.rightWheelW.transform.position); } }
private void handleAntiRoll(AxleInfo axleInfo) { WheelCollider WheelL = axleInfo.leftWheel; WheelCollider WheelR = axleInfo.rightWheel; WheelHit hit; float travelL = 1.0f; float travelR = 1.0f; float AntiRoll = 5000.0f; bool groundedL = WheelL.GetGroundHit(out hit); if (groundedL) { travelL = (-WheelL.transform.InverseTransformPoint(hit.point).y - WheelL.radius) / WheelL.suspensionDistance; } bool groundedR = WheelR.GetGroundHit(out hit); if (groundedR) { travelR = (-WheelR.transform.InverseTransformPoint(hit.point).y - WheelR.radius) / WheelR.suspensionDistance; } float antiRollForce = (travelL - travelR) * AntiRoll; if (groundedL) { rigidbody.AddForceAtPosition(WheelL.transform.up * -antiRollForce, WheelL.transform.position); } if (groundedR) { rigidbody.AddForceAtPosition(WheelR.transform.up * antiRollForce, WheelR.transform.position); } }
private void addCarControllerScript() { SimpleCarController simpleCarController = machineGameObject.AddComponent <SimpleCarController>(); AxleInfo frontAxle = new AxleInfo(); frontAxle.wheels = new WheelCollider[] { wheels[0].GetComponent <WheelCollider>(), wheels[1].GetComponent <WheelCollider>() }; frontAxle.motor = true; frontAxle.steering = true; AxleInfo rearAxle = new AxleInfo(); rearAxle.wheels = new WheelCollider[] { wheels[2].GetComponent <WheelCollider>(), wheels[3].GetComponent <WheelCollider>() }; rearAxle.motor = true; rearAxle.steering = false; simpleCarController.axleInfos = new List <AxleInfo>() { frontAxle, rearAxle };; simpleCarController.maxMotorTorque = DEFAULT_MOTOR_TORQUE; simpleCarController.maxSteeringAngle = 50; simpleCarController.verticalAxisInputName = verticalAxisInputName; simpleCarController.horizontalAxisInputName = horizontalAxisInputName; }
private void Brake(AxleInfo axleInfo) { axleInfo.leftWheelCollider.brakeTorque = brakeTorque; // axleInfo.rightWheelCollider.brakeTorque = brakeTorque; }
private void Steering(AxleInfo axleInfo, float steering) { axleInfo.leftWheelCollider.steerAngle = steering; //axleInfo.rightWheelCollider.steerAngle = steering; }
private void Brake(AxleInfo axleInfo) { axleInfo.leftWheelCollider.brakeTorque = brakeTorque; axleInfo.rightWheelCollider.brakeTorque = brakeTorque; brakeLights.SetActive(true); }
private void Deceleration(AxleInfo axleInfo) { axleInfo.leftWheel.brakeTorque = decelerationForce; axleInfo.rightWheel.brakeTorque = decelerationForce; }
void OnGUI() { CarMesh = EditorGUILayout.ObjectField("CarMesh", CarMesh, typeof(GameObject), true); Root = EditorGUILayout.ObjectField("Root", Root, typeof(GameObject), true); IsFrontWheel = EditorGUILayout.Toggle("Front Wheel", IsFrontWheel); if (GUILayout.Button("Rig Body")) { CarMesh = Selection.activeGameObject; Object baseCar = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Vehicles/PrefabPieces/VehicleRoot.prefab", typeof(GameObject)); Root = Instantiate(baseCar, (CarMesh as GameObject).transform.position, (CarMesh as GameObject).transform.rotation); (CarMesh as GameObject).transform.parent = (Root as GameObject).transform; } if (GUILayout.Button("Rig Wheel")) { GameObject WheelMesh = Selection.activeGameObject; // Front Wheel if (IsFrontWheel) { // Create Wheel Collider Object Wheel = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Vehicles/PrefabPieces/FrontWheel.prefab", typeof(GameObject)); GameObject wheelCollider = Instantiate(Wheel, WheelMesh.transform.position, WheelMesh.transform.rotation) as GameObject; // Set parent/child relationships in hierarchy wheelCollider.transform.parent = WheelMesh.transform.parent; WheelMesh.transform.parent = wheelCollider.transform; AxleInfo ax = (Root as GameObject).GetComponent <BasicVehicle>().axleInfos[0]; if (ax.leftWheel == null) { ax.leftWheel = wheelCollider.GetComponent <WheelCollider>(); } else { ax.rightWheel = wheelCollider.GetComponent <WheelCollider>(); } } // Back Wheel else { // Create Wheel Collider Object Wheel = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Vehicles/PrefabPieces/RearWheel.prefab", typeof(GameObject)); GameObject wheelCollider = Instantiate(Wheel, WheelMesh.transform.position, WheelMesh.transform.rotation) as GameObject; // Set parent/child relationships in hierarchy wheelCollider.transform.parent = WheelMesh.transform.parent; WheelMesh.transform.parent = wheelCollider.transform; AxleInfo ax = (Root as GameObject).GetComponent <BasicVehicle>().axleInfos[1]; if (ax.leftWheel == null) { ax.leftWheel = wheelCollider.GetComponent <WheelCollider>(); } else { ax.rightWheel = wheelCollider.GetComponent <WheelCollider>(); } } } GUILayout.Label(text); }
public void CalculateAndApplySteering(AxleInfo axleInfo, float speed, float maxSteeringAngle) { axleInfo.leftWheel.steerAngle = 30 * speed; axleInfo.rightWheel.steerAngle = 30 * speed; }