• 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
Question by Major · Mar 31, 2012 at 09:49 AM · carrpgvehicleexitenter

Entering and Exiting Vehicals

Now I know that this question gets asked a lot, but my script simply refuses to work properly. I have this script through some tuts, but it won't work like it will in those. Here is my script, I hope that somebody can help me out. Thanks!

 // arrays
 var myCams : Camera[];
 var mySeats : Transform[];
 
 // timer
 var timer : int = 3;
 
 // ship
 var ship : GameObject;
 
 // active control vars
 private var state : int = -1;
 private var count : int = 0;
 
 // player position & cam
 var player_object : Transform;
 var player_camera : Camera;
 var old_position : Vector3;
 
 function Update()
 {
     if (Input.GetAxis ("e"))
     {
         if (state == -1 && timer < 0)
         {
             for (var index : int = 0; index < mySeats.Length; index++)
             {
                 if(Vector3.Distance (mySeats[index].position, player_object.position ) < 3 )
                 {
                     // deactivate first person camera
                     player_camera.enabled = false;
 
                     // activate other camera
                     myCams[index].enabled = true;
 
                     // now sitting at
                     state = index;
 
                     print (index);
 
                     // tell turret / driving scripts they can activate
                     BroadcastMessage ("Activate_Turret", state);
 
                     // reset timer
                     timer = 20;
 
                     // parent user to seat
                     player_object.parent = mySeats[index];
 
                     old_position = player_object.transform.localPosition;
 
                     player_object.transform.localPosition = Vector3(0, 0, 0);
 
                     // break out of the for loop cause we have already selected one seat
                     break;
                 }
             }
         }
 
         // get out
         if (state > -1 && timer < 0)
         {
             // we are now first person again
             state = -1;
 
             // reset timer
             timer = 30;
 
             // tell turret / driving scripts they can deactivate
             BroadcastMessage ("Activate_Turret", -1);
 
             // put back relitave to where the player entered the vehicle/seat
             player_object.transform.localPosition = old_position;
 
             // Detaches the transform from its parent.
             transform.parent = null;
 
             // reactivare first person camera
             player_camera.GetComponent("Camera").enabled = true;
 
             // disable all other cameras
             for (var thisCam in myCams)
             {
                 thisCam.GetComponent("Camera").enabled = false;
             }
         }
     }
 
     // fix speed
     timer = timer - 1;
 }
Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Mar 31, 2012 at 10:38 AM

You usually want to use Input.GetButtonDown instead of Input.GetAxis. That way you don't need this hacky timer which doesn't even work very stable in your case. You set the timer to 20 and you subtract 1 each frame. At a quite high framerate it will toggle very fast while you hold down "e".

Input.GetButtonDown is only true in one frame.

You should be more precise when you ask a question.
"but it won't work like it will in those" doesn't explain your problem in any way.

Comment

People who like this

0 Show 0 · 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

Answer by Major · Apr 01, 2012 at 12:05 AM

Okay you have a good point. And what isn't working is I can go up to the car, click e and i'll end up in the car. But I am facing the wrong direction, I can't move, and the FPS controller is still active.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

JS Enter Exit with NetworkPlayer 1 Answer

How to make an Enterable/Exitable Vehicle 1 Answer

vehicle enter/exit, player position problem after exiting 0 Answers

Entering/exiting vehicles 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