Zvonimir Rudinski
Zvonimir Rudinski

Reputation: 495

Unity Rigidbody2D Velocity Sudden Freeze

I wrote a script in C# for Character Movement using Rigidbody2D velocity.However,sometimes when I try to move,my character moves then suddenly freezes and won't go forward.Only backwards.I checked the colliders and they are all equal and snapped.I tried even AddForce but it still freezes.

using UnityEngine;
using System.Collections;

public class CharacterController2D : MonoBehaviour {

[SerializeField]
float speed = 5;
[SerializeField]
float jumpForce = 500;
[SerializeField]
LayerMask whatisground;
[SerializeField]
bool isGrounded = false;
Transform groundCheck;

private Rigidbody2D rb2d;

// Use this for initialization
void Start () {
    rb2d = gameObject.GetComponent<Rigidbody2D> ();
    groundCheck = gameObject.transform.GetChild (0);
}

void FixedUpdate(){
    float hor = Input.GetAxis ("Horizontal");
    rb2d.AddForce (new Vector2 (hor * speed,0));

    //rb2d.velocity = new Vector2(hor*speed,rb2d.velocity.y);
    isGrounded = Physics2D.OverlapCircle (groundCheck.position, 0.15F);
}
// Update is called once per frame
void Update () {

}
}

Upvotes: 1

Views: 338

Answers (1)

Teun
Teun

Reputation: 175

Does your character walk from a collider to another? If that's the case, check if the intersection between the colliders in holding your character back.

Upvotes: 0

Related Questions