Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zefrof · May 27, 2011 at 09:02 PM · carvehicleenter

Vehicle Tutorial

Does anybody know of a tutorial that can teach me how to make it so characters can enter vehicles and drive them around?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RetepTrun · Jun 01, 2011 at 05:16 PM

No tutorial but its not hard

In both the vehicle's and the man's script add an if and boolean so if it's true then the arrow keys make the car move, otherwise make the keys controll the man. Also parent the player to the car, while simutaneously moving the player to the position of a sear object.

You can look at this for clues, mine is a bit complicated because my game is split screen and the players are rigibodies. This script is attached to the car object which has a large trigger-box-collider, when we are within this trigger a person can press "p" and is teleported to the driver's seat.

Variables:

seats1&2-empty children of the car that just mark where to teleport the player

seat1exit: where to put player when he presses "u" to get out

camerA,B: cameras for player1or2 attached to the car that are turned on when we get in

personcamAB: the script finds the man's camera and turns it off when he gets in

bodycol 1 2 3: colliders of the car that we dont want the player inside to collide with because a rigidbody in a rigidbody makes things wierd

itsp1 p2: because I have 2 players my script identifies the different people by the type of movement script they have.

2 important lines:

GetComponent(car1).ControllableA = true; // here this script talks to the one with the car's controlls and says to let car be controlled

     la.nut();  //la is the script of the player, and we call function nut() on it which makes it so the man cannot be controlled
 
 <pre>
 
 var seat1 : Transform;
 var seat2 : Transform;
 var seat1exit : Transform;
 var seat2exit : Transform;
 
 private var seat1used : boolean;
 private var seat2used : boolean;
 
 private var passenger : Transform;
 private var passenger2 : Transform;
 
 var personCamA:Camera;
 var personCamB:Camera;
 var cameraA: Camera;
 var cameraB:Camera;
 
 private var itsP1 :boolean=false;
 private var itsP2 :boolean=false;
 var bodycol1 : Transform;
 var bodycol2: Transform;
 var bodycol3: Transform;
 
 function Start()
 {
     seat1used = false;
     seat2used = false;    
     //GetComponent(MyScript).enabled = false;
     cameraA = transform.Find("CameraA").camera;
     cameraB = transform.Find("CameraB").camera;
     //cameraA = transform.Find("CameraA");
     //cameraB = transform.Find("CameraB");    
     cameraB.enabled = false;
     cameraA.enabled = false;
 }
 
 function OnTriggerStay(other : Collider)
 {
     //first seat get in    
     if(other.tag == "Player" && other.rigidbody && Input.GetKey("p") && seat1used == false)
     {
     seat1used = true;
     other.rigidbody.isKinematic = true;
     passenger = other.transform;
     passenger.parent = transform;
     passenger.transform.position = seat1.position;
     passenger.transform.rotation = seat1.rotation;
     
     var  la = passenger.GetComponent(physContrlr);            //p1 seat1
     var  le = passenger.GetComponent(myControllerP2);        //p2 seat1
     
     if( passenger.GetComponent(physContrlr) )//for player1 1st seat controlls and cameras
         {
         personCamA = passenger.Find("Camera_P1").camera;
         GetComponent(car1).ControllableA = true;            
         la.nut();
         cameraA.enabled = true;
         personCamA.enabled = false;
         itsP1=true;
         
         Physics.IgnoreCollision(passenger.collider, bodycol1.collider);
         Physics.IgnoreCollision(passenger.collider, bodycol2.collider);
         Physics.IgnoreCollision(passenger.collider, bodycol3.collider);
         //cameraA.active = true;
         }
     if( passenger.GetComponent(myControllerP2))//for player2 1st seat controlls and cameras
         {
         personCamB = passenger.Find("Camera_P2").camera;
         GetComponent(car1).ControllableB = true;
         le.nut();
         cameraB.enabled = true;
         personCamB.enabled = false;
         itsP2 = true;
         //cameraB.active = true;
         }
     //p1 script = la
     //p2 script = le    
     }
     
 //second seat get in
     if (other.tag == "Player" && other.rigidbody && Input.GetKey("y") && seat1used == true && seat2used == false)
     {
     passenger2 = other.transform;
     seat2used = true;
     other.rigidbody.isKinematic = true;
     passenger2 = other.transform;
     passenger2.parent = transform;
     passenger2.transform.position = seat2.position;
     passenger2.transform.rotation = seat2.rotation;    
     }
 }
 
 //
 //        
 //    
 
 //}
 
 //function Update()
 //{    
     //if(Input.GetKey("p") && seat1used == false) 
     //{        
     //}    
 function Update(){
     if (Input.GetKey("u") && seat1used == true)
     {                
         passenger.transform.position = seat1exit.position;
         passenger.transform.rotation = seat1exit.rotation;        
         passenger.parent = null;
         passenger.rigidbody.isKinematic = false;        
         seat1used = false;        
         
         if(itsP1 ==  true){//player1 get out of 1st seat
 
         passenger.GetComponent(physContrlr).nut();
         GetComponent(car1).ControllableA = false;        
         cameraA.enabled = false;
         personCamA.enabled = true;
         
         }
         
         if(itsP2 == true){
 
         GetComponent(car1).ControllableB = false;        
         passenger.GetComponent(myControllerP2).nut2();
         cameraB.enabled = false;
         personCamB.enabled = true;
         }
     }
         //get out of 2nd seat
     if (seat2used == true && Input.GetKey("t"))
     {    
             var  da = passenger2.GetComponent(physContrlr);       //p1 seat2
             var  de = passenger2.GetComponent(myControllerP2);  //p2 seat2
             seat2used = false;
             passenger2.transform.position = seat2exit.position;
             passenger2.transform.rotation = seat2exit.rotation;
             passenger2.rigidbody.isKinematic = false;
             passenger2.parent = null;
             
             if(de)   //player2
             {
             de.nut2(myControllerP2);
             cameraB.enabled = false;
             personCamB.enabled = true;
             }
             else    //player1
             {
             da.nut2(physContrlr);
             cameraA.enabled = false;
             personCamA.enabled = true;
             }
     }
 }
 
 
 
 function deadpassenger()
 {
 seat2used = false;
 }
 function deaddriver()
 {
 seat1used = false;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FLASHDENMARK · Jun 01, 2011 at 05:30 PM 0
Share

When posting code please highlight the code and press the "10101" button. It will make the code look nice and readable. I will do this for you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

could someone help with entering/exiting a vehicle? 0 Answers

Entering and Exiting Vehicals 2 Answers

Problem Enter and Exit with Vehicle 2 Answers

Enter car problem Character grows And have to hold the button? 1 Answer

Problem with my car enter/exit script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges