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

Wall collision when jumping in 2D demo

Discussion in '2D' started by majlan, Feb 6, 2014.

  1. majlan

    majlan

    Joined:
    Dec 30, 2013
    Posts:
    12
    I downloaded Tower Bridge demo to find out how to create some 2D game basics and I used some scripts etc. But I can't achieve this effect - in demo, when you jump up and right AND you hit the wall, the player slides down to the ground. However the same doesn't happen in my game. I use simple 2D box colliders on my walls and my player controller script is following:

    Code (csharp):
    1. public class PlayerScript : MonoBehaviour {
    2.     [HideInInspector]
    3.     public bool facingRight = true;
    4.  
    5.     public float moveForce = 365f;
    6.     public float jumpForce = 200f;
    7.     public float maxSpeed = 5f;
    8.  
    9.     private Transform groundCheck;
    10.     private bool onGround;
    11.     private bool jump;
    12.  
    13.     void Awake() {
    14.         groundCheck = transform.Find("groundCheck");
    15.     }
    16.  
    17.     void Update() {
    18.         onGround = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    19.         if(Input.GetButtonDown("Jump")  onGround) {
    20.             jump = true;
    21.         }
    22.     }
    23.  
    24.  
    25.     void FixedUpdate() {
    26.         float h = Input.GetAxis("Horizontal");
    27.  
    28.         if(h * rigidbody2D.velocity.x < maxSpeed) {
    29.             rigidbody2D.AddForce(Vector2.right * h * moveForce);
    30.         }
    31.  
    32.         if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed) {
    33.             rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
    34.         }
    35.  
    36.         if(jump) {
    37.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    38.             jump = false;
    39.         }
    40.  
    41.         if (h > 0  !facingRight) {
    42.             Flip();
    43.         } else if (h < 0  facingRight) {
    44.             Flip();
    45.         }          
    46.     }
    47.    
    48.     void Flip() {
    49.         // Switch the way the player is labelled as facing.
    50.         facingRight = !facingRight;
    51.        
    52.         // Multiply the player's x local scale by -1.
    53.         Vector3 theScale = transform.localScale;
    54.         theScale.x *= -1;
    55.         transform.localScale = theScale;
    56.     }
    57. }
    Yet the player object is sticked to the wall as long as I hold the keys down. Thank you for any advice.

    $playerscreenshot.jpg
     
    Last edited: Feb 6, 2014
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Easy fix, just give your walls a physics material with no friction or bounce and it won't stick to the wall.
     
  3. majlan

    majlan

    Joined:
    Dec 30, 2013
    Posts:
    12
    Thank you very much, sir!