- Home /
If Statement order of execution
Im trying to add a kind of cloud platform thing in my 2D platformer. my problem is just getting the correct order for the if() statements. atm the all just cancel each other out :/ Any help would be very appreciated!
void FixedUpdate ()
{
if(GameObject.FindWithTag("Player").transform.position.y < transform.position.y + 0.5f)
{
colChange(false);
}
else
{
colChange(true);
if(GameObject.FindWithTag("Player").transform.position.y > transform.position.y && Input.GetKey(KeyCode.S))
{
colChange(false);
}
else
{
colChange(true);
}
}
}
void colChange(bool enabled)
{
gameObject.GetComponent<Collider2D>().enabled = enabled;
}
Answer by RobAnthem · Jan 09, 2017 at 08:45 AM
Currently this will make it so that if the player is on the cloud it will have a collider, if he presses S, the will fall through the cloud, and if he gets away from the cloud, it loses its collider.
void FixedUpdate() //The fixed update event.
{
GameObject player = GameObject.FindWithTag("Player"); //Call object with the player tag, if the player has a script on it, use FindObjectOfType<playerScript>().gameObject, to get the player.
if (Input.GetKey(KeyCode.S)) //While the S key is held down.
{
if (Vector3.Distance(player.transform.position, transform.position) < 0.5f) //If the distance from the player and the cloud is less than 0.05f.
{
colChange(false); //Disable the clouds collider.
}
else
{
colChange(true); //Enable the clouds collider.
}
}
else if (Vector3.Distance(player.transform.position, transform.position) > 1f) //If the player is farther than 1f away from our cloud.
{
colChange(true); //Enable the clouds collider.
}
}
/// <summary>
/// Toggle the Collider2D of the object this script is attached to.
/// </summary>
/// <param name="enabled"></param>
void colChange(bool enabled)
{
gameObject.GetComponent<Collider2D>().enabled = enabled; //Enable the Collider 2D.
}
I'm note sure what your intention was, but if this doesn't work, let me know what you had in mind.
Thanks for the help! Though i found another way to solve this. By using an Effector on the 2D collider, and attaching the Platform effector 2D to the game object. This automaticaly makes the way way platforms work :)
Answer by UrielKane · Jan 09, 2017 at 09:12 AM
I don't know exactly what you are trying to achive. But i suggest two things to avoid the problem. One of course is to store the two transform you need to compare it's position (You dont want extra call's on fixedupdate). Second is to make a "state machine" wich im not sure if can be called such in this case. Anyway, if its just two posibilitys, just make a bool to see if you have to check one statement or the other. The two of them have almost the same behaviour exept for that "getkey". So make the getkey the bool for example, and if the get key is true, make one statement, if false, compute the other.
When you have several statement that look very similar with just little variations. You have to make sure to avoid them to cancel each other. And a very good way around this is with enums and bools.
Hope it's help.
Your answer
Follow this Question
Related Questions
Distance sensor 1 Answer
why != is different from var1 = !var2? 2 Answers
Check Material C# 1 Answer
IF Statement Being Ignored 3 Answers