SrcWheel.cs
unknown
plain_text
2 years ago
2.5 kB
3
Indexable
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScrWheel : MonoBehaviour { private Rigidbody rb; public bool wheelFrontLeft; public bool wheelFrontRight; public bool wheelRearLeft; public bool wheelRearRight; [Header("Suspension")] public float restLength; public float springTravel; public float springStiffness; public float damperStiffness; private float minLength; private float maxLength; private float lastLength; private float springLength; private float springVelocity; private float springForce; private float damperForce; [Header("Wheel")] public float steerAngle; public float steerTime; private Vector3 suspensionForce; private Vector3 wheelVelocityLS; // Local Space private float Fx; private float Fy; private float wheelAngle; [Header("Wheel")] public float wheelRadius; void Start() { rb = transform.root.GetComponent<Rigidbody>(); minLength = restLength - springTravel; maxLength = restLength + springTravel; } void Update() { wheelAngle = Mathf.Lerp(wheelAngle, steerAngle, steerTime * Time.deltaTime); //transform.localRotation = Quaternion.Euler(transform.localRotation.x, transform.localRotation.y + steerAngle, transform.localRotation.z); transform.localRotation = Quaternion.Euler(Vector3.up * wheelAngle); Debug.DrawRay(transform.position, -transform.up * (springLength + wheelRadius), Color.green); } void FixedUpdate() { RaycastHit hit = new RaycastHit(); if (Physics.Raycast(transform.position, -transform.up, out hit, maxLength + wheelRadius)) { lastLength = springLength; springLength = hit.distance - wheelRadius; springLength = Mathf.Clamp(springLength, minLength, maxLength); springVelocity = (lastLength - springLength) / Time.fixedDeltaTime; springForce = springStiffness * (restLength - springLength); damperForce = damperStiffness * springVelocity; suspensionForce = (springForce + damperForce) * transform.up; wheelVelocityLS = transform.InverseTransformDirection(rb.GetPointVelocity(hit.point)); Fx = Input.GetAxis("Vertical") * 0.5f * springForce; Fy = wheelVelocityLS.x * springForce; rb.AddForceAtPosition(suspensionForce + (Fx * transform.forward) + (Fy * -transform.right), hit.point); } } }
Editor is loading...