玩家被困在地图上

时间:2015-05-27 13:04:45

标签: c# unity3d

我团结一致,第一场比赛顺利进行。

它像往常一样有玩家,外星人和地图。

有时候玩家会卡住并且不会向前移动, 即使玩家向前移动的动画运行并且他的腿继续移动但仍然没有移动

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

它是随机发生的,而不是在任何固定的位置。

我无法弄清楚为什么会发生这种情况

我试图再次制作地图,但仍然在那里

任何建议都会有很大的帮助。

ublic class Player:MonoBehaviour {

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));
}

}

由于

1 个答案:

答案 0 :(得分:0)

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

我认为,当您向刚体添加力时,您可以添加“太多”力并​​且刚体会与某些东西发生碰撞,之后会堆叠。

编辑:

检查OnCollisionEnter,OnCollisionStay和退出触发器。

http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html