Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

switch from vehicle to awalking player

Discussion in 'Scripting' started by HaVaNa7, Dec 30, 2011.

  1. HaVaNa7

    HaVaNa7

    Joined:
    Dec 23, 2011
    Posts:
    44
    Hi guys, i have a script that with a controller change the vehicle when you press a button.
    Now i would like to insert also a fps character so i added:

    Code (csharp):
    1. Camera.main.GetComponent(CharacterController).enabled=true;
    2.  
    3.         Camera.main.GetComponent(CharacterMotor).enabled=true;
    4.  
    5.         Camera.main.GetComponent(FPSInputController).enabled=true;
    6.  
    7.         Camera.main.GetComponent(MouseLook).enabled=true;
    in the place of

    Code (csharp):
    1. Camera.main.GetComponent(SmoothFollow).enabled=true;
    2.  
    3.         Camera.main.GetComponent(SmoothFollow).target=transform;
    and it works, but the camera appears always in te same place even if i moved the fps character.

    For example if i choose a car, i move it, then i select another vehicle, and i reselect again the car, i find it in the exactly position where i left it. I want this also for the fps character...

    I think that Camera.main.GetComponent(SmoothFollow).target=transform; gives it the position but i don't know how to get it with fpscharacter..
     
  2. HaVaNa7

    HaVaNa7

    Joined:
    Dec 23, 2011
    Posts:
    44
    anyone?
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Just turning off scripts and such is not a good solution for you.

    First, you have to find a vehicle to get in, then do all the scripting stuff.

    Here is a simple script to find the nearest vehicle that we are looking at:
    Code (csharp):
    1.  
    2. var walking : boolean=true;
    3. var minimumEnterRange : float = 10.0;
    4. var currentVehicle : GameObject;
    5.  
    6. function Update(){
    7.     if(Input.GetKeyDown(KeyCode.H)){
    8.         if(walking){
    9.             // find all the vehicles in the scene
    10.             var vehicles : GameObject[] = GameObject.FindGameObjectsWithTag ("Vehicle");
    11.             // find the closest vehicle we are looking at
    12.             for(var vehicle : GameObject in vehicles){
    13.                 if(Vector3.Distance(vehicle.transform.position, transform.position) < minimumEnterRange){\
    14.                     var vehicleInCameraSpace = Camera.main.transform.InverseTransformPoint(vehicle.transform.position).normalized;
    15.                     if(Vector3.Dot(Camera.main.transform.forward, vehicleInCameraSpace)>.3){
    16.                         currentVehicle = vehicle;
    17.                         break;
    18.                     }
    19.                 }
    20.             }
    21.             // if we can find a vehicle close enough then lets get in it.
    22.             if(currentVehicle){
    23.                 walking = false;
    24.                 Camera.main..GetComponent(CharacterController).enabled=false;
    25.                 Camera.main..GetComponent(CharacterMotor).enabled=false;
    26.                 Camera.main..GetComponent(FPSInputController).enabled=false;
    27.                 Camera.main.GetComponent(MouseLook).enabled=false;
    28.                 Camera.main.GetComponent(SmoothFollow).enabled=false;
    29.                 Camera.main.GetComponent(SmoothFollow).target=transform;
    30.             }
    31.         } else {
    32.             // if we are currently driving then lets get out of the vehicle.
    33.             walking = true;
    34.             Camera.main..GetComponent(CharacterController).enabled=true;
    35.             Camera.main..GetComponent(CharacterMotor).enabled=true;
    36.             Camera.main..GetComponent(FPSInputController).enabled=true;
    37.             Camera.main.GetComponent(MouseLook).enabled=true;
    38.             Camera.main.GetComponent(SmoothFollow).enabled=false;
    39.             currentVehicle = null;
    40.         }
    41. }
    42.  
    You will notice that if you are walking an nothing is in range, we are still walking. If there is a vehicle in front of us that is close enough, then we can get in it. And, if we are in a vehicle, we would get out of it.

    You still are missing the vehicle controllers as well, That would be something like vehicle.GetComponent(VehicleController).isPlayer = true; or something like that.
     
  4. HaVaNa7

    HaVaNa7

    Joined:
    Dec 23, 2011
    Posts:
    44
    Thnx BigMisterB

    But if i don't want to change the whole script, is there a short way to get position of Camera.main.GetComponent(SmoothFollow).target and assign it to the CharacterController?
     
  5. Shubin

    Shubin

    Joined:
    Nov 5, 2011
    Posts:
    12
    It would be easy having two cameras, and disabling either one depending if your in the vehicle or not. Same with controls. You could have two sets and disable one or the other.
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The point is that you dont want to do that. You want to set the SmoothFollow target to the vehicle, not the CharacterController.

    Yes and no, you will still need to determine which vehicle that you want to enter. That first part that I wrote about finding the vehicle does that.

    Edit from above:
    Code (csharp):
    1.  
    2. Camera.main.GetComponent(SmoothFollow).target=currentVehicle;
    3.  
     
  7. HaVaNa7

    HaVaNa7

    Joined:
    Dec 23, 2011
    Posts:
    44
    Ok, i'll try your way. i'm tring to understand what you wrote because i'm new to javascript... can yopu explain me in a few words how to use your script?

    Thnx for all
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The first part is, that you have to have two basic scripts that handle your functions. A character controller for the character and a vehicle controller for the vehicles.

    The character controller doesn't really have to have a physical model and it is a single entity. In your script I would disable the whole model so it doesn't interfere with physics. And reenable it and reposition it when you exit the vehicle.

    All of the vehicle's should have the same script. This script will define if they are driven by the player, an AI or no one. They are always active and should always be available for physics.

    In concept, you search for all Non AI vehicles that are in range that the player is facing. If you find one, then you swap the camera controls and disable the player. When you exit the vehicle the player needs to be re-enabled and placed by the exit point of the vehicle.

    so its kind of like this:
    Code (csharp):
    1.  
    2. // get the enterExit key
    3. // if we are driving, place the character at the driver's door (part of the vehicle script) and enable him. Also change the camera from smoothfollow to mouselook and change the vehicle to isAI=false and isPlayer=false.
    4. // if we are not driving then find a close vehicle's driver's door (part of the vehicle script) and check to see if we are facing it.
    5. // if the vehicle isAI == true then we need to figure out if we can throw him out.
    6. // if we have a vehicle that we want to enter, then disable the player, and set the vehicle isPlayer = true and isAI = false and change the camera from mouselook to smoothfollow
    7.  
    So many things could be added to this, like walking to the drivers door if we are no where near it, playing some animation to throw an AI driver out.
     
  9. HaVaNa7

    HaVaNa7

    Joined:
    Dec 23, 2011
    Posts:
    44
    Thnx again Big, nut i'm too noob to create another scene, i have to learn something first, at the moment all i can do is a way to keep the position of Camera.main.GetComponent(SmoothFollow).target and transform that in a fps camera
     
  10. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    OK, yes, you should answer in the forum instead of PM, so if other people have problems they can see the answer. ;)

    OK, so the first thing that I notice is in the FixedUpdate of your tank controller.

    queryUserInput

    This is a indicator that the tank is to be controlled. In order to change it from another script, it cannot be private though.

    That would be this:
    vehicle.GetComponent(VehicleController).queryUserInput= true;

    So somewhere in there I stated that you need to make an adjustment in your Vehicle controller script... that is the adjustment. True, you drive the tank, false, you don't.

    So, when you get in a vehicle, disable the character so that it does not affect physics, and turn the queryUserInput to true on the tank you want to control. Also, you need to enable the smooth follow and point it towards the tank and disable the mouselook.

    When you want to get out of the vehicle, you will need to change the position of the character to where the vehicle stopped at. (or around there somewhere) enable him, set the queryUserInput to true, enable the mouselook and disable the smooth follow.
     
  11. HaVaNa7

    HaVaNa7

    Joined:
    Dec 23, 2011
    Posts:
    44
    Yes public is better, i thought different because of your only replyes...

    I still have some problem, you say: vehicle.GetComponent(VehicleController).queryUserInput= true; but i don't know where to write this, in your script (attached to the main cam) or the tank script (attached to the tank).

    Have i to attach the script to the tank and not to the main cam?

    maybe something like: GameObject.Find("M1").GetComponent("Tank").enabled=false; where M1 is my tank and Tank is my script attached to the tank.
     
    Last edited: Jan 4, 2012
  12. CrewGuy

    CrewGuy

    Joined:
    Jan 11, 2015
    Posts:
    1
    can u show an example of how to use this script like making a you tube video. if so that would be great. Then i would sub so if u are new u get a sub so fast. plus it's true. plus ask this guy for proof : https://www.youtube.com/channel/UC6PGLVCMpPFkjwDvhmTp7Jw just go to one of his videos and he will tell reply to u. i was always looking for how to do this on you tube. But no one teaches properly. Plus i am new to java scripting and C# scripting so plz help. would make my life so much happy. plus just asking can u use any car controller for this script? I bought realistic car controller on the asset store. can i use that. if i can then i would have my dream come true game in no time. and i will post the download on my website when i am finished the game. here is my website so far : http://www.crewriders.com well please help me i and u will get subscribed in no time. thanks a lot. BIG TIME!!!