This question was
closed Mar 15, 2017 at 03:22 PM by
SisterRay for the following reason:
The question is answered, right answer was accepted
Question by
SisterRay · Mar 15, 2017 at 03:03 PM ·
2drigidbody2dvelocityrunner
Player randomly stops moving
Hi, I'm making an infinite 2D runner and have a problem when player's velocity randomly drops to zero and if I hit any button (jump or fire) he continues to move but stop again after a while. Here's a code with player controls.
public class PlayerControl : MonoBehaviour {
bool isGrounded = false;
public Transform[] groundCheck;
public float groundRadius;
public LayerMask whatIsGround;
public float speed;
public float jumpForce;
public GameObject bullet;
private bool jump = true;
private bool grounded;
private Rigidbody2D player;
void Start () {
player = GetComponent<Rigidbody2D> ();
}
void Shoot (){
Instantiate (bullet, transform.position, Quaternion.identity);
}
void Update(){
if (Input.GetKeyDown(KeyCode.Space))
jump = false;
if (Input.GetKeyDown(KeyCode.Mouse0)){
Shoot();
}
}
void FixedUpdate () {
player.velocity = new Vector2 (speed, player.velocity.y);
foreach (Transform point in groundCheck) {
Collider2D[] colliders = Physics2D.OverlapCircleAll (point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++) {
if (colliders [i].gameObject != gameObject)
isGrounded = true;
}
}
if (!jump && isGrounded) {
player.AddForce (new Vector2 (0f, jumpForce));
isGrounded = false;
jump = true;
}
}
}
Do you have any ideas how to solve this ??
Comment
Best Answer
Answer by SisterRay · Mar 15, 2017 at 03:21 PM
Nevermind. The problem was in some badly instantiated colliders.