如何停止角色停止移动时发生的闪烁? [英] How do I stop the flickering that occurs when the character stops moving?

查看:159
本文介绍了如何停止角色停止移动时发生的闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在youtube上关注了本教程: Unity3D。自上而下的8个方向移动 - YouTube [ ^ ]。我花了一段时间,但我几乎从箭头的移动转换为鼠标触摸。但现在我遇到了这个问题,当角色停止移动时会出现闪烁现象。我查看了他发给我的链接,但它不起作用。请看这个视频,告诉你我在说什么: 我的团结问题 - YouTube [ ^ ]。有谁知道如何阻止角色停止移动时发生的闪烁?这是我的代码:



I followed this tutorial on youtube: Unity3D. Top-down 8 directions movement - YouTube[^]. It took me a while but I've almost converted from movement by arrows to mouse touch . but now I have this problem, there's flickering that occurs when the character stops moving. I looked at the link he sent me but it doesn't work. Please look at this video to show you what I am talking about: My unity problem - YouTube[^]. Does anyone knows how to stop the flickering that occurs when the character stops moving? This is my code:

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


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

 void Update () {
     touched = true;
     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;

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

             Vector2 movement = new Vector2(
                 speed * movementDirection.x ,
                 speed * movementDirection.y);
             movement *= Time.deltaTime;
             transform.Translate(movement);

             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 {
         touched = false;
         anim.SetBool("walking" , false);
     }
 }





我尝试了什么:



我尝试过以下方法:

https://josejimenez.info/blog/unity3d-top-down-8-direction-movement .html



我试过检查如果我添加类似



if(movingDirection)会发生什么.x!= 0 || movementDirection.y!= 0){

anim.SetFloat(SpeedX,movementDirection.x);

anim.SetFloat(SpeedY ,movementDirection.y);

}







如果(movementDirection.x!= 0 || movementDirection.y!= 0){

float rot_z = Mathf.Atan2(movementDirection.y,movementDirection.x)* Mathf.Rad2Deg;

transform.rotation = Quaternion.Euler(0f,0f,rot_z);

}



以及

transform.rotation = Quaternion.Euler(0f,0f,rot_z - 90)



What I have tried:

I have tried the following:
https://josejimenez.info/blog/unity3d-top-down-8-direction-movement.html

I've Tried checking out what happens if I add something like

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

and

if (movementDirection.x != 0 || movementDirection.y != 0) {
float rot_z = Mathf.Atan2 (movementDirection.y, movementDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler (0f, 0f, rot_z);
}

as well as
transform.rotation = Quaternion.Euler (0f, 0f, rot_z - 90)

推荐答案

你的Animat需要一个死区离子跃迁使它们> 0.1f NOT>如果你得到抖动。将焦点放在对象检查器中的角色上并观察动画编辑器的转换。



这是你的代码的补丁,但这不是我要解释的正确修复下一页

You need a dead zone on your Animation transitions make them > 0.1f NOT > 0f you are getting jitter. Hold focus on the character in the object inspector and watch the animator editor transitions.

This is the patch to your code but this is not the proper fix which I will explain next
if (movementDirection.x < 0.1f) {
  anim.SetFloat("LastMoveX" , -1f);
} else if (movementDirection.x > 0.1f) {
  anim.SetFloat("LastMoveX" , 1f);
} else {
  anim.SetFloat("LastMoveX" , 0f);
}
 
if (movementDirection.y < 0.1f) {
  anim.SetFloat("LastMoveY" , -1f);
} else if (movementDirection.y > 0.1f) {
  anim.SetFloat("LastMoveY" , 1f);
} else {
  anim.SetFloat("LastMoveYX" , 0f);
}





您可以将此代码正常运行,只需将其发送给动画师即可控制不受影响




You can do what this code is doing properly just sending it out to the animator control untouched

anim.SetFloat("LastMoveX" , movement.x);
anim.SetFloat("LastMoveY" , movement.y);



现在动画师控件中的转换(状态之间的箭头)变化比较> 0.1f和< -0.1f



没有必要将自己限制为0-1传递给动画师的值是浮点数它可能是消极的。基本上你有代码在动画师做更好更简单的事情。



因此在动画编辑器中,状态之间的箭头改变它们

Idle state transitions
LastMoveX < -0.1f transition to walking left
LastMoveX > 0.1f transition to walking right

Walk left state transition dropback to idle
LastMoveX > -0.1f transition to idle

Walk right state transition dropback to idle
LastMoveX < 0.1f transition to idle



按照你应该的那样在动画师上组织,你的代码变得完全冗余而且不需要。


Organize that on the animator like you are supposed to and your code becomes totally redundant and is not needed.


这篇关于如何停止角色停止移动时发生的闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆