Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with OneWayPlatform try everthing!!!

Discussion in '2D' started by fosmark13, Jun 30, 2016.

  1. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    hi im trying to make a behaviour like ninja gaiden 3 where you can be hanging form some kind of objects and then jump trough them and landing on them and if i press a button going down from them and get back to the hanging object, so now i currently can do all of this, the problem is the jumping because everythime a jump through the platform it does ignore the collision between the player's collider and the ground collider of the platform, but the LineCast2D ground gameobject detector detects the ground and it stops my jump, yes the player is on the platform but i want that he can make a clean jump through the platform and the landing on it, so i have the next code:

    Code (CSharp):
    1. // Coroutine for ignoring oneWayPlatform after hanging
    2.  
    3.     IEnumerator ignoreOneWayPlatformWhileHanging(){
    4.  
    5.         touchingHangingArea = false;
    6.  
    7.         jumpingAfterHanging = true;
    8.  
    9.         GameObject.FindGameObjectWithTag ("jumpSFX").GetComponent<AudioSource> ().Play();
    10.  
    11.         Debug.Log ("Jumping after Hanging");
    12.  
    13.  
    14.         hangingObjects = GameObject.FindGameObjectsWithTag("hangingPlatforms");
    15.  
    16.         groundObjects = GameObject.FindGameObjectsWithTag ("groundObjects");
    17.  
    18.  
    19.         foreach(GameObject colliders in hangingObjects){
    20.  
    21.             colliders.GetComponent<Collider2D>().enabled = false;
    22.  
    23.         }
    24.  
    25.         foreach(GameObject GroundCollider in groundObjects){
    26.        
    27.  
    28.             Physics2D.IgnoreCollision (GetComponent<Collider2D>(), GroundCollider.GetComponent<Collider2D>(), true);
    29.  
    30.  
    31.         }
    32.  
    33.  
    34.        
    35.  
    36.         GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0f, jumpForceOnHanging));
    37.  
    38.  
    39.         GameObject hangingCheck = this.transform.GetChild(00).gameObject;
    40.  
    41.         hangingCheck.SetActive (false);
    42.  
    43.         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("hanging"), LayerMask.NameToLayer("player"), true);
    44.  
    45.  
    46.         GameObject hangingCheckEnd = this.transform.GetChild(30).gameObject;
    47.  
    48.         hangingCheckEnd.SetActive (false);
    49.  
    50.  
    51.         GameObject groundCheck = this.transform.GetChild(07).gameObject;
    52.  
    53.         groundCheck.SetActive (false);
    54.  
    55.         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("ground"), LayerMask.NameToLayer("player"), true);
    56.  
    57.  
    58.         GameObject oneWayCheck = this.transform.GetChild(08).gameObject;
    59.  
    60.         oneWayCheck.SetActive (false);
    61.  
    62.         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("oneWayPlatform"), LayerMask.NameToLayer("player"), true);
    63.  
    64.  
    65.  
    66.  
    67.  
    68.         yield return new WaitForSeconds(0.5f);
    69.  
    70.  
    71.  
    72.         hangingCheck.SetActive (true);
    73.  
    74.         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("hanging"), LayerMask.NameToLayer("player"), false);
    75.  
    76.  
    77.         groundCheck.SetActive (true);
    78.  
    79.         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("ground"), LayerMask.NameToLayer("player"), false);
    80.  
    81.  
    82.         hangingCheckEnd.SetActive (true);
    83.  
    84.  
    85.         oneWayCheck.SetActive (true);
    86.  
    87.         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("oneWayPlatform"), LayerMask.NameToLayer("player"), false);
    88.  
    89.  
    90.  
    91.         hangingObjects = GameObject.FindGameObjectsWithTag("hangingPlatforms");
    92.  
    93.  
    94.         foreach(GameObject colliders in hangingObjects){
    95.  
    96.             colliders.GetComponent<Collider2D>().enabled = true;
    97.  
    98.         }
    99.  
    100.         foreach(GameObject GroundCollider in groundObjects){
    101.  
    102.  
    103.             Physics2D.IgnoreCollision (GetComponent<Collider2D>(), GroundCollider.GetComponent<Collider2D>(), false);
    104.  
    105.  
    106.         }
    107.  
    108.  
    109.     }

    All this happen when i press the jump button and is hanging, like you can read here i already tried to disable the gameobjects that the LineCast2D uses to draw the lines, i tried to use the IgnoreLayerCollision (i have all the gameObjects of the player inside the Player's Layer so it has to ignore them all) there's even a IgnoreCollision between the colliders, i even tried the new Platform Defector but i have the same problem, so i don't know what else to do, i just want a clean jump through the platform. I can provide any info or video showing this. Please help!!!




     
    Last edited: Jun 30, 2016
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    If the problem is that your character is stopping at the ground level on the way up, and you want him to continue up before landing, then make sure you only do (or delay) the ground check until the character is falling (negative Y velocity) after a hanging jump.
     
  3. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    But the jump isn't supose to be stop until the coroutine finish and active the ground check? So why is stopping? the ground check is disabled so wha is that happening? Why is colliding? Thanks for your answer bro
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Sorry, I haven't had a chance to read through your code in detail.

    It's hard to say what's causing the problem. But if you think it's one of your collisions elements, then put some debug log statements in their OnCollisionEnter or OnTriggerEnter methods and see which one is getting fired. If none of them are getting fired, there must be something else applying a force or resetting velocity etc.

    Unfortunately this may be an exercise in debugging for you, as you seem to have a somewhat delicate balance of toggling components and layer switching.

    You said you're doing a linecast to check for the ground, is that linecast still firing and running logic?
     
  5. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    hi again, yes i checked all the collisions and i can see that the groundCheck is actually the one causing this "problem" because exactly when the line passes through the ground area i can check in the Inspector that the "grounded" check box is actually true, but i don't know why... because i have the gameobject attached disabled as you can read in the code, and besides the IgnoreLayerCollision isn't supose to ignore this?? i mean the groundCheck GameObject is in the Player Layer so should be ignored as well?? doesn't it?
     
  6. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    and i tried as you said, comparing the vSpeed in something like this:

    Code (CSharp):
    1. /// For Jumping when is in Hanging Area
    2.  
    3.                 if(inputDevice.Action1.WasPressed){
    4.  
    5.  
    6.                     touchingHangingArea = false;
    7.  
    8.                     jumpingAfterHanging = true;
    9.  
    10.                     GameObject.FindGameObjectWithTag ("jumpSFX").GetComponent<AudioSource> ().Play ();
    11.  
    12.                     Debug.Log ("Jumping after Hanging");
    13.  
    14.                     GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0f, jumpForceOnHanging));
    15.  
    16.  
    17.                     if (anim.GetFloat ("vSpeed") > 0.1f) {
    18.  
    19.    
    20.  
    21.                         hangingObjects = GameObject.FindGameObjectsWithTag ("hangingPlatforms");
    22.  
    23.                         groundObjects = GameObject.FindGameObjectsWithTag ("groundObjects");
    24.  
    25.  
    26.                         foreach (GameObject colliders in hangingObjects) {
    27.  
    28.                             colliders.GetComponent<Collider2D> ().enabled = false;
    29.  
    30.                         }
    31.  
    32.                         foreach (GameObject GroundCollider in groundObjects) {
    33.  
    34.  
    35.                             Physics2D.IgnoreCollision (GetComponent<Collider2D> (), GroundCollider.GetComponent<Collider2D> (), true);
    36.  
    37.  
    38.                         }
    39.                            
    40.  
    41.                         GameObject hangingCheck = this.transform.GetChild (00).gameObject;
    42.  
    43.                         hangingCheck.SetActive (false);
    44.  
    45.                         Physics2D.IgnoreLayerCollision (LayerMask.NameToLayer ("hanging"), LayerMask.NameToLayer ("player"), true);
    46.  
    47.  
    48.                         GameObject hangingCheckEnd = this.transform.GetChild (30).gameObject;
    49.  
    50.                         hangingCheckEnd.SetActive (false);
    51.  
    52.  
    53.                         GameObject groundCheckObject = this.transform.GetChild (07).gameObject;
    54.  
    55.                         groundCheckObject.SetActive (false);
    56.  
    57.                         Physics2D.IgnoreLayerCollision (LayerMask.NameToLayer ("ground"), LayerMask.NameToLayer ("player"), true);
    58.  
    59.  
    60.                         GameObject oneWayCheck = this.transform.GetChild (08).gameObject;
    61.  
    62.                         oneWayCheck.SetActive (false);
    63.  
    64.                         Physics2D.IgnoreLayerCollision (LayerMask.NameToLayer ("oneWayPlatform"), LayerMask.NameToLayer ("player"), true);
    65.  
    66.                     }
    67.  
    68.                     else if (anim.GetFloat ("vSpeed") < -0.1f) {
    69.  
    70.  
    71.                         GameObject hangingCheck = this.transform.GetChild (00).gameObject;
    72.  
    73.                         groundObjects = GameObject.FindGameObjectsWithTag ("groundObjects");
    74.  
    75.  
    76.                         hangingCheck.SetActive (true);
    77.  
    78.                         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("hanging"), LayerMask.NameToLayer("player"), false);
    79.  
    80.  
    81.                         GameObject groundCheckObject = this.transform.GetChild (07).gameObject;
    82.  
    83.                         groundCheckObject.SetActive (true);
    84.  
    85.                         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("ground"), LayerMask.NameToLayer("player"), false);
    86.  
    87.  
    88.                         GameObject hangingCheckEnd = this.transform.GetChild (30).gameObject;
    89.  
    90.                         hangingCheckEnd.SetActive (true);
    91.  
    92.  
    93.                         GameObject oneWayCheck = this.transform.GetChild (08).gameObject;
    94.  
    95.                         oneWayCheck.SetActive (true);
    96.  
    97.                         Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("oneWayPlatform"), LayerMask.NameToLayer("player"), false);
    98.  
    99.  
    100.  
    101.                         hangingObjects = GameObject.FindGameObjectsWithTag("hangingPlatforms");
    102.  
    103.  
    104.                         foreach(GameObject colliders in hangingObjects){
    105.  
    106.                             colliders.GetComponent<Collider2D>().enabled = true;
    107.  
    108.                         }
    109.  
    110.                         foreach(GameObject GroundCollider in groundObjects){
    111.  
    112.  
    113.                             Physics2D.IgnoreCollision (GetComponent<Collider2D>(), GroundCollider.GetComponent<Collider2D>(), false);
    114.  
    115.  
    116.                         }
    117.                    
    118.                    
    119.                     }
    it does exactly as the coroutine method that i had before and like before... the jump stops!!!

    thanks!!!
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    On which object, and with kind of logic is "grounded" set to true? If that logic is still running, your problem is definitely related to how you implemented the grounded check, as it's not being disabled by the code you posted. So you probably either need to do further disabling to stop that check, or change the way you do you grounded check so that it respects the layers and physics toggles properly.
     
  8. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    Hi there, so how can i disable the LineCast2D ? To make it disapear while the player jumps, how can i code that?
     
  9. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Well, there are a few ways you can achieve that.

    Disable the script that is running the check during a jump. If you can't do that, maybe only do the linecast if the character has a negative Y velocity.

    You could keep a boolean which is checked before the linecast, and toggle it whenever you need.

    Another way would be to pass the LineCast2D a LayerMask, populated with the layers that it should look for, and then bitshift the mask to add/remove layers it should hit.

    Without knowing exactly how you have structured your code, it's hard to say what the best approach would be. Hopefully that gives you some ideas.
     
  10. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    i tought about the change the layer mask to a different one or remove it while is jumping but i don't know how to do that?? i mean how can i code it?? can you give me an example of this option in code please??
     
  11. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    Hi there again i was experimenting and i found one answer to this problem, i just put a If statement over where i defined what is ground like this:

    Code (CSharp):
    1.     if (!jumpingAfterHanging) {
    2.  
    3.  
    4.             grounded = Physics2D.Linecast (groundCheck.position, groundCheckEnd.position, whatIsGround);
    5.  
    6.  
    7.         }
    So when is hanging the JumpingAfterHanging boolean gets true and the lineCast does not happen until the coroutine finishes and then gets the boolean back to false, this works great!!! i may implement the negative Y axis if later this brings problems but for now it does the job, what do you think of this mehotd @jeffreyschoch ??
     
    LiterallyJeff likes this.
  12. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    fosmark13 likes this.
  13. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    LiterallyJeff likes this.
  14. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Unity also has a way to make 1-way platforms.
    1) Add component PlatformEffector2D.
    2) From BoxCollider2D set "UsedByEffector" to true.
     
  15. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    Hi thanks for your answer but i said in my first post that i use the platform effector as well but with the same result but i already resolved thanks!!!