Search Unity

How to make this enter/exit car script apply for any vehicle

Discussion in 'Scripting' started by TSC, Sep 14, 2013.

  1. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    241
    hey everyone I also have this car enter/exit script I want to work for any car I get in instead of just one ... like making it a in general script this is what I have

    Code (csharp):
    1. var Car :Transform;
    2. var Player :Transform;
    3. var exitPoint :Transform;
    4. var doorTriggerLeft :Transform;
    5. var PlayerCamera :Camera;
    6. var CarCamera :Camera;
    7. var isPlayerVisible :boolean;
    8.  
    9. function Update () {
    10.  
    11.     if(Input.GetButtonUp("Action") isPlayerVisible){
    12.         Player.gameObject.SetActiveRecursively(false);
    13.         Player.gameObject.active = false;
    14.         Player.parent = exitPoint.transform;
    15.         Player.transform.localPosition = Vector3(-1.5,0,0);
    16.         exitPoint.parent = Car.transform;
    17.         exitPoint.localPosition = Vector3(-3,0,0);
    18.     //  GameObject.Find("Car").GetComponent("Car").enabled = true;
    19.     //  GameObject.Find("Car").GetComponent("SoundController").enabled = true;
    20.         GameObject.Find("Charger").GetComponent("CarController").enabled = true;
    21.         PlayerCamera.enabled = false;
    22.         CarCamera.enabled = true;  
    23.     }
    24.    else{
    25.      if(Input.GetKeyUp("r")){
    26.         Player.gameObject.SetActiveRecursively(true);
    27.         Player.gameObject.active = true;
    28.         Player.transform.parent = null;
    29.         //exitPoint.parent = doorTriggerLeft.transform;
    30.         exitPoint.parent = Car.transform;
    31. //      GameObject.Find("Car").GetComponent("Car").enabled = false;
    32. //      GameObject.Find("Car").GetComponent("SoundController").enabled = false;
    33.         GameObject.Find("Charger").GetComponent("CarController").enabled = false;
    34.         //GameObject.FindGameObjectWithTag("sentryGun").enabled = true;
    35.         PlayerCamera.enabled = true;
    36.         CarCamera.enabled = false;   
    37.      
    38.      }  
    39.    
    40.    }
    41. }
    42.  
    43. function OnTriggerEnter(Player : Collider){
    44.     isPlayerVisible = true;
    45. }
    46.  
    47. function OnTriggerExit(Player : Collider){
    48.     isPlayerVisible = false;
    49. }
    --RIO
     
  2. carking1996

    carking1996

    Joined:
    Jun 15, 2010
    Posts:
    2,563
    Place a trigger next to the car. Get the car component script of the car. Run it when you wanna get into the car by using the trigger.
     
    Lethn likes this.
  3. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    241
    what If different cars have different car scripts like one is named car the other is named CarController also I have door triggers already it's just if I have both lines of code activated (18, 20) and (31,33) then it will place my character in the first car which is line (18, 31) and also move the other car (20, 33) that's if I remove the comments, im looking for the script to work on any car without moving the other cars
     
  4. Soul_Raven

    Soul_Raven

    Joined:
    Apr 12, 2014
    Posts:
    13
  5. CzechBuh

    CzechBuh

    Joined:
    Jun 28, 2016
    Posts:
    2
    ENTER/EXIT Car like as GTA + Canister 10l and FuelMeter with animation


    This video show a complete script in C:

    Include functions:

    * Car Enter
    * Car Exit
    * Boot Lid Open
    * Boot Lid Open
    * Car fuel meter
    * Fuel canister 10l

    Messages:
    "You added fuel into the car"
    "Canister is Empty!"
    "I cant drive with canister"
    "I cant drive, if boot lid si open"
     
  6. dooly123

    dooly123

    Joined:
    Jul 7, 2015
    Posts:
    36
    here is a single-player component of my script that I sell in the asset store with some modifications and removed systems but this will defiantly get your enter exit working for what you are trying to do. I have a networked version on the asset store if anyone is interested.

    more info on the script can be found below.
     

    Attached Files:

    Last edited: Aug 22, 2017
    metaldc4life likes this.
  7. thedetective123

    thedetective123

    Joined:
    Aug 20, 2017
    Posts:
    3
    Dooley how to setup there scripts
     
    dooly123 likes this.
  8. dooly123

    dooly123

    Joined:
    Jul 7, 2015
    Posts:
    36
    that's the easy part all you have to do is add both to your car. EnterExitSingle is the single-player variant of my code but still has the simple method of drop and drag. in other words, apply it fill the boxes out added all the scripts you want to be enabled when the player is in the vehicle and when he's out to lose control of after settings this up


    make sure to add the spawnpoint script to where you want the player to exit!!
     
  9. mattis89

    mattis89

    Joined:
    Jan 10, 2017
    Posts:
    1,056
    It would be awesome to get some help with a enter/exit script that is raycast based because I cant use ontrigger..
     
  10. dooly123

    dooly123

    Joined:
    Jul 7, 2015
    Posts:
    36
    https://discord.gg/eGASr3 follow up with me here.
     
    mattis89 likes this.
  11. habib15964

    habib15964

    Joined:
    Jul 20, 2018
    Posts:
    4
    Tutorial link ,please!
     
  12. habib15964

    habib15964

    Joined:
    Jul 20, 2018
    Posts:
    4
    @dooly123 if possible , you can send project link?
     
    dooly123 likes this.
  13. dooly123

    dooly123

    Joined:
    Jul 7, 2015
    Posts:
    36
  14. habib15964

    habib15964

    Joined:
    Jul 20, 2018
    Posts:
    4
  15. dooly123

    dooly123

    Joined:
    Jul 7, 2015
    Posts:
    36
  16. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    675
    What you want is called polymorphism. Create a base class called AbstractCarController that defines what methods and variables are shared among all cars, then you can inherit from that to make specific car classes.
     
  17. VinoTube7

    VinoTube7

    Joined:
    Mar 29, 2019
    Posts:
    1
    SORRY bad english. To set the script, create new gameobject with box collider, and put the SpawnPoint Script to the gameobject. Put your EnterVehicle Script to Your car and you will see a variable "componentstodisableifnotdriver" and put your car in the variable, make sure that you see the the car script in the variable. Hope This Helps.
     
  18. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,800
    For simplicity, I would probably also use box, or sphere colliders at doors, with OnCollisionEnter or similar.