gpt4 book ai didi

c# - Unity3D:回到空闲状态动画

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:28 24 4
gpt4 key购买 nike

我在返回游戏中的空闲状态动画时遇到问题。这video是我的问题的一个例子。我的播放器没有返回到空闲状态动画,我确实尝试将其添加到我的代码中:

anim.SetBool ("walking", false); 

(这个放在我的 code 的末尾附近)

但是这个happens .这不是我想要的。在first我给你看的视频,它显示我点击了 walking parameter off这样我就可以向您展示如果我的播放器到达目的地后停下来我的动画会是什么样子。在视频中您可以看到我的播放器在上下走动后面向错误的方向,它面对这个 wayway ,我也想修复,但不知道如何修复。那么任何人都可以帮助我解决我的两个问题,即:

  1. 在我的播放器到达目的地后返回闲置动画。
  2. 并确保我的播放器朝向正确的方向。我的玩家的 Sprite 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);
}
}

最佳答案

检测玩家是否在当前目的地。您可以从 target 获取当前目的地。如果它在当前目的地,则将“行走”设为假。面对正确的方向应该可以正常工作。

将此 block 放入 Update():

if(transform.position == target)
{
anim.SetBool ("walking", false);
}

关于c# - Unity3D:回到空闲状态动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38412266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com