简体   繁体   中英

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 () {

}
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM