【问题标题】:Unity2D: Player continues to move after he respawnsUnity2D:玩家重生后继续移动
【发布时间】:2016-07-22 19:00:05
【问题描述】:

好吧,简单来说,我有一个 2d 自上而下的点击移动游戏,但有一个小问题。你看我创造了我的玩家有三个心脏生命,当你被一个物体击中你会失去一颗心,一旦你失去一颗心,玩家会自动重新生成回到他开始的地方。但是我在播放器的移动方面遇到了问题,如前所述,要移动我的播放器,您必须四处点击(点击移动)。当我点击一个地方并且我被一个物体击中时,我的玩家确实会回到它开始的地方(这是我想要的)但是在它重置回到开始的地方之后,我的玩家会继续移动直到它到达目的地(这不是我想要的)

这是我的玩家移动脚本:

public class PlayerMovement : MonoBehaviour {

private Animator anim;
public float speed = 15f;
private Vector3 target;
public PlayerMovement playerMovementRef;
private bool touched;

void Start () {
    target = transform.position;
    anim = GetComponent<Animator> ();
}


void Update () {
    if (Input.GetMouseButtonDown (0)) {
        Vector3 mousePosition = Input.mousePosition;
        mousePosition.z = 10; // distance from the camera
        target = Camera.main.ScreenToWorldPoint(mousePosition);
        target.z = transform.position.z;
    }

    transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);

    var movementDirection = (target - transform.position).normalized;

    if (movementDirection.x != 0 || movementDirection.y != 0) {
        anim.SetBool ("walking", false);
        anim.SetFloat("SpeedX", movementDirection.x);
        anim.SetFloat("SpeedY", movementDirection.y);
        anim.SetBool ("walking", true);
    }
}

void FixedUpdate () {
    float LastInputX = transform.position.x - target.x;
    float LastInputY = transform.position.y - target.y;


    if (touched) {
        if (LastInputX != 0 || LastInputY != 0) {
            anim.SetBool ("walking", true);
            if (LastInputX < 0) {
                anim.SetFloat ("LastMoveX", 1f);
            } else if (LastInputX > 0) {
                anim.SetFloat ("LastMoveX", -1f);
            } else {
                anim.SetFloat ("LastMoveX", 0f);
            }
            if (LastInputY > 0) {
                anim.SetFloat ("LastMoveY", 1f);
            } else if (LastInputY < 0) {
                anim.SetFloat ("LastMoveY", -1f);
            } else {
                anim.SetFloat ("LastMoveY", 0f);
            }
        }
    }else{
        touched = false;
        anim.SetBool ("walking", false);            
        }
    }
}

这是我的玩家的健康脚本(这个脚本在我的玩家被物体击中后将他重生到它开始的地方):

public class PlayerHealth : MonoBehaviour {

//Stats
public int curHealth;
public int maxHealth = 3;
Vector3 startPosition;


void Start ()
{
    curHealth = maxHealth;
    startPosition = transform.position;
}


void Update ()
{

    if (curHealth > maxHealth) {
        curHealth = maxHealth;
    }
    if (curHealth <= 0) {

        Die ();
    }
}

void Die ()
{
    //Restart
    Application.LoadLevel (Application.loadedLevel);


}


public void Damage(int dmg)
{
    curHealth -= dmg;
    Reset();
}


void Reset()
{
    transform.position = startPosition;
    }
}

【问题讨论】:

  • 我看到您已经得到了一个可行的答案,但是将来当您必须扩展播放器的实现时,它可能会产生更多问题。如果您不是编程初学者,我有一个更好的解决方案,需要一点的编程。你想让我发布答案吗?
  • 嗯,好的,谢谢 :)

标签: unity3d


【解决方案1】:

所以当你第一次点击玩家走向的位置时, Input.GetMouseButtonDown (0) 为真,因此在 if 语句中,您使用以下行设置 mousePosition:

Vector3 mousePosition = Input.mousePosition;

然后你将目标设置为等于

target = Camera.main.ScreenToWorldPoint(mousePosition);

问题是在 if 语句之外,但在更新方法中,你有

transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);

玩家的位置不断变化以向目标移动。这是导致问题的原因,因为即使在您重生之后,也会不断调用 update(),并且如果您没有为目标分配不同的值,您的玩家仍将朝着目标移动,该目标在玩家死亡之前设置。

简单地说,当你点击一个要移动的地方时,目标只会设置一次,但你的角色总是会朝着目标移动,即使在他重生之后也是如此。在Start() 方法内部,写类似PlayerMovement.target = startPosition

请记住,目标要么必须是公开的,要么必须具有公开的 getter 和 setter 才能从您的健康类中访问它。

【讨论】:

  • 感谢您的回复,我尝试了您所说的应该将 target 设置为等于 startPostion 的位置,但它仍然在做同样的事情。另外,我不知道您所说的重置 target.z 或 transform.postion 是什么意思。谢谢
  • @JesscaStone,查看我编辑的帖子以获得更详细的答案
  • @Veerbal Kint,我查看并尝试了你所说的,我明白你的意思,但是我现在得到了关于我的 playerhealth 脚本的错误。我确保我的 playermovement 中的“目标”设置为公开,但我仍然出错。感谢您的回复,如果您能继续提供帮助,那就太棒了。谢谢
  • 我的错误:需要对象引用才能访问非静态成员`PlayerMovement.target'
  • 嘿@JesscaStone,抱歉回复晚了。 static 关键字意味着您不必实例化新对象。无需过多涉足面向对象编程,只需公开目标即可: public static Vector3 target;而不是私有 Vector3 目标;
猜你喜欢
  • 2017-11-08
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
相关资源
最近更新 更多