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

Player stuck on walls and/or colliders

Discussion in '2D' started by Dirt410, Jun 28, 2014.

  1. Dirt410

    Dirt410

    Joined:
    Jun 28, 2014
    Posts:
    4
    Creating a game (duh) but I've run into a small "bug". It's not "game breaking" but I would like to fix it "relatively soon". Am I using too many "quotation marks"?

    The problem is when the player jumps into a wall or an object with a collider and keeps moving forward/walking, they become stuck in the object until the walk button is let go.

    It also seems to affect when walking into an object with a circle collider, in which case the player magically walks up the object.
    The video shows what I mean.



    Here is my player controller script. I'm fairly new to Unity and I have little to no programming experience, merely been cobbling together code from different tutorials around the web. Any and all help would be greatly appreciated. <3

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControl : MonoBehaviour
    5. {
    6.     public float maxSpeed = 10f;
    7.     bool facingRight = true;
    8.  
    9.     Animator Anim;
    10.  
    11.     bool grounded = false;
    12.     public Transform groundCheck;
    13.     float groundRadius = 0.2f;
    14.     public LayerMask whatIsGround;
    15.     public float jumpForce = 125f;
    16.  
    17.     void Start () {
    18.  
    19.         Anim = GetComponent<Animator> ();
    20.     }
    21.  
    22.     void FixedUpdate () {
    23.  
    24.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    25.         Anim.SetBool ("Ground", grounded);
    26.  
    27.         Anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    28.  
    29.  
    30.  
    31.  
    32.  
    33.         float move = Input.GetAxis ("Horizontal");
    34.  
    35.         Anim.SetFloat ("Speed", Mathf.Abs (move));
    36.  
    37.         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    38.  
    39.     if (move > 0 &&!facingRight)
    40.                         Flip ();
    41.                 else if (move < 0 && facingRight)
    42.                         Flip ();
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         if(grounded && Input.GetKey ("space")) //CHANGE GETKEYDOWN
    48.  
    49.         {
    50.             Anim.SetBool("Ground", false);
    51.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    52.      
    53.         }
    54.  
    55.         }
    56.  
    57.     void Flip()
    58.     {
    59.         facingRight = !facingRight;
    60.         Vector3 theScale = transform.localScale;
    61.         theScale.x *= -1;
    62.         transform.localScale = theScale;
    63.  
    64.     }
    65. }
    66.  
     
    Last edited: Jun 28, 2014
  2. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Make a Physics2D material and set the friction to be 0. Attach it to your object that youre having trouble with.
     
    Spidlee and Dirt410 like this.
  3. Dirt410

    Dirt410

    Joined:
    Jun 28, 2014
    Posts:
    4

    It worked. Thank you.
     
  4. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Well, im glad to hear that.:)