Created
May 28, 2016 09:51
-
-
Save predominant/c0e33a8396f1890985d894b77ba03157 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
// ReSharper disable once CheckNamespace | |
namespace MotherTruckers.Vehicle | |
{ | |
[Serializable] | |
public class Axle | |
{ | |
public WheelCollider LeftWheel; | |
public WheelCollider RightWheel; | |
public bool IsMotor; | |
public bool IsSteering; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
using BeardedManStudios.Network; | |
// ReSharper disable once CheckNamespace | |
namespace MotherTruckers.Vehicle | |
{ | |
[RequireComponent(typeof(Rigidbody))] | |
public class VehicleController : NetworkedMonoBehavior | |
{ | |
public bool Controllable = true; | |
public List<Axle> Axles; | |
public float MaxTorque = 400f; | |
public float MaxAngle = 30f; | |
public float SteeringSpeed = 1f; | |
public bool UseCustomCenterOfMass = false; | |
public Transform CenterOfMass; | |
private float TargetSteeringAngle = 0f; | |
private Rigidbody _rigidbody; | |
private Transform _transform; | |
public void Awake() | |
{ | |
this._rigidbody = this.GetComponent<Rigidbody>(); | |
this._transform = this.GetComponent<Transform>(); | |
if (this.UseCustomCenterOfMass) | |
this._rigidbody.centerOfMass = this.CenterOfMass.localPosition; | |
} | |
public void FixedUpdate() | |
{ | |
if (this.IsOwner) | |
this.ProcessControls(); | |
if (!this.IsOwner) | |
this._rigidbody.isKinematic = true; | |
} | |
public void Update() | |
{ | |
if (this.IsOwner) | |
this._transform.FindChild("Cameras").gameObject.SetActive(true); | |
foreach (var axle in this.Axles) | |
{ | |
this.VisualPosition(axle.LeftWheel); | |
this.VisualPosition(axle.RightWheel); | |
} | |
} | |
protected void ProcessControls() | |
{ | |
if (!this.Controllable) | |
return; | |
var torque = this.MaxTorque * Input.GetAxis("Vertical"); | |
var steering = this.MaxAngle * Input.GetAxis("Horizontal"); | |
foreach (var axle in this.Axles) | |
{ | |
if (axle.IsSteering) | |
this.Steering(axle, steering); | |
if (axle.IsMotor) | |
this.Torque(axle, torque); | |
} | |
} | |
protected void Steering(Axle axle, float amount) | |
{ | |
axle.LeftWheel.steerAngle = amount; | |
axle.RightWheel.steerAngle = amount; | |
} | |
protected void Torque(Axle axle, float amount) | |
{ | |
axle.LeftWheel.motorTorque = amount; | |
axle.RightWheel.motorTorque = amount; | |
} | |
protected void VisualPosition(WheelCollider c) | |
{ | |
Transform wheelTransform = c.gameObject.GetComponent<Transform>().GetChild(0); | |
Vector3 pos; | |
Quaternion rot; | |
c.GetWorldPose(out pos, out rot); | |
wheelTransform.position = pos; | |
wheelTransform.rotation = rot; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment