Search Unity

Bug-fixing. Endless running in my game after hitting a collider and moving again against it

Discussion in 'Scripting' started by Foose, Jul 31, 2014.

  1. Foose

    Foose

    Joined:
    Jul 3, 2014
    Posts:
    21
    hey guys, so I have set up an alpha version of my WIP game to let my friends test it for bugs. They found out that there is something wrong with my collision script. When they move the character to another collider (e.g. fence) and press the button multiple times fast in a row, the character is starting to run again. against the fence but you cannot move it anymore. I hope you guys can help me. I post the link to my game as well as the code.
    Game: http://nubz.parseapp.com/

    charMovement script:
    Code (CSharp):
    1.     void FixedUpdate ()
    2.     {
    3.  
    4.         if (Input.GetKeyDown (KeyCode.LeftArrow) && rigidbody.isKinematic == true){
    5.             rigidbody.isKinematic = false;
    6.             rigidbody.velocity = new Vector3(0, 0, speed);
    7.             transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
    8.             anim.speed = animSpeed;  
    9.             anim.SetFloat("Speed", animSpeed);
    10.            
    11.         }
    12.         if (Input.GetKeyDown (KeyCode.RightArrow) && rigidbody.isKinematic == true){
    13.             rigidbody.isKinematic = false;
    14.             rigidbody.velocity = new Vector3(0, 0, -speed);
    15.             transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
    16.             anim.speed = animSpeed;  
    17.             anim.SetFloat("Speed", animSpeed);
    18.            
    19.            
    20.         }
    21.         if (Input.GetKeyDown (KeyCode.UpArrow) && rigidbody.isKinematic == true){
    22.             rigidbody.isKinematic = false;
    23.             rigidbody.velocity = new Vector3(speed, 0, 0);
    24.             transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
    25.             anim.speed = animSpeed;  
    26.             anim.SetFloat("Speed", animSpeed);
    27.            
    28.            
    29.         }
    30.         if (Input.GetKeyDown (KeyCode.DownArrow) && rigidbody.isKinematic == true){
    31.             rigidbody.isKinematic = false;
    32.             rigidbody.velocity = new Vector3(-speed, 0, 0);
    33.             transform.rotation = Quaternion.LookRotation(rigidbody.velocity);
    34.             anim.speed = animSpeed;  
    35.             anim.SetFloat("Speed", animSpeed);
    36.         }
    37.        
    38.        
    39.  
    40.     }
    collision script that is attached to every object at the field:
    Code (CSharp):
    1.     private Animator anim;
    2.  
    3.     void OnCollisionEnter ( Collision collision) {
    4.         if (collision.gameObject.CompareTag ("Player")) {
    5.             anim = collision.gameObject.GetComponent<Animator>();
    6.             anim.SetFloat("Speed", 0);
    7.             collision.rigidbody.isKinematic = true;
    8.             }
    9.         }
    10.     void OnCollisionExit ( Collision collision) {
    11.         collision.rigidbody.isKinematic = true;
    12.         }
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599