Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Wheel : MonoBehaviour
- {
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- public bool Steering = true;
- public bool active = true;
- public float SpringLength = 0.5f;
- public float Velocity = 0;
- public float Strength = 50;
- public float DampeningMultiplier = 2f;
- public float SteerAngle = 0;
- public float MaxSteerAngle = 0;
- public float MaxSpeed = 60;
- public float friction = 0.9f;
- public bool onGround = false;
- public WheelCurve wc;
- public Vector2 Input = Vector2.zero;
- public Transform VisualWheel;
- public float startAngle = 0;
- public bool crashed = false;
- private float visualRotationAngle = 0;
- private float visualStartY = 0;
- private float vwheelpos = 0;
- private float wheeldistance = 0;
- private float WheelRadius = 0.5f;
- private Rigidbody carRB;
- private float suslerp = 0;
- private float mult = 1;
- private float oldvelo = 0;
- public float GetSpeed()
- {
- return Vector3.Dot(carRB.transform.forward,carRB.linearVelocity);
- }
- public float getSteeringAngle()
- {
- return 6 * Input.x;
- }
- void Start()
- {
- carRB = transform.parent.GetComponent<Rigidbody>();
- if (VisualWheel != null)
- {
- WheelRadius = VisualWheel.GetComponent<Renderer>().bounds.size.y;
- visualStartY = VisualWheel.localPosition.y;
- startAngle = VisualWheel.localRotation.eulerAngles.y;
- SpringLength += WheelRadius / 2;
- }
- }
- // Update is called once per frame
- void FixedUpdate()
- {
- RaycastHit hit;
- wheeldistance = SpringLength;
- if (Physics.Raycast(transform.position, transform.up * -1, out hit, SpringLength))
- {
- Vector3 WheelWorldVel = carRB.GetPointVelocity(transform.position);
- float vel = Vector3.Dot(transform.up, WheelWorldVel);
- float CarSpeed = GetSpeed();
- if (Mathf.Abs(Input.x) > 0.1f && Steering == true)
- {
- transform.localRotation = Quaternion.Euler(0, getSteeringAngle(), 0);
- }
- else
- {
- transform.localRotation = Quaternion.Euler(0, Mathf.Lerp(transform.localRotation.eulerAngles.y, 0, 1), 0);
- }
- if (Mathf.Abs(Input.y) > 0.1f)
- {
- float NormalizedSpeed = Mathf.Clamp01(Mathf.Abs(CarSpeed * carRB.mass) / (MaxSpeed / carRB.mass));
- float AvailableTorque = NormalizedSpeed * Input.y;
- carRB.AddForceAtPosition(transform.forward * (MaxSpeed / 8) * AvailableTorque * (carRB.mass), transform.position);
- }
- float steeringvel = Vector3.Dot(transform.right, WheelWorldVel);
- float desiredVelChange = -steeringvel * friction;
- float desiredAccel = desiredVelChange / Time.fixedDeltaTime;
- if (VisualWheel != null)
- {
- if (Mathf.Abs(desiredAccel) > 300 && onGround == true)
- {
- VisualWheel.Find("Smoke").GetComponent<ParticleSystem>().Play();
- }
- else
- {
- VisualWheel.Find("Smoke").GetComponent<ParticleSystem>().Stop();
- }
- }
- //carRB.AddForceAtPosition(transform.right * (-oldvelo * 0.5f), transform.position);
- oldvelo = (carRB.mass * 0.025f) * desiredAccel;
- carRB.AddForceAtPosition(transform.right * oldvelo, transform.position);
- Debug.DrawLine(transform.position, transform.position + (transform.right * desiredAccel), Color.white, 0f, false);
- Velocity = ((SpringLength - hit.distance) * (Strength)) - (vel * (DampeningMultiplier));
- Debug.DrawLine(transform.position, transform.position + (transform.up * Velocity), Color.white, 0f, false);
- carRB.AddForceAtPosition(transform.up * Velocity * carRB.mass * (Time.fixedDeltaTime * 60), transform.position);
- onGround = true;
- wheeldistance = hit.distance;
- }
- else
- {
- onGround = false;
- if (VisualWheel != null)
- {
- VisualWheel.Find("Smoke").GetComponent<ParticleSystem>().Stop();
- }
- }
- if (VisualWheel != null)
- {
- vwheelpos = VisualWheel.localPosition.y;
- suslerp = 0;
- }
- }
- void Update()
- {
- if (onGround == false)
- {
- mult = Mathf.Max(mult - (Time.deltaTime * 3), Mathf.Max(Mathf.Abs(Input.y),0));
- }
- else
- {
- mult = 1;
- }
- visualRotationAngle -= GetSpeed() * mult * (Time.deltaTime * 120);
- if (VisualWheel != null)
- {
- if (Steering == true)
- {
- VisualWheel.localRotation = Quaternion.Euler(0, Mathf.Lerp(VisualWheel.localRotation.eulerAngles.y, (Input.x * 30) + startAngle, Time.deltaTime * 6), visualRotationAngle);
- }
- else
- {
- VisualWheel.localRotation = Quaternion.Euler(0, startAngle, visualRotationAngle);
- }
- suslerp = Mathf.Min(suslerp + (Time.deltaTime / Time.fixedDeltaTime), 1);
- VisualWheel.localPosition = new Vector3(VisualWheel.localPosition.x, Mathf.Lerp(vwheelpos, visualStartY - (wheeldistance - (WheelRadius / 2)), suslerp), VisualWheel.localPosition.z);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment