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. Dismiss Notice

Why does it not change?

Discussion in 'Scripting' started by Bloodlister, Oct 4, 2014.

  1. Bloodlister

    Bloodlister

    Joined:
    May 18, 2014
    Posts:
    6
    So I am trying to make a endless jumping game and I have a issue with my code. It awlays sets the rightWallJump = true even when it is hit from the other side. So to say it simply when the character is hit from the right side with a wall it jumps and is pushed to the left like it should BUT if it is hit from the LEFT with a wall and tries to jump it jumps upwards but is again pushed to the left.

    Can you guys explain why it never sets leftWallJump to True?

    Code (JavaScript):
    1.  
    2. var sideJumpHeight : int = 7;
    3. var sidePush : int =3;
    4. var leftWallJump : boolean = false;
    5. var rightWallJump : boolean = false;
    6. var jumpKey : KeyCode;
    7.  
    8. function Start () {}
    9.  
    10. function Update ()
    11. {
    12.     //For getting pushed left when jumping from the right side of the wall.
    13.  
    14.     if(Input.GetKey(jumpKey) && leftWallJump == true)
    15.     {
    16.         rigidbody2D.velocity.y = sideJumpHeight;
    17.         rigidbody2D.velocity.x = sidePush;
    18.         leftWallJump = false;
    19.     }
    20.    //For getting pushed right when jumping from the left side of the wall.
    21.     else if(Input.GetKey(jumpKey) && rightWallJump == true)
    22.     {
    23.         rigidbody2D.velocity.y = sideJumpHeight;
    24.         rigidbody2D.velocity.x = -sidePush;
    25.         rightWallJump = false;
    26.     }
    27. }
    28.  
    29. function OnCollisionEnter2D(info : Collision2D)
    30. {
    31. //Send a ray to the right to check for a wall.
    32. var hitRight : RaycastHit2D = Physics2D.Raycast(transform.position,Vector2(1,0),0.1);
    33.  
    34. //Send a ray to the left to check for a wall.
    35. var hitLeft : RaycastHit2D = Physics2D.Raycast(transform.position,-Vector2(1,0),-0.1);
    36.  
    37. //If the character collides with a object with the tag wall and the ray cast to the LEFT is hitting something
    38. //then give me the ability to jump and be pushed to the Right.
    39.      if(info.gameObject.tag == "Wall"  && hitLeft != null)
    40.     {
    41.         leftWallJump = true;
    42.     }
    43.  
    44. //if the character collides with a object with the tag wall and the ray cast to the RIGHT is hitting something
    45. //then give me the ability to jump and be pushed to the Left.
    46.     else if (info.gameObject.tag == "Wall" && hitRight != null)
    47.     {
    48.         rightWallJump = true;
    49.     }
    50.  
    51. }
    52.  
    53. function OnCollisionExit2D()
    54. {
    55.     leftWallJump = false;
    56.     rightWallJump = false;
    57. }
    58.  
     
  2. Fyko-chan

    Fyko-chan

    Joined:
    Sep 29, 2012
    Posts:
    76
    My guess is because you set the direction to left, then set it to a negative distance, making it go right.
    Code (CSharp):
    1. var hitLeft : RaycastHit2D = Physics2D.Raycast(transform.position,-Vector2(1,0),0.1);
     
  3. Bloodlister

    Bloodlister

    Joined:
    May 18, 2014
    Posts:
    6
    I went back and changed it and i still have the same issue.