根据角色周围的旋转动画 [英] Animation according to the rotation around the character

查看:149
本文介绍了根据角色周围的旋转动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I currently have a character who can move in different directions, for each direction a different animation is launched. The movement is currently done using the keys.

我希望动画的含义也根据鼠标围绕角色的角度而改变.

I would like that depending on the angle of the mouse around the character the meaning of the animation also changes.

我需要根据鼠标的角度,开始方向动画.如下面的示例.

I need that depending on the angle of the mouse, the animation of the direction starts. As the example below.

[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);
}

如果有任何领导或榜样,我先谢谢您.

If anyone has a lead or an example, I thank you in advance.

推荐答案

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

So what I basically did below is calculate angle between cursor and player then set the animations accordingly. if you have questions or need clarification just reply but I feel like the code might work even if you simply put it in your Update function. Input.mousePosition may not readily work for 2D in that case you can just switch it up with Camera.main.ScreenToWorldPoint(Input.mousePosition). Good luck!

//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);

这篇关于根据角色周围的旋转动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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