Advertisement
Guest User

Simple car movement

a guest
Feb 2nd, 2011
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. public class CWheel
  7. {
  8.     public CWheel(Transform wheel, Boolean frontWheels)
  9.     {
  10.         Self = wheel;
  11.         Visual = Self.Find("WheelGraphic");
  12.         Collider = Self.gameObject.GetComponent<WheelCollider>();
  13.         IsFrontWheel = frontWheels;
  14.     }
  15.  
  16.     public void PerformUpdate(float forwardThrust, float currentSteer)
  17.     {
  18.         // Set torque on the rear wheels
  19.         if (!IsFrontWheel)
  20.             Collider.motorTorque = forwardThrust;
  21.  
  22.         // Rotate front wheels
  23.         if (IsFrontWheel)
  24.         {
  25.             Quaternion steerRot = Quaternion.Euler(0, currentSteer, 0);
  26.             Self.localRotation = steerRot;
  27.         }
  28.  
  29.         // Perform wheel graphics update
  30.         float rps = Collider.rpm / 60.0f;
  31.         float angle = 360 * rps * Time.fixedDeltaTime;
  32.         Visual.Rotate(-Vector3.up * angle);
  33.     }
  34.  
  35.     public Transform Self;
  36.     public Transform Visual;
  37.     public WheelCollider Collider;
  38.     public Boolean IsFrontWheel;
  39.  
  40. }
  41.  
  42.  
  43. public class TheCarEngine : MonoBehaviour
  44. {
  45.  
  46.     public float EnginePower = 50;
  47.     public float MaxSteerAngle = 30;
  48.  
  49.     public Transform WheelFL;
  50.     public Transform WheelFR;
  51.     public Transform WheelRL;
  52.     public Transform WheelRR;
  53.  
  54.     public Transform CenterOfMass;
  55.     public float ForceDown = 3;
  56.  
  57.     private float thrust = 0;
  58.     private float steer = 0;
  59.  
  60.     private float angle = 0;
  61.  
  62.  
  63.     private List<CWheel> Wheels = new List<CWheel>();
  64.  
  65.     // Use this for initialization
  66.     void Start()
  67.     {
  68.         Boolean[] frontWheelsFlag = new Boolean[] { true, true, false, false };
  69.         Transform[] wheelTransforms = new Transform[] { WheelFL, WheelFR, WheelRL, WheelRR };
  70.  
  71.         for (int i = 0; i < 4; i++)
  72.             Wheels.Add(new CWheel(wheelTransforms[i], frontWheelsFlag[i]));
  73.  
  74.         rigidbody.centerOfMass = CenterOfMass.position;
  75.     }
  76.  
  77.     // Update is called once per frame
  78.     void Update()
  79.     {
  80.         ReadInput();
  81.     }
  82.  
  83.     void ReadInput()
  84.     {
  85.         thrust = Input.GetAxis("Vertical");
  86.         steer = Input.GetAxis("Horizontal");
  87.  
  88.     }
  89.  
  90.     void FixedUpdate()
  91.     {
  92.         var forwardThrust = thrust * EnginePower;
  93.         float currentSteer = steer * MaxSteerAngle;
  94.  
  95.         foreach (CWheel wheel in Wheels)
  96.             wheel.PerformUpdate(forwardThrust, currentSteer);
  97.  
  98.         // try to push down
  99.         foreach (CWheel wheel in Wheels)
  100.         {
  101.             rigidbody.AddForceAtPosition(Vector3.down*ForceDown, wheel.Self.position, ForceMode.Force);
  102.         }
  103.  
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement