【问题标题】:Get Unity Blend Tree to update based on player rotation (mouse position)获取 Unity Blend Tree 以根据玩家旋转(鼠标位置)进行更新
【发布时间】:2019-07-24 02:08:50
【问题描述】:


我正在为自上而下的游戏中的角色制作动画。当玩家向前移动到顶部时,我让动画工作,但是,每当我用鼠标旋转玩家(瞄准)时,向前动画现在是左/右等等。我使用 Unity3d 和 C#与混合树动画。

我使用的代码很简单:

animator.SetFloat("VelX", playerInput.Horizontal);
animator.SetFloat("VelY", playerInput.Vertical);

当我向鼠标位置移动时,我不知道如何播放前向动画。

我的解决方案:

var heading = Input.mousePosition - this.transform.position;
var distance = heading.magnitude;
var dir = heading / distance;
animator.SetFloat("VelX", playerInput.Horizontal * Mathf.Cos(dir));
animator.SetFloat("VelY", playerInput.Vertical * Mathf.Sin(dir));

任何帮助将不胜感激。

【问题讨论】:

    标签: c# unity3d animation trigonometry game-development


    【解决方案1】:

    您可能已经想到了这一点,但我也遇到了同样的问题,我刚刚找到了一种方法来做到这一点。希望它可以帮助其他人。

     Vector3 inputVector = (Vector3.forward * Input.GetAxis("Vertical")) + (Vector3.right * Input.GetAxis("Horizontal"));
    
     Vector3 animationVector = _playerModel.transform.InverseTransformDirection(inputVector);
    
     var VelocityX = animationVector.x;
     var VelocityZ = animationVector.z;
     
     this._playerAnimator.SetFloat("x", VelocityX);
     this._playerAnimator.SetFloat("z", VelocityZ);
    

    在我的情况下,我的移动脚本位于父空对象上。这个对象旋转子玩家模型,这是一个动画。如果您这样做,那么您将需要像我一样引用播放器模型。否则,如果您使用父对象,则可以进行转换。

    我在我的父对象而不是刚体上使用角色控制器,但它可能以任何方式工作。

    据我了解,关键部分是“InverseTransformDirection”,它采用我的物理世界水平和垂直向量,然后根据我的玩家模型的旋转对其进行转换。然后,这使得移动与玩家旋转而不是世界旋转相关,并且当我移动鼠标进行旋转和移动时,我可以正确地向后移动或扫射。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多