Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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.

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

Distance sensor 1 Answer

why != is different from var1 = !var2? 2 Answers

How to add two if statements to one result. (Flashlight being on, and pressing "F", to turn off the flashlight.) 3 Answers

Check Material C# 1 Answer

IF Statement Being Ignored 3 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