Entering/Exiting Vehicle 90% complete

I have put together a working enter & exit system for a car (or any other land based vehicle). I have worked quite hard to piece this system together as I noticed there’s many unity questions about this particular system, but few specific tutorials or examples. I have supplied mine below. Please feel free to use it however you like :slight_smile:

NOTE: Make sure after you import the package, you create a new tag “MMM” or it won’t work. Use Enter/Return to enter the car and Space to exit. Textures weren’t included to minimize file size.


I would appreciate some help/advice regarding three specific additions I’d like to make to this system:

(1) How can I make the character invisible once it enters the vehicle and have it’s collider disabled etc?

(2) If I add additional cameras to the car (eg. Dashboard view, Bumper view etc) how could I switch between them, without switching to the players cameras, and vice versa?

(3) How difficult would it be to convert the current two camera system to a one camera system? Since if for example I have a mere 5 cars in the scene, that will be a difference of having either 5 or 10 cameras to deal with.


I know that the mesh renderer can be disabled: GetComponent(MeshRenderer).enabled = false; but no matter where I put this in the script it simply does not work (example here). Also, for the camera system would it be simpler to create a switching camera system such as the one described here, or would it be better to parent the character to the vehicle upon entering and use the character’s cameras to view the vehicle (example here).

Please, if someone knows how to do any one of these, I would really appreciate your help.

Driving Package
10mb

Well, as long as you aren't rendering from more than one camera at a time, there's no real penalty to having a lot of different camera angles to choose from! This only really answers part of your question, and right now I don't have time to go into more detail on the others (sorry) so this goes as a comment...

1 Answer

1

Without having the ability to test your package, I'd say that this is the easiest way to solve your problems:

Question 1:

How can I make the character invisible once it enters the vehicle and have it's collider disabled etc?

Your options are to either make the collider into a trigger and inactivate the renderer, use the camera layers for culling or inactivate the GameObject. I'm not sure what the problem with your script is, try to isolate the event within a new project where you only disable an object. If the object is separated into several GameObjects then you'd have to iterate through them. Use #pragma strict to make sure dynamic scripting is off so you get a more reliable workflow.

#pragma strict
#pragma downcast

private var player : GameObject;

function Start () {
    player = GameObject.Find("Player");
}

function OnEnterVehicle () {
    player.renderer.enabled = false;
    player.collider.isTrigger = true;

}

function OnExitVehicle () {
    player.renderer.enabled = true;
    player.collider.isTrigger = true;
}

To iterate through the components use this (and this is why we use #pragma downcast):

for (child in player.transform) {
    child.renderer.enabled = false;
}

Question 2:

If I add additional cameras to the car (eg. Dashboard view, Bumper view etc) how could I switch between them, without switching to the players cameras, and vice versa?

You could consider setting everything up with just one camera and make that camera reposition itself. Although you probably want to have different abilities for them where one camera is able to move smoothly around the player and several of them stuck onto the object (like the bumper camera). Then it's easier to work with several cameras in the scene where you just switch between them. They all have their separate scripts which gets activated if the camera is in use.

To iterate through your current cameras you can use something like this:

#pragma strict

var currentCamera : int = 0;
var cameras : Camera[];

function Start () {
    SwitchCamera(false);
}

function Update () {
    if (Input.GetKeyDown(KeyCode.C)) {
        SwitchCamera(true);
    } 
}

function SwitchCamera (switchCam : boolean) {

    if (switchCam) {
        if (currentCamera < cameras.length-1) {
            currentCamera++;
        } else {
            currentCamera = 0;
        }
    }

    for (cam in cameras) {
        cam.enabled = false;
        cam.GetComponent(AudioListener).enabled = false;
    }

    cameras[currentCamera].enabled = true;
    cameras[currentCamera].GetComponent(AudioListener).enabled = true;
}

To make this work you'd have to only have the cameras for the car inside the `cameras` array. When you enter the car you activate the script object (or instantiate the cameras childed to an empty GameObject as a prefab - remember in that case to destroy the earlier set of cameras) and deactivate the camera for the player:

function OnEnterVehicle () {
    player.renderer.enabled = false;
    player.collider.isTrigger = true;
    //Adding:
    vehicleCameras.active = true;
    playerCamera.active = false;
}

Then you'd reverse it for `OnExitVehicle()`.

You could then have a reference to the car you just entered and let the script know what object to position and child itself to.

static var enteredVehicle : GameObject;
function OnEnterVehicle () {
    player.renderer.enabled = false;
    player.collider.isTrigger = true;
    vehicleCameras.active = true;
    playerCamera.active = false;
    //Adding:
    enteredVehicle = theCar;
    vehicleCameras.transform.position = theCar.transform.position;
    vehicleCameras.parent = theCar.transform;
}

There are a couple of ways to determine what car has been entered. One is to tag them within their script with an int which gets referenced to by the player, or you could determine this with a raycast from the player at entrance. So `theCar` would be set as a GameObject from a raycast inside `OnEnterVehicle()`. You'd call `OnEnterVehicle()` inside an update if the player is looking at a car, pressing the button and by checking its distance.

You probably have different sizes of the vehicles where you'd have to reposition some of the cameras. This can be done quite easily with a predefined case-switch:

function SwitchCamera (switchCam : boolean) {
    //Do this after you've altered currentCamera
    switch (currentCamera) {
        case 0:
            cameras[currentCamera].transform.localPosition = Vector3(-2,0,4);
        break;
        case 1:
            //and so on
        break;
    }
}

You could also position them towards another array with empty GameObjects:

cameras[currentCamera].transform.position = positions[currentCamera].transform.position;

Question 3:

How difficult would it be to convert the current two camera system to a one camera system? Since if for example I have a mere 5 cars in the scene, that will be a difference of having either 5 or 10 cameras to deal with.

It would be easier to have two separated systems for vehicles and walking. The main reason is that you can deactivate camera-scripts that isn't needed easier. You just switch between the systems like described in the answer for Question 2. If you only use one camera or one base for all cameras then you'd have to have a quite large case switch which isn't necessary if you instead object orient this.

I'd also recommend on posting this on the [Unity Forums][1] as you seem to want help in a very broad topic. Unity Answers is more of a specific issue - kind of deal. [1]: http://forum.unity3d.com/

Good to hear things worked out! I updated the original answer. In the future try to isolate your question a bit further so you'll get more precise answers and better help from the community. ;-)