Untitled
unknown
plain_text
3 years ago
2.0 kB
9
Indexable
using UnityEngine; public class simpleCarController : MonoBehaviour { private float horizontalInput; private float verticalInput; private float steeringAngle; public WheelCollider frontLeftWheel, frontRightWheel; public WheelCollider rearLeftWheel, rearRightWheel; public Transform frontLeftTransform, frontRightTransform; public Transform rearLeftTransform, rearRightTransform; public Vector3 centerOfMass; public float maxSteerAngle = 30; public float motorForce = 1000; public void GetInput() { horizontalInput = Input.GetAxis("Horizontal"); verticalInput = Input.GetAxis("Vertical"); } private void Steer(){ steeringAngle = maxSteerAngle * horizontalInput; frontLeftWheel.steerAngle = steeringAngle; frontRightWheel.steerAngle = steeringAngle; } private void Accelerate(){ frontLeftWheel.motorTorque = verticalInput * motorForce; frontRightWheel.motorTorque = verticalInput * motorForce; } private void UpdateWheelPoses(){ UpdateWheelPose(frontLeftWheel,frontLeftTransform); UpdateWheelPose(frontRightWheel,frontRightTransform); UpdateWheelPose(rearLeftWheel,rearLeftTransform); UpdateWheelPose(rearRightWheel,rearRightTransform); } private void UpdateWheelPose(WheelCollider collider, Transform transform){ Vector3 pos = transform.position; Quaternion quat = transform.rotation; collider.GetWorldPose(out pos, out quat); transform.position = pos; transform.rotation = quat; } void Start() { Rigidbody rb = GetComponent<Rigidbody>(); rb.centerOfMass = centerOfMass; } // Update is called once per frame void FixedUpdate() { GetInput(); Steer(); Accelerate(); UpdateWheelPoses(); } }
Editor is loading...