Untitled
unknown
plain_text
2 years ago
2.0 kB
6
Indexable
using UnityEngine; public class CarController : MonoBehaviour { [SerializeField] WheelCollider frontRight; [SerializeField] WheelCollider frontLeft; [SerializeField] WheelCollider backRight; [SerializeField] WheelCollider backLeft; [SerializeField] Transform frontRightT; [SerializeField] Transform frontLeftT; [SerializeField] Transform backRightT; [SerializeField] Transform backLeftT; public float acceleration = 500f; public float brakingForce = 300f; public float maxSteerAngle = 30f; private float currentAcceleration = 0f; private float currentBrakeForce = 0f; private float currentSteerAngle = 0f; private void FixedUpdate() { //steer currentSteerAngle = maxSteerAngle * Input.GetAxis("Horizontal"); frontLeft.steerAngle = currentSteerAngle; frontRight.steerAngle = currentSteerAngle; //Forward currentAcceleration = acceleration * Input.GetAxis("Vertical"); //Space if (Input.GetKey(KeyCode.S)) { currentBrakeForce = brakingForce; } else { currentBrakeForce = 0f; } //Acceleration to back wheels backRight.motorTorque = currentAcceleration; backLeft.motorTorque = currentAcceleration; frontRight.brakeTorque = currentBrakeForce; frontLeft.brakeTorque = currentBrakeForce; backRight.brakeTorque = currentBrakeForce; backLeft.brakeTorque = currentBrakeForce; //Update of wheels UpdateWheel(frontLeft, frontLeftT); UpdateWheel(frontRight, frontRightT); UpdateWheel(backLeft, backLeftT); UpdateWheel(backRight, backRightT); } void UpdateWheel(WheelCollider col, Transform trans) { //Get wheel stats Vector3 position; Quaternion rotation; col.GetWorldPose(out position, out rotation); trans.position = position; trans.rotation = rotation; } }
Editor is loading...