简体   繁体   English

当 NPC 向左或向右走时,如何使 animation 匹配?

[英]How can I make the animation match when the NPC walks left or right?

I have some NPCs that randomly walk within a range, however when I can't determine when they are walking left or right so the animation looks wrong.我有一些 NPC 在一个范围内随机行走,但是当我无法确定他们何时向左或向右行走时,animation 看起来是错误的。

I have a function called "Decrease" that warns inside the terminal when the object walks decreases or increases its position, but this function does not work as it should I have a function called "Decrease" that warns inside the terminal when the object walks decreases or increases its position, but this function does not work as it should

So...所以...

How can I make the animation match when the NPC walks left or right?当 NPC 向左或向右走时,如何使 animation 匹配?

Here is my NPC configuration:这是我的NPC配置:

在此处输入图像描述

Here is my code:这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NPCController : MonoBehaviour
{
    private float waitTime;
    private Animator _animator;
    private SpriteRenderer _renderer;

    public Transform moveSpot;
    private bool _facingRight = true;
    public float lastXVal;
    public float speed;
    public float startWaitTime;
    public float minX;
    public float maxX;
    public float minY;
    public float maxY;

    void Awake()
    {
        _animator = GetComponent<Animator>();
        _renderer = GetComponent<SpriteRenderer>();
    }

    void Start()
    {
        waitTime = startWaitTime;
        moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
    }

    void Update()
    {
        transform.position = Vector2.MoveTowards(transform.position, moveSpot.position, speed * Time.deltaTime);

        if (Vector2.Distance(transform.position, moveSpot.position) < 0.2f)
        {
            //Change animation state
            if (waitTime <= 0)
            {
                _animator.SetBool("Walk", true);
                moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
                waitTime = startWaitTime;
            }
            else
            {
                _animator.SetBool("Walk", false);
                _animator.SetBool("Idle", true);
                waitTime -= Time.deltaTime;
            }
        }
    }

    public void Decrease()
    {
        if (transform.hasChanged)
        {
            if (transform.position.x < lastXVal)
            {
                //Update lastXVal
                lastXVal = transform.position.x;
                Debug.Log("Decreased!");
            }

            else if (transform.position.x > lastXVal)
            {
                //Update lastXVal
                lastXVal = transform.position.x;
                Debug.Log("Increased");
            }

            transform.hasChanged = false;
        }
    }

}

You can use dot product to calculate the direction of travel.您可以使用点积来计算行进方向。

Vector2 moveDirection = (moveSpot.position - transform.position).normalized;
float dotValue = Vector2.Dot(Vector2.right, moveDirection)

// if dotValue is 1 then you are moving right and if dotValue is -1 you are moving left

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当NPC对准动画时,才能启用拍摄功能? - How can I enable shooting only when the npc is in aiming animation? 有没有人知道如何让 animation 在带有标签玩家的玩家走进触发器时发生一次 - does anybody know how to make an animation happen once when a player with tag player walks into a trigger 如何使用 Vector2/3.MoveTowards 使 NPC 仅垂直和水平移动而不是对角线移动,如何使 NPC 仅通过平铺移动 - How can I make the NPC move only vertical and horizontal and not diagonal with Vector2/3.MoveTowards and how can I make the NPC move only by tile 如何在我的网络表单中从右到左导航栏? - How Can I Make Right To Left Navbar In My Webform? 当我的角色走进它时,如何让我的球像球一样滚动? - How do I make my ball roll like a ball when my character walks into it? 当我按下左键或右键时,如何让播放器不旋转? - how to make the player not rotate when i press left or right button? 在更改动画制作者过渡时,如何使NPC角色面向播放器缓慢旋转? - How can I make the npc character to rotate slowly facing the player while changing animator transitions? 我怎样才能让每个 npc 在延迟 3 秒后开始沿着航路点移动? - How can I make that each npc will start moving along the waypoints after 3 seconds of delay? 如何让 npc 用头部或使用 transform.lookat 来查看代理? - How can I make the npc look at the agent with the head or using the transform.lookat? 如何在航点之间移动每个 npc? - How can I move each npc between the waypoints?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM