当翻转角色时,角色武器的旋转向后旋转

问题描述 投票:0回答:1

我在场景中有两个游戏对象,1.Character(Parent Object)和2.Weapon(Child Object)。问题是,当角色向右侧移动时,武器的旋转很好,它正朝着角色朝向并按预期旋转的位置,如下面的Gif图像所示。但是,当我向左翻转时,一切都出了问题,武器向后倾斜;当我按下向下箭头时,旋转上升;当按下向上箭头时,旋转下降,请参阅下面的Gif图片。

这是我的代码:

 public float weaponRotationSpeed = 13f;

 private Animator anim;
 private float angle;

 void Awake()
 {
     anim = GetComponent<Animator>();
 }

 void Update()
 {
     Vector2 hv = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
     Vector3 changeParentScale = transform.localScale;

     if (hv != Vector2.zero)
     {
         if (Input.GetAxis("Horizontal") < 0)
         {
             changeParentScale.x = -5f;
             transform.localScale = changeParentScale;
         }
         else if (Input.GetAxis("Horizontal") > 0)
         {
             changeParentScale.x = 5f;
             transform.localScale = changeParentScale;
         }


         angle = Mathf.Atan2(hv.y, hv.x) * Mathf.Rad2Deg;

         transform.Find("Weapon").rotation = Quaternion.Lerp(transform.Find("Weapon").rotation,
             Quaternion.Euler(0, 0, angle),
             weaponRotationSpeed * Time.deltaTime);

         anim.SetBool("isRunning", true);
     }
     else
     {
         anim.SetBool("isRunning", false);
     }

>

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9hbnN3ZXJzLnVuaXR5LmNvbS9zdG9yYWdlL3RlbXAvMTYxOTk3LWxlZnRmbGlwLmdpZiJ9”>

我在场景中有两个游戏对象,1.Character(Parent Object)和2.Weapon(Child Object)。问题是当角色向右移动时,武器的旋转很好,这是...

c# unity3d 2d game-development
1个回答
0
投票

那么您可能还想翻转旋转角度吗?例如。使用Mathf.Sign

Mathf.Sign
© www.soinside.com 2019 - 2024. All rights reserved.