【发布时间】: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