【问题标题】:Player getting stuck on the map玩家卡在地图上
【发布时间】:2015-05-27 13:04:44
【问题描述】:

我在unity中制作了我的第一款游戏,运行流畅。

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

有时玩家会被卡住而无法前进, 即使玩家向前移动的动画运行,并且他的腿一直在移动但它仍然没有移动

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

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

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

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

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

公共类播放器: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));
}

}

谢谢

【问题讨论】:

  • 建议:使用调试器查看程序运行不正常时发生的情况。这里没有人可以重现这个,因为它“有时”会发生。此外,如果不查看您的应用程序代码,也无法做出任何假设。
  • 这可能应该发布在特定的 Unity/Games stackexchange 上
  • 我不确定谁能帮助您,因为我们对您的代码一无所知。 描述代码是不必要的。
  • 我应该发布代码的哪一部分?该地图没有任何代码,将粘贴玩家代码
  • 物理代码应该放在FixedUpdate中。

标签: c# unity3d


【解决方案1】:

你应该检查你要去的点不在你的地图之外。

我认为,当您向刚体添加力时,您可能会添加“太多”力,然后刚体会与某物发生碰撞,然后它就会堆叠起来。

编辑:

检查 OnCollisionEnter、OnCollisionStay 和 Exit 以及触发器。

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

【讨论】:

  • 没有玩家只停留在地图中,已经制作了两层,所以它不能超出边界并且堆叠的事情也发生了,但在这种情况下,只有玩家没有对象地图设计和其他地方是空的,所以它不能与任何东西发生碰撞
  • 如果玩家的对撞机在墙壁对撞机内,那么你会遇到这种问题。您可以尝试通过使用 transform.position 移动来更改 AddForce
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多