Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MetalStorm80 · Feb 14, 2015 at 07:18 PM · 2dtopdownvehicle

Problem entering vehicle on top down 2d game

Hey guys,

I am still pretty new to Unity and coding and have been trying to solve this problem myself for a while but am stuck.

I have a 2d top down world map similar to old rpgs like dragon warrior, final fantasy,etc.

The problem I am having is entering and exiting the ship, I have a script on the ship itself and in the OnTriggerStay2D function I have it watch for a button press, when it is pressed it checks if the player is already on the ship. If he is not it disables the player and changes control to the ship and sets the camera to follow the ship.

If the player is on the ship it looks at the collider and checks for one named Grass1 (so it lets you off on land not water) and then enables the player and sets the position to that of the Grass1 tile.

The issue I am having is that when you try to enter the ship it will put you on then instantly move you off to one of the Grass1 tiles so both scenarios are being used. I sort of understand why that is happening but can not find a solution to the problem.

If I change the code to use a different button for each, x on controller to enter ship, o on controller to exit ship then it will work fine but I want both of them to work when pressing Fire1.

Any suggestions?

Here is the code I have right now.

     void OnTriggerStay2D(Collider2D col)
     {
 
 
         if (Input.GetButtonDown ("Fire1")) 
         {
             if (!IsPlayerOnShip)
             {
                 if (col.name == "Player")      
                 {
                     IsPlayerOnShip = true;
                     Player.SetActive (false);
                 }
             }
         } 
 
         if (Input.GetButtonDown ("Fire2")) 
         {
             if (IsPlayerOnShip) 
             {
                 if (col.name == "Grass1") 
                 {
                     IsPlayerOnShip = false;
                     Player.transform.position = col.transform.position;
                     Player.SetActive (true);
                 }
             }
         }
 
                 
     }
 
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TargonStudios · Feb 15, 2015 at 03:21 AM

You could easily just add in an else statement, to check if the player is in the ship or not. Something like:

 void OnTriggerStay2D(Collider2D col)
       {
   
   
           if (Input.GetButtonDown ("Fire1")) 
           {
               if (!IsPlayerOnShip)
               {
                   if (col.name == "Player")      
                   {
                       IsPlayerOnShip = true;
                       Player.SetActive (false);
                       
                   }
               }
               else
               {
                   
               if (IsPlayerOnShip) 
               {
                   if (col.name == "Grass1") 
                   {
                       IsPlayerOnShip = false;
                       Player.transform.position = col.transform.position;
                       Player.SetActive (true);
                   }
               }
           } 
          }
   
    
       }

Pretty much, it simply checks if the player is in the ship, if not, put him in. If so, take hime out. Voilà.

Hope this works for you! (I've never used C#, but it should work I hope)

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
avatar image
0

Answer by MetalStorm80 · Feb 16, 2015 at 02:42 AM

Using an else statement it would work the first time but then have the original issue the second time. The way I solved it was to make a bool that you set to try when it was changed and then change it back to false in LateUpdate(). That way it could not run both scenarios in one frame.

I am sure there is a way better method to do this but it works now so for now it is good.

Thanks for the response.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Control individual wheels of a top down 2d vehicle 0 Answers

Rotating a Top-Down vehicle To Face a Direction 0 Answers

Sprite facing wrong direction 0 Answers

2d top down Camera help 1 Answer

Trying to make a topdown 2d laser 2 Answers


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