【发布时间】:2016-07-16 14:40:17
【问题描述】:
我在游戏中返回空闲状态动画时遇到问题。这个video 是我的问题的一个例子。我的播放器不会回到空闲状态动画,我确实尝试将其添加到我的代码中:
anim.SetBool ("walking", false);
(这放在我的code 的末尾附近)
但是这个happens。这不是我想要的。在我向您展示的first 视频中,它显示我单击了walking parameter off,这样我就可以向您展示如果我的播放器在到达目的地后停止时我的动画会是什么样子。在视频中您可以看到我的播放器面对走下来走错了路,它面对这个way和那个way,我也想修复,但不知道如何修复。那么任何人都可以帮助我解决我的两个问题,即:
- 一旦我的播放器到达目的地,就会返回空闲动画。
- 并确保我的播放器朝向正确的方向。我的玩家精灵image
这是我的代码:
二次编辑
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(transform.position == target)
{
anim.SetBool ("walking", false);
}
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;
// 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);
Debug.LogFormat ("X: {0}, Y: {1}", movementDirection.x, movementDirection.y);
if (movementDirection.x > 0)
{
anim.SetFloat ("LastMoveX", 1f);
}
else if (movementDirection.x < 0)
{
anim.SetFloat ("LastMoveX", -1f);
}
else
{
if (movementDirection.y > 0)
{
anim.SetFloat ("LastMoveY", 1f);
}
else if (movementDirection.y < 0)
{
anim.SetFloat ("LastMoveY", -1f);
}
else
{
anim.SetFloat ("LastMoveY", 0f);
}
}
}
else
{
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
}
}
【问题讨论】: