简体   繁体   English

Unity Rigidbody2D Velocity Sudden Freeze

[英]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. 我使用Rigidbody2D速度在C#中编写了一个用于角色运动的脚本。但是,有时当我试图移动时,我的角色移动然后突然冻结并且不会向前移动。只是向后移动。我检查了碰撞器并且它们都是相等的并且被啪地一声。我甚至试过AddForce但它仍然冻结。

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. 如果是这种情况,请检查碰撞器之间的交叉点是否有角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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