C# 玩家被卡在地图上

C# 玩家被卡在地图上,c#,unity3d,C#,Unity3d,我在unity打了我的第一场比赛,比赛进行得很顺利 它像往常一样有一个玩家、外星人和一张地图 有时玩家被卡住,无法向前移动, 即使玩家向前移动的动画运行,他的腿不断移动,但仍然不移动 我必须向其他方向移动,然后它可以通过它卡住的那个点 它是随机发生的,而不是在任何固定地点 我不知道为什么会这样 我试着再做一次地图,但它仍然在那里 任何建议都会大有帮助 公共职业玩家:单行为{ public float speed = 10f; public Vector2 maxVelocity = new Ve

我在unity打了我的第一场比赛,比赛进行得很顺利

它像往常一样有一个玩家、外星人和一张地图

有时玩家被卡住,无法向前移动, 即使玩家向前移动的动画运行,他的腿不断移动,但仍然不移动

我必须向其他方向移动,然后它可以通过它卡住的那个点

它是随机发生的,而不是在任何固定地点

我不知道为什么会这样

我试着再做一次地图,但它仍然在那里

任何建议都会大有帮助

公共职业玩家:单行为{

public float speed = 10f;
public Vector2 maxVelocity = new Vector2(3, 5);
public bool standing;
public float jetSpeed = 15f;
public float airSpeedMultiplier = .3f;
public AudioClip leftFootSound;
public AudioClip rightFootSound;
public AudioClip thudSound;
public AudioClip rocketSound;
public Vector3 PlayerDirection = new Vector3(1,1,1);
public int ArtifactCount = 0;

private Animator animator;
private PlayerController controller;

void Start(){
    controller = GetComponent<PlayerController> ();
    animator = GetComponent<Animator> ();
}



void PlayLeftFootSound(){
    if (leftFootSound)
        AudioSource.PlayClipAtPoint (leftFootSound, transform.position);
}

void PlayRightFootSound(){
    if (rightFootSound)
        AudioSource.PlayClipAtPoint (rightFootSound, transform.position);
}

void PlayRocketSound(){
    if (!rocketSound || GameObject.Find ("RocketSound"))
        return;

    GameObject go = new GameObject ("RocketSound");
    AudioSource aSrc = go.AddComponent<AudioSource> ();
    aSrc.clip = rocketSound;
    aSrc.volume = 0.7f;
    aSrc.Play ();

    Destroy (go, rocketSound.length);

}

void OnCollisionEnter2D(Collision2D target){
    if (!standing) {
        var absVelX = Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x);
        var absVelY = Mathf.Abs(GetComponent<Rigidbody2D>().velocity.y);

        if(absVelX <= .1f || absVelY <= .1f){
            if(thudSound)
                AudioSource.PlayClipAtPoint(thudSound, transform.position);
        }
    }

}

// Update is called once per frame
void Update () {
    var forceX = 0f;
    var forceY = 0f;

    var absVelX = Mathf.Abs (GetComponent<Rigidbody2D>().velocity.x);
    var absVelY = Mathf.Abs (GetComponent<Rigidbody2D>().velocity.y);

    if (absVelY < .2f) {
        standing = true;
    } else {
        standing = false;
    }

    if (controller.moving.x != 0) {
        if (absVelX < maxVelocity.x) {

            forceX = standing ? speed * controller.moving.x : (speed * controller.moving.x * airSpeedMultiplier);

            PlayerDirection = transform.localScale = new Vector3 (forceX > 0 ? 1 : -1, 1, 1);

        }
        animator.SetInteger ("AnimState", 1);
    } else {
        animator.SetInteger ("AnimState", 0);
    }

    if (controller.moving.y > 0) {
        PlayRocketSound();
                    if (absVelY < maxVelocity.y)
                            forceY = jetSpeed * controller.moving.y;

                    animator.SetInteger ("AnimState", 2);
            } else if (absVelY > 0) {
        animator.SetInteger("AnimState", 3);
            }

    GetComponent<Rigidbody2D>().AddForce (new Vector2 (forceX, forceY));
}
公共浮动速度=10f;
公共向量2 maxVelocity=新向量2(3,5);
公众利益;
公共浮子喷射速度=15f;
公共浮子空速倍增管=.3f;
公共音频剪辑leftFootSound;
公共音频剪辑rightFootSound;
公共音频剪辑重击声;
公共音频剪辑rocketSound;
公共向量3 PlayerDirection=新向量3(1,1,1);
public int ArtifactCount=0;
私人动画师;
专用播放控制器;
void Start(){
controller=GetComponent();
animator=GetComponent();
}
void PlayLeftFootSound(){
if(左足音)
AudioSource.PlayClipAtPoint(leftFootSound,transform.position);
}
void PlayRightFootSound(){
if(右脚声)
AudioSource.PlayClipAtPoint(右脚声音、变换、位置);
}
void PlayRocketSound(){
如果(!rocketSound | | GameObject.Find(“rocketSound”))
回来
GameObject go=新游戏对象(“RocketSound”);
AudioSource aSrc=go.AddComponent();
aSrc.clip=rocketSound;
aSrc.体积=0.7f;
Play();
破坏(前进,火箭声,长度);
}
无效OnCollisionInter2D(碰撞2D目标){
如果(!站着){
var absVelX=Mathf.Abs(GetComponent().velocity.x);
var absVelY=Mathf.Abs(GetComponent().velocity.y);
if(absVelX 0){
PlayRocketSound();
if(绝对<最大速度y)
forceY=jetSpeed*controller.moving.y;
animator.SetInteger(“AnimState”,2);
}否则如果(绝对值>0){
animator.SetInteger(“AnimState”,3);
}
GetComponent().AddForce(新向量2(forceX,forceY));
}
}


谢谢

您应该检查您要去的地点是否在地图之外

我认为当你向刚体添加力时,你可能会添加“太多”的力,刚体会与某个物体碰撞,然后会堆积起来

编辑:

检查OnCollisionCenter、OnCollisionStay并同时退出触发器


建议:使用调试器查看程序运行不符合预期时发生的情况。由于“有时”会发生这种情况,因此除了您之外,没有人可以复制这种情况。此外,如果没有看到您的应用程序代码,则不可能做出任何假设。这可能会发布在特定的Unity/Games StackExchange上。我不确定是否有人可以帮助您,因为我们对您的代码一无所知。描述代码是不必要的。我应该发布代码的哪一部分?地图没有任何代码,将粘贴玩家代码物理代码应该放在FixedUpdate中。不,玩家只停留在地图中,有两层,因此它不能超出边界,堆叠的事情也在发生,但在这种情况下,玩家没有对象,只有地图设计,其余的地方是空的,因此它不会与任何东西发生碰撞,如果co玩家的角色在墙对撞机内,你会遇到这样的问题。你可以尝试通过使用transform.position移动AddForce来改变它