- Home /
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);
}
}
}
}
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)
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.
Your answer
Follow this Question
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