WEBLEB
Home
AI Editor
Login
Pro
English
English
Français
Español
HTML
CSS
JS
using UnityEngine; public class TruckController : MonoBehaviour { [Header("Wheel Colliders")] public WheelCollider frontLeftWheel; public WheelCollider frontRightWheel; public WheelCollider rearLeftWheel; public WheelCollider rearRightWheel; [Header("Wheel Meshes")] public Transform frontLeftMesh; public Transform frontRightMesh; public Transform rearLeftMesh; public Transform rearRightMesh; [Header("Truck Settings")] public float motorTorque = 1500f; public float brakeForce = 3000f; public float maxSteerAngle = 25f; public float maxSpeed = 80f; // km/h approx public float centerOfMassOffsetY = -0.5f; private Rigidbody rb; private float currentMotor; private float currentBrake; private float currentSteer; void Start() { rb = GetComponent
(); if (rb != null) { Vector3 com = rb.centerOfMass; com.y += centerOfMassOffsetY; rb.centerOfMass = com; } } void Update() { HandleInput(); LimitSpeed(); } void FixedUpdate() { ApplyMotor(); ApplySteering(); UpdateWheelVisuals(frontLeftWheel, frontLeftMesh); UpdateWheelVisuals(frontRightWheel, frontRightMesh); UpdateWheelVisuals(rearLeftWheel, rearLeftMesh); UpdateWheelVisuals(rearRightWheel, rearRightMesh); } void HandleInput() { float vertical = Input.GetAxis("Vertical"); // W/S or Up/Down float horizontal = Input.GetAxis("Horizontal"); // A/D or Left/Right bool isBraking = Input.GetKey(KeyCode.Space); currentMotor = vertical * motorTorque; currentSteer = horizontal * maxSteerAngle; currentBrake = isBraking ? brakeForce : 0f; } void ApplyMotor() { // Rear-wheel drive rearLeftWheel.motorTorque = currentMotor; rearRightWheel.motorTorque = currentMotor; rearLeftWheel.brakeTorque = currentBrake; rearRightWheel.brakeTorque = currentBrake; frontLeftWheel.brakeTorque = currentBrake; frontRightWheel.brakeTorque = currentBrake; } void ApplySteering() { frontLeftWheel.steerAngle = currentSteer; frontRightWheel.steerAngle = currentSteer; } void LimitSpeed() { if (rb == null) return; float speed = rb.velocity.magnitude * 3.6f; // m/s to km/h if (speed > maxSpeed) { rb.velocity = rb.velocity.normalized * (maxSpeed / 3.6f); } } void UpdateWheelVisuals(WheelCollider col, Transform mesh) { if (col == null || mesh == null) return; Vector3 pos; Quaternion rot; col.GetWorldPose(out pos, out rot); mesh.position = pos; mesh.rotation = rot; } } using UnityEngine; public class CameraFollow : MonoBehaviour { public Transform target; public Vector3 offset = new Vector3(0f, 5f, -10f); public float followSpeed = 5f; public float rotateSpeed = 5f; void LateUpdate() { if (target == null) return; // Desired position Vector3 desiredPosition = target.TransformPoint(offset); transform.position = Vector3.Lerp(transform.position, desiredPosition, followSpeed * Time.deltaTime); // Look at truck Quaternion desiredRotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, rotateSpeed * Time.deltaTime); } }
Preview
Open Advanced Editor
Publish Code
Full Screen
HTML
CSS
JS
using UnityEngine; public class TruckController : MonoBehaviour { [Header("Wheel Colliders")] public WheelCollider frontLeftWheel; public WheelCollider frontRightWheel; public WheelCollider rearLeftWheel; public WheelCollider rearRightWheel; [Header("Wheel Meshes")] public Transform frontLeftMesh; public Transform frontRightMesh; public Transform rearLeftMesh; public Transform rearRightMesh; [Header("Truck Settings")] public float motorTorque = 1500f; public float brakeForce = 3000f; public float maxSteerAngle = 25f; public float maxSpeed = 80f; // km/h approx public float centerOfMassOffsetY = -0.5f; private Rigidbody rb; private float currentMotor; private float currentBrake; private float currentSteer; void Start() { rb = GetComponent
(); if (rb != null) { Vector3 com = rb.centerOfMass; com.y += centerOfMassOffsetY; rb.centerOfMass = com; } } void Update() { HandleInput(); LimitSpeed(); } void FixedUpdate() { ApplyMotor(); ApplySteering(); UpdateWheelVisuals(frontLeftWheel, frontLeftMesh); UpdateWheelVisuals(frontRightWheel, frontRightMesh); UpdateWheelVisuals(rearLeftWheel, rearLeftMesh); UpdateWheelVisuals(rearRightWheel, rearRightMesh); } void HandleInput() { float vertical = Input.GetAxis("Vertical"); // W/S or Up/Down float horizontal = Input.GetAxis("Horizontal"); // A/D or Left/Right bool isBraking = Input.GetKey(KeyCode.Space); currentMotor = vertical * motorTorque; currentSteer = horizontal * maxSteerAngle; currentBrake = isBraking ? brakeForce : 0f; } void ApplyMotor() { // Rear-wheel drive rearLeftWheel.motorTorque = currentMotor; rearRightWheel.motorTorque = currentMotor; rearLeftWheel.brakeTorque = currentBrake; rearRightWheel.brakeTorque = currentBrake; frontLeftWheel.brakeTorque = currentBrake; frontRightWheel.brakeTorque = currentBrake; } void ApplySteering() { frontLeftWheel.steerAngle = currentSteer; frontRightWheel.steerAngle = currentSteer; } void LimitSpeed() { if (rb == null) return; float speed = rb.velocity.magnitude * 3.6f; // m/s to km/h if (speed > maxSpeed) { rb.velocity = rb.velocity.normalized * (maxSpeed / 3.6f); } } void UpdateWheelVisuals(WheelCollider col, Transform mesh) { if (col == null || mesh == null) return; Vector3 pos; Quaternion rot; col.GetWorldPose(out pos, out rot); mesh.position = pos; mesh.rotation = rot; } } using UnityEngine; public class CameraFollow : MonoBehaviour { public Transform target; public Vector3 offset = new Vector3(0f, 5f, -10f); public float followSpeed = 5f; public float rotateSpeed = 5f; void LateUpdate() { if (target == null) return; // Desired position Vector3 desiredPosition = target.TransformPoint(offset); transform.position = Vector3.Lerp(transform.position, desiredPosition, followSpeed * Time.deltaTime); // Look at truck Quaternion desiredRotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, rotateSpeed * Time.deltaTime); } }
Preview
Validating your code, please wait...