Being not very good at scripting, I have come up with a basic script for entering and exiting a car, after a really long time of searching for one.
Entering car:
var instructions : GUIText;
var FPSplayer : Transform;
var car : Transform;
var CarReplacement : Transform;
var RepSound : AudioClip;
function Start () {
instructions.enabled = false;
}
function OnTriggerStay (col : Collider) {
if (Input.GetKeyUp("e")) {
FPSplayer.parent = car;
FPSplayer.active = false;
gameObject.active = false;
car.parent = CarReplacement;
// Play a replacement audio clip
if (RepSound)
AudioSource.PlayClipAtPoint(RepSound, transform.position);
// Replace ourselves with the controllable car
if (CarReplacement) {
var replacement : Transform = Instantiate(CarReplacement, transform.position, transform.rotation);
// Copy position rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, replacement);
}
instructions.enabled = true;
}
}
function OnTriggerExit () {
instructions.enabled = false;
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) {
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
Exiting car:
var instructions : GUIText;
var FPSplayer : Transform;
var car : Transform;
var CarReplacement : Transform;
var RepSound : AudioClip;
function Start () {
instructions.enabled = false;
}
function OnTriggerStay (FPSplayer) {
if (Input.GetKeyUp("e")) {
FPSplayer.parent = null;
FPSplayer.active = true;
Destroy(gameObject);
// Play a replacement audio clip
if (RepSound)
AudioSource.PlayClipAtPoint(RepSound, transform.position);
// Replace ourselves with the controllable car
if (CarReplacement) {
var replacement : Transform = Instantiate(CarReplacement, transform.position, transform.rotation);
// Copy position rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, replacement);
}
instructions.enabled = true;
}
}
function OnTriggerExit () {
instructions.enabled = false;
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) {
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
Here’s how it’s supposed to work: I go next to the car (where the trigger is) then the first person controller is disabled (and the is put as the child of the controllable car), the car is deleted, and then the controllable car is spawned in the same position so that you can drive around.
The get out car script will re-enable the firstperson player, delete the controllable car, and spawn a non-controllable car, so that you can walk around in first person again.
Here are the problems:
-It’s supposed to only work when the player stays in the trigger AND THEN PRESSES E. But instead it works WHENEVER the player presses E ![]()
-Instructions are not enabled when the player enters the trigger ![]()
-I am pretty sure that the get out car script does not work, but I am still trying!
-The enter script disables the car, but not it’s children???
-Also, how do I make it where an OnTriggerEnter only reacts when a certain gameObject enters it?
P.S., the enter script is attached to the car, and the exit script is attached to the controllable car.
If anyone can help in any way, Thanks.