• 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 Gaffa80 · Jan 09, 2017 at 08:24 AM · if-statementsorder-of-execution

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;
     }
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
1
Best Answer

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.

Comment
Add comment · Show 1 · 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 Gaffa80 · Jan 09, 2017 at 10:28 AM 0
Share

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 :)

avatar image
0

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.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Problem comparing contents of arrays 1 Answer

How do i make two if statements 3 Answers

Why is this simple C# script not working? 3 Answers

Trigger to play music needs to check if the same music is already playing 1 Answer

Enum Type Inventory? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges