How to check if player remains on a path? (2D)
Hello, I am quite new to coding and am developing a top-down mobile game for a college project. The fundament of the game's mechanic is to navigate a path without walking off it. The whole idea seemed pretty simple at first. Give the player a collider, same with the path objects, then give the path objects a Tag "Platform" and check if the player is ever not in contact with the path. Unity wont seem to take the ! operator for the "if (!col.tag == ("Platform"))". I tried making a else, statement to check if the player is ever not colliding with the path and it always seems to come up as true.
Any suggestions?
void OnTriggerStay2D (Collider2D col)
{
if (!col.tag == "Platform") {
}
}
Just to note, I have tried "OnTriggerExit2D", but it triggers every time I move from one path to the next, even if I never leave a Platform tagged collider.
I also will not place colliders around the entire path, because a lot of the levels get complex and I'd rather just be able to drag and drop my paths into the environment for instant function. As part of this project was to make development streamlined.
Answer by Winterblood · Apr 27, 2016 at 02:15 PM
I think what you have there is going to try to interpret col.tag as a bool, negate it, and then compare it to the "Platform" string. Try this:
if (col.tag != "Platform")
Thank you! It does work now... Sort of. After fixing this it made me realize I now have a logic error. The code now executes under this "if" statement if the Player is colliding with any object other than the one with the "Platform" tag. When really I want it to only execute when the player is not colliding with a "Platform". Any thoughts on how to get this to work?
Your answer
Follow this Question
Related Questions
How to tell if a collider is on the right or left of another collider? 2 Answers
Check if character has entered trigger, if it has play animation on character. 1 Answer
Need help with using Destroy with a variable condition 0 Answers
Unity Android build multiplying decimal numbers by 10 for an unknown reason 0 Answers