Search Unity

Best method for the user to alter gravity?

Discussion in '2D' started by FalsAlarm, Dec 27, 2016.

  1. FalsAlarm

    FalsAlarm

    Joined:
    Dec 12, 2016
    Posts:
    21
    Below is my code. the problem i'm having is that the triggers sometimes don't happen.

    Code (CSharp):
    1.    void OnTriggerEnter2D(Collider2D col)
    2.     {
    3.         print("trig hit!" + col.gameObject.name);
    4.  
    5.         switch(col.gameObject.name)
    6.         {
    7.             case "LeftImage":
    8.                 print("Left Image Hit!");
    9.                 //UnityEngine.Physics.gravity = new Vector3(-10,0,0);
    10.                 Physics2D.gravity = new Vector3(-80f,0f,0f);
    11.                 break;
    12.             case "RightImage":
    13.                 print("Right Image Hit!");
    14.                 //UnityEngine.Physics.gravity = new Vector3(-10,0,0);
    15.                 Physics2D.gravity = new Vector3(80f,0f,0f);
    16.                 break;
    17.             case "TopImage":
    18.                 print("Top Image Hit!");
    19.                 //UnityEngine.Physics.gravity = new Vector3(-10,0,0);
    20.                 Physics2D.gravity = new Vector3(0f,80f,0f);
    21.                 break;
    22.             case "BottomImage":
    23.                 print("Bottom Image Hit!");
    24.                 //UnityEngine.Physics.gravity = new Vector3(-10,0,0);
    25.                 Physics2D.gravity = new Vector3(0f,-80f,0f);
    26.                 break;
    27.         }
    28. }
    29.  
     
    Last edited: Dec 27, 2016
  2. FalsAlarm

    FalsAlarm

    Joined:
    Dec 12, 2016
    Posts:
    21
    I have four large triangle images with polygon colliders that i'm using to trigger the gravity change.
     
  3. Hiinas

    Hiinas

    Joined:
    Dec 30, 2016
    Posts:
    21
    When you say it doesn't always trigger, is it the gravity that doesn't change or is it the trigger that doesn't trigger?
    The way you change gravity is quite simple and should be working from what I can see. Just make sure your case strings are correct.