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 /
  • Help Room /
avatar image
0
Question by CptBolee · Nov 24, 2015 at 08:19 AM · c#2dvehicleenter-car

Enter/Exit Vehicle in 2D top down view

Hello I am trying to make a game similar to gta 1 or 2 .. where you have top down view and you can drive any vehicle.... but I ran in to a problem...

I made a script for entering or exiting vehicle.. script is attached to vehicle. And Vehicle (in this case tractor) has 2 colliders, 1 is for rigidbody and other is for player entering car, which is trigger. Problem is that camera doesn't get to 0,0,-10 everytime.. and when you press E it enters/exits several times, Here is my script:

 public int movementSpeed = 2;
 public int rotationSpeed = 50;
 static float angle = 0;
 
 GameObject camera1;
 GameObject player;
 GameObject tractor1;
 bool isInVehicle;
 bool isInCollider;
 Vector3 tractorPosition;
 //private Vector3 pos;
 
 void Start () {
     isInVehicle = false;
 }
 
 void Update () {
     if (isInVehicle) {
         if (Input.GetKey (KeyCode.W)) {
             transform.Translate (Vector3.up * movementSpeed * Time.deltaTime); 
         }
         if (Input.GetKey (KeyCode.S)) {
             transform.Translate (Vector3.down * movementSpeed * Time.deltaTime);
         }        
         if (Input.GetKey (KeyCode.A)) {
             angle = angle + rotationSpeed * Time.deltaTime;
         }    
         if (Input.GetKey (KeyCode.D)) {
             angle = angle - rotationSpeed * Time.deltaTime;
         }    
         transform.rotation = Quaternion.Euler (new Vector3 (0f, 0f, angle));
     }
     if (Input.GetKey (KeyCode.Escape)) {
         Application.LoadLevel(0);
     }
     if (Input.GetKey (KeyCode.E)) {
         if (isInCollider) {
             Debug.Log ("Key pressed...");
             camera1 = GameObject.FindWithTag ("MainCamera");
             player = GameObject.FindWithTag ("player");
             tractor1 = GameObject.FindWithTag ("tractor1");
             tractorPosition = transform.position;
             if (!isInVehicle) {
                 Debug.Log ("Entered vehicle..");
                 camera1.transform.parent = tractor1.transform;
                 player.GetComponent<Renderer> ().enabled = false;
                 isInVehicle = true;
             } else {
                 Debug.Log ("In vehicle....");
                 if (isInVehicle) {
                     Debug.Log ("Exited vehicle....");
                     camera1.transform.parent = player.transform;
                     player.GetComponent<Renderer> ().enabled = true;
                     player.transform.position = tractorPosition;
                     isInVehicle = false;
                 }
             }
             camera1.transform.position = new Vector3 (0f, 0f, -10f);
             camera1.transform.rotation = Quaternion.Euler (new Vector3 (0f, 0f, angle));
         }
     }
 }
 
 void OnTriggerStay2D(Collider2D col) {
     Debug.Log("Trigger entered....");
     isInCollider = true;        
 }


If you have any questions,, please ask. Thanks in advance!

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
0

Answer by lloladin · Nov 29, 2015 at 04:27 PM

to me it seems like its bc you are using

 Input.GetKey (KeyCode.E))

which means you want this to happend whenever you hold down the key so it will run the code multiple times you probly want to use

 Input.GetKeyDown (KeyCode.E))

this should fix that you enter the exit alot of times not sure if it will fix the camera not always changeing postion hope this helps

Comment
Add comment · 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

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

48 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[2D] Sticking to walls and ceilings 0 Answers

GameObject overlapping on random spawn 0 Answers

Unity2D C# Randomly Spawn GameObject in an Area over another GameObject 1 Answer

How to instantiate multiple objects with intervals every X seconds? 1 Answer

Player Movement Not Always Responding 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