Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help with minor tweaks to 3rd Person Enter/Exit Vehicle Script

Discussion in 'Scripting' started by Wolfsshadow, Aug 14, 2012.

  1. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    Hello everyone!
    I've managed to work through several tutorials and found a way to successfully use a 3rd person camera while entering and exiting a vehicle. The problem is that I can enter it from very far away unless I happen to hit or come close to the side of the track. My guess is that it has something to do with the collider on the side of the track and once I exit I cannot re-enter until I catch up with my vehicle(which is how it should be.). Anyone have an ideas as to why I can enter and exit with the vehicle still speeding down the track, but when coming close to a collider it functions correctly? Script is as follows:

    var car : Transform;
    var player : Transform;
    var exitPoint : Transform; // Place this empty gameobject next to the driver car door.
    var doorTriggerLeft : Transform;
    var PlayerCamera : Camera;
    var CarCamera : Camera; // By default the camera "component" for the car camera should be set to OFF first.
    var isPlayerVisible : boolean;

    function Update()
    {
    if (Input.GetKeyUp("e") isPlayerVisible) // Assign any key you want to enter/operate vehicle.
    {
    Debug.Log("Driving");
    // Make player invisible and still standing
    player.gameObject.SetActiveRecursively(false);
    player.gameObject.active = false;
    // Parent player to Exit Point
    player.parent = exitPoint.transform;
    player.transform.localPosition = Vector3(-1.5,0,-0.65);
    // Parent PlayerParent to car
    exitPoint.parent = car.transform;
    exitPoint.transform.localPosition = Vector3(-3.0,1.5,0.0); // Driverside exit point, IMPORTANT: Adjust accordingly per vehicle.
    // GameObject.Find(VehicleObjectName").GetComponent("DrivingScriptHere").enabled-true;
    GameObject.Find("Car").GetComponent("Car").enabled=true; // Enables the script component to operate Vehicle. Verify GameObject Component name!
    PlayerCamera.enabled = false; // Disables the playerCamera
    CarCamera.enabled = true; // Enables the carCamera
    }
    else
    {
    if (Input.GetKeyUp("r")) // Assign any key you want to exit/park vehicle.
    {
    Debug.Log("Walking");
    // Make character visible again
    player.gameObject.SetActiveRecursively(true);
    player.gameObject.active = true;
    // Unparent player from everything
    player.transform.parent = null;
    // exitPoint parent = doorTriggerLeft transform;
    // Parent exitPoint to car gameobject
    exitPoint.parent = car.transform;
    // GameObject.Find("VehicleObjectName").GetComponent("DrivingScriptHere").enabled=false;
    GameObject.Find("Car").GetComponent("Car").enabled=false; // Disables the script component to park Vehicle. Verify GameObject and Component name!
    PlayerCamera.enabled = true; // Re-enables player camera
    CarCamera.enabled = false; // disables car camera

    }
    }
    }

    function OnTriggerEnter(Player : Collider)
    {
    Debug.Log("Trigger Enter, Player Detected");
    isPlayerVisible = true;
    }

    function OnTriggerExit(Player : Collider)
    {
    Debug.Log("Trigger Exit");
    isPlayerVisible = false;
    }


    Thanks for your time!

    Wolfsshadow
     
  2. Wolfsshadow

    Wolfsshadow

    Joined:
    Aug 14, 2012
    Posts:
    14
    If any additional information would help I'd be more than happy to provide it. I'm just really trying to figure this thing out.