I’m back with something useful! A quick little script that allows your player to get in and out of vehicles. I’ve been kind of obsessed with it since I got it working… I can’t stop flying around in my little helicopter!
//Javascript
#pragma strict
var sitPosition: Vector3;
var sitAngle: Vector3;
var vehicleType: vehicleTypes;
var enterDistance = 5.000;
var exitDistance = 7.500;
private var vehicleActive= false;
private var user: Transform;
private var enterPos: Vector3;
var heliSpeed: float = 5.0;
var afterburnerPower: float = 10.0;
var heliTurn: float = 5.0;
var heliActiveDrag: float = 1.0;
var heliActiveAngDrag: float = 2.0;
enum vehicleTypes {
Car = 0, //not implemented yet
Heli = 1
}
function Start () {
}
function OnTriggerStay (other: Collider) {
if (Vector3.Distance(transform.position, gameObject.FindWithTag("Player").transform.position) <= enterDistance Input.GetKeyDown(KeyCode.H)) {
vehicleActive = true;
}
if (vehicleActive) {
var found = other.gameObject.FindWithTag("Player");
user = found.transform;
Invoke("GetUserPos", 0);
user.parent = this.transform;
found.GetComponent(IdleRunJumpJS).enabled = false;
user.localEulerAngles = sitAngle;
user.GetComponent(CharacterController).enabled = false;
if (Input.GetKeyDown(KeyCode.N)) {
vehicleActive = false;
}
}
else if (!vehicleActive) {
Invoke("Exit", 0);
}
}
function FixedUpdate () {
if (vehicleType == 1 vehicleActive) {
rigidbody.useGravity = false;
rigidbody.isKinematic = false;
var afterburner = afterburnerPower*Input.GetAxis("Afterburner")*rigidbody.mass;
rigidbody.AddRelativeForce(heliSpeed*Input.GetAxis("Horizontal")*rigidbody.mass, heliSpeed*Input.GetAxis("Altitude")*rigidbody.mass, heliSpeed*Input.GetAxis("Vertical")*rigidbody.mass+afterburner);
rigidbody.AddRelativeTorque(0, heliTurn*Input.GetAxis("Roll")*rigidbody.mass, 0);
transform.eulerAngles = Vector3(Mathf.LerpAngle(transform.eulerAngles.x, 0, 0.15), transform.eulerAngles.y, Mathf.LerpAngle(transform.eulerAngles.x, 0, 0.15));
rigidbody.drag = heliActiveDrag;
rigidbody.angularDrag = heliActiveAngDrag;
}
else if (!vehicleActive) {
rigidbody.useGravity = true;
rigidbody.drag = 0.01;
rigidbody.angularDrag = 0.05;
}
}
function GetUserPos () {
enterPos = user.localPosition;
user.localPosition = sitPosition;
}
function Exit () {
user.localPosition = exitDistance*transform.right;
user.eulerAngles = Vector3(0, user.eulerAngles.y, 0);
user.parent = null;
enterPos = Vector3(0,0,0);
user.GetComponent(CharacterController).enabled = true;
user.GetComponent(IdleRunJumpJS).enabled = true;
user = null;
}
It still needs some improvement and implementation of other vehicles but it works nicely so far. Any feedback? I’m working on minimizing code by removing useless bits, so if you see something that doesn’t appear to be doing anything go ahead and comment it out. If you get the same functionality, let me know what you removed and I’ll confirm and update the code above.
I also want to release this on the asset store so I can get some money (I really need it!), let me know what you think a good price would be. It’s still a work in progress so you can change your decision as the code develops. I’m thinking $5 or below, once everything is optimized and implemented… ![]()
Edit: I also got the car implementation working. It currently uses a remote enable/disable system, so if you accelerate forward and then leave the car keeps going. It’s interesting, but a bit annoying at times, so I’ll work on condensing it all into one script. Currently it uses Matajek’s car script, so I’ll talk to him before releasing that code.
Plans:
Get a video demonstrating the how it works
Make the in/out keys into one key
Improve exiting the vehicles (to prevent clipping through terrain/buildings)*
Improve helicopter physics**
*I may need some assistance setting up the raycast system for exiting, so feel free to mess around with that.
**Will require research, so that’ll come last



