根据围绕角色旋转的动画

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

我目前有一个可以向不同方向移动的角色,对于每个方向,将启动不同的动画。当前移动是通过按键完成的。

我想根据鼠标围绕角色的角度,动画的含义也会改变。

enter image description hereenter image description here

我需要根据鼠标的角度来启动方向的动画。如下面的示例。

enter image description here

[Header("Movement")]
[Tooltip("Walk movement")]
public float speed = 5f;
[Tooltip("Player Rigidbody")]
public Rigidbody2D rigidBody;
public Animator animator;

Vector2 movement;

void FixedUpdate() {

    // Position
    movement.x = Input.GetAxisRaw("Horizontal");
    movement.y = Input.GetAxisRaw("Vertical");

    // Animations
    animator.SetFloat("Horizontal", movement.x);
    animator.SetFloat("Vertical", movement.y);
    animator.SetFloat("Vitesse", movement.sqrMagnitude);

    // Angle isometric
    if (movement.x != 0  && movement.y != 0)
    {
        movement.y = movement.y / 2;
    }

    Vector2 inputVector = new Vector2(movement.x, movement.y);
    inputVector = Vector2.ClampMagnitude(inputVector, 1);
    Vector2 movement = inputVector * speed;

    // Movement
    rigidBody.MovePosition(rigidBody.position + movement * Time.fixedDeltaTime);
}

如果有领导或榜样,我先谢谢你。

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

所以我基本上在下面做的是计算光标和播放器之间的角度,然后相应地设置动画。如果您有疑问或需要澄清,只需答复即可,但是即使将代码简单地放在Update函数中,我也认为代码可能会起作用。在这种情况下,Input.mousePosition可能不适用于2D,只需用Camera.main.ScreenToWorldPoint(Input.mousePosition)进行切换即可。祝你好运!

//direction towards cursor
Vector2 towardCursor = Input.mousePosition - transform.position;


//angle between worldspace right and direction towards cursor
//signedAngle to detect negative angles
float angleVertical = Vector2.SignedAngle(Transform.right, towardCursor);

//angle between worldspace up and direction towards cursor
float angleHorizontal = Vector2.SignedAngle(Transform.up, towardCursor);

//reversed to make clockwise angles negative angles        
angleVertical = -angleVertical;
angleHorizontal = -angleHorizontal;

if (angleVertical > 90
||
angleVertical < -90)
{

angleVertical = angleVertical > 0 ? 180 - angleVertical : -180 - angleVertical;

}

if (angleHorizontal > 90
||
angleHorizontal < -90)
{

angleHorizontal = angleHorizontal > 0 ? 180 - angleHorizontal : -180 - angleHorizontal;

}

animator.setFloat("Vertical", angleVertical / 90);
animator.setFloat("Horizontal", angleHorizontal / 90);
© www.soinside.com 2019 - 2024. All rights reserved.