Search Unity

Unity 2D Character controller bug problem

Discussion in '2D' started by Polpettone, Oct 31, 2014.

  1. Polpettone

    Polpettone

    Joined:
    Jun 30, 2014
    Posts:
    4
    Hi I have a problem with this script,the script is not bad but there are a bug where if you press A + Space and after D or the reverse,the player increases the speed, and does not change direction,please someone that help to solve this problem? The code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class CharacterController2D : MonoBehaviour {
    4.      public float upSpead;
    5.      public float jumpSpead;
    6.      public bool jumping = false;
    7.      public bool grounded = false;
    8.      public string sidesHit = "none";
    9.      public Vector2 down;
    10.      public Vector2 sides;
    11.      public Vector2 sideLeft;
    12.      public float skinWidth = 0.1f;
    13.      public float InitalAcceleration = 500;
    14.      public float airspeed = 500;
    15.      public bool maxSpeed = false;
    16.      Vector2 start;
    17.      RaycastHit2D hit;
    18.      void Start(){
    19.          //start = transform.position;
    20.          //start.y = transform.position.y - transform.localScale.y/2f;
    21.          }
    22.      void Update () {
    23.      //Basic ground checking code with Physics2d raycasting
    24.          sideLeft = transform.position;
    25.          sideLeft.x = transform.position.x - transform.localScale.x / 2 - 0.1f;
    26.          sides = transform.position;
    27.          sides.x = transform.position.x + transform.localScale.x / 2 + 0.1f;
    28.          start = transform.position;
    29.          start.y = transform.position.y - transform.localScale.y/2f - 0.1f;
    30.          Debug.DrawRay (start, -Vector2.up);
    31.          Debug.DrawRay (new Vector2(start.x - transform.localScale.x/2, start.y), -Vector2.up);
    32.          Debug.DrawRay (new Vector2(start.x + transform.localScale.x/2, start.y), -Vector2.up);
    33.          Debug.DrawRay (sides, Vector2.right);
    34.          Debug.DrawRay (sideLeft, -Vector2.right);
    35.          if (Physics2D.Raycast (start, -Vector2.up, skinWidth).collider != null || Physics2D.Raycast (new Vector2(start.x + transform.localScale.x/2, start.y), -Vector2.up, skinWidth).collider != null || Physics2D.Raycast (new Vector2(start.x - transform.localScale.x/2, start.y), -Vector2.up, skinWidth).collider != null) {
    36.                          grounded = true;
    37.                  } else {
    38.              grounded = false;
    39.                  }
    40.    
    41.          if (Input.GetKeyUp(KeyCode.A) && grounded == true) {
    42.              //Detect when certain keys are released to reset velocity
    43.              //Reset the velocity to a number close to 0 to make a sudden stop, but ease out to fell smoother
    44.              rigidbody2D.velocity = new Vector2(-2,0);
    45.          }
    46.          if (Input.GetKeyUp(KeyCode.D) && grounded == true) {
    47.              rigidbody2D.velocity = new Vector2(2,0);
    48.          }
    49.          //Jumping Trigger
    50.          if (Input.GetKeyDown (KeyCode.Space)) {
    51.              if(grounded == true){
    52.              jumping = true;
    53.                  //Add the Initial Accleration to make the player shoot up, and then slow down, then fall
    54.                  rigidbody2D.AddForce(new Vector2(0,InitalAcceleration));
    55.              }
    56.                  }
    57.          if (Input.GetKeyUp (KeyCode.Space)) {
    58.              jumping = false;
    59.          }
    60.          if (Input.GetKey (KeyCode.Space)) {
    61.              if(jumping ==true){
    62.                  //Detect if speed limit is reached, and then fall back down
    63.              if(rigidbody2D.velocity.y >=10){
    64.                  jumping = false;
    65.                  }
    66.                  rigidbody2D.AddForce(new Vector2(0,jumpSpead * Time.deltaTime));
    67.                  }
    68.              }
    69.          //Detect sides by raycastting. Possibly can be used for wall jumping
    70.          if (Physics2D.Raycast (sides, Vector2.right, skinWidth).collider != null) {
    71.                          sidesHit = "right";
    72.          } else if (Physics2D.Raycast (sideLeft, -Vector2.right, skinWidth).collider != null) {
    73.              sidesHit = "left";
    74.                  }
    75.              else {
    76.              sidesHit = "none";
    77.            
    78.          }
    79.          Move ();
    80.      }
    81.      void Move(){
    82.          //Move in air code
    83.          if (Input.GetAxisRaw ("Horizontal") < 0 && grounded == false && maxSpeed == true) {
    84.              rigidbody2D.AddForce(new Vector2(airspeed * Time.deltaTime,0));
    85.                  }
    86.          else if (Input.GetAxisRaw ("Horizontal") > 0 && grounded == false && maxSpeed == true) {
    87.              rigidbody2D.AddForce(new Vector2(-airspeed * Time.deltaTime,0));
    88.          }
    89.          //Move Left/Right Code
    90.          if (rigidbody2D.velocity.x >= 9 || rigidbody2D.velocity.x <= -9) {
    91.                          maxSpeed = true;
    92.                  } else {
    93.              if(maxSpeed == true){
    94.                  maxSpeed = false;
    95.              }
    96.                  }
    97.        
    98.          if (Input.GetAxisRaw ("Horizontal") < 0 && sidesHit != "left") {
    99.              if (maxSpeed == false && grounded == false) {
    100.                  rigidbody2D.AddForce(new Vector2(-airspeed * Time.deltaTime,0));
    101.                  }else if(maxSpeed == false){
    102.              rigidbody2D.AddForce(new Vector2(-upSpead * Time.deltaTime,0));
    103.                  }
    104.              }
    105.          if (Input.GetAxisRaw ("Horizontal") > 0 && sidesHit != "right") {
    106.              if (maxSpeed == false && grounded == false && Input.GetAxisRaw ("Horizontal") < 0) {
    107.                  rigidbody2D.AddForce(new Vector2(airspeed * Time.deltaTime,0));
    108.              }else if(maxSpeed == false){
    109.              rigidbody2D.AddForce(new Vector2(upSpead * Time.deltaTime,0));
    110.              }
    111.          }
    112.      }
    113. }
    114.  
    Download Project: https://github.com/Mimerme/SuperMeatyController2D/archive/master.zip
     
    Last edited: Oct 31, 2014