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

Small character controller issue ( c# )

Discussion in '2D' started by Loff, Aug 12, 2014.

  1. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Hello,

    The player controller seems to work pretty good besides one thing. If the player runs into a wall, the player speed is still going up to maxSpeed even if the character doesn't move past the wall. Because the speed isn't 0 the player won't fall so the player is kept in the air until the key is released.

    Any ideas how I can fix this? a raycast against the walls? I'm not that good with scripting yet so please bear with me :)

    Code (CSharp):
    1.     void FixedUpdate () {
    2.  
    3.    
    4.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius,whatIsGround);
    5.         anim.SetBool("Ground", grounded);
    6.  
    7.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    8.  
    9.         float move = Input.GetAxis ("Horizontal");
    10.  
    11.         anim.SetFloat("Speed",Mathf.Abs (move));
    12.  
    13.         rigidbody2D.velocity = new Vector2 (move*maxSpeed, rigidbody2D.velocity.y);
    14.  
    15.         if(move > 0 && !facingRight) {
    16.             Flip(); }
    17.             else if( move <0 && facingRight){
    18.             Flip ();
    19.         }
    20.  
    21.  
    22.         }
    23.     //FixedUpdate end
    24.  
    25.     void Update(){
    26.         if (grounded && Input.GetKeyDown("w") ) {
    27.             anim.SetBool("Ground", false);
    28.             rigidbody2D.AddForce (new Vector2 (0,jumpForce));
    29.         }
    30.  
     
  2. Mark_Wilson

    Mark_Wilson

    Joined:
    Jan 6, 2013
    Posts:
    112
    you could put a tag on your wall and then simply say -
    if player collides with the wall tag{
    player.move = 0;
    }
    If you need to know how to say this in c# code I can give it to you, I just cant remember it off the top of my head as i'm a newbie too :p
     
  3. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    could work, but I don't use "only walls". I use the ground layer I made. So it would be easier to have some sort of *raycast* in front of the player that checks if he can walk foward or not depening on it being a collision from the ground layer in front of him :)
     
    Last edited: Aug 13, 2014
  4. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    maxSpeed isn't your problem.

    Consider the real-world senario of a car up against a brick wall with its tires moving at maximum speed but the car is not going anywhere.

    Your issue is likely with friction. If 2 objects have 100% friction they will stick together if a force is pushing them together.

    Do this:

    Assets > Create > Physics2D Material.

    Name your material something logical like "SlipperyBody" or "FrictionlessBody" Set Friction and Bounciness both to zero.

    Apply that physics material to your Player's colliders by dragging it it's 'material' window. That should fix your issues.
     
    Loff likes this.
  5. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Wow, that was a easy way to fix it. Thanks a lot!
     
    Last edited: Aug 17, 2014