好吧,我差不多完成了我的2D RPG点击移动游戏。如果你看看这个video,你将会看到,当我点击前进我的球员时,一旦它到达了位置,就会面对错误的方向,而不是直接面对球员。为了让自己更清晰,这是我目前使用的精灵表的image,因为你可以看到它有8个方向。当您点击here(在游戏中)时,我的玩家将走下来并面对此direction或此direction,而不是面对此direction(正常/首选位置)。当我点击here(在游戏中)时,也会发生这种情况,我的玩家会走上并面对此direction或面对此direction,而不是面对此direction。我如何确保我的玩家一旦到达目的地就会面对正确的方向。再次,这只发生在Y轴内,所以沿着X轴行走很好。Unity3D:我如何提高我的空闲动画(Y轴)
private Animator anim;
public float speed = 15f;
private Vector3 target;
private bool touched;
private bool playerMovementRef;
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;
var movementDirection = (target - transform.position).normalized;
Vector3 animDirection = Vector3.zero;
if (movementDirection.sqrMagnitude > 0)
{
// Use >= to default to horizontal on both being equal
if (movementDirection.x > movementDirection.y)
animDirection.x = 1;
else
animDirection.y = 1;
anim.SetBool ("walking", true);
anim.SetFloat ("SpeedX", movementDirection.x);
anim.SetFloat ("SpeedY", movementDirection.y);
if (movementDirection.x < 0) {
anim.SetFloat ("LastMoveX", -1f);
} else if (movementDirection.x > 0) {
anim.SetFloat ("LastMoveX", 1f);
} else {
anim.SetFloat ("LastMoveX", 0f);
}
if (movementDirection.y > 0) {
anim.SetFloat ("LastMoveY", 1f);
} else if (movementDirection.y < 0) {
anim.SetFloat ("LastMoveY", -1f);
} else {
anim.SetFloat ("LastMoveY", 0f);
}
}
} else {
if (Mathf.Approximately (transform.position.x, target.x) && Mathf.Approximately (transform.position.y, target.y)) {
touched = false;
anim.SetBool ("walking", false);
} else {
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
}
}
}
}
一个很好的答案 –
谢谢你的回复,你能否给我一个关于如何执行我的debug.log的想法。再次感谢您的答复。 –
到目前为止,我已经拿出了这个:\t \t \t \t \t print(“movementDirection.x is:”+ movementDirection.x +“movementDirection.y is:”+ movementDirection.y); –