【问题标题】:How can i make the player idle when not moving the mouse?不移动鼠标时如何让播放器闲置?
【发布时间】:2017-05-04 03:40:41
【问题描述】:

当我在周围移动鼠标时,玩家会面向鼠标光标行走。 现在我想这样做,如果我不移动鼠标让玩家闲置的话。

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

public class WorldInteraction : MonoBehaviour
{
    public int speed = 5; // Determines how quickly object moves towards position
    public float rotationSpeed = 5f;
    private Animator _animator;
    private bool toMove = true;
    private Vector3 tmpMousePosition;

    UnityEngine.AI.NavMeshAgent playerAgent;

    private void Start()
    {
        tmpMousePosition = Input.mousePosition;
        _animator = GetComponent<Animator>();
        _animator.CrossFade("Idle", 0);

        playerAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0) && toMove == false &&
  !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) 
       {
            toMove = true;
        }
        if (Input.GetMouseButtonDown(1) && toMove == true)
        {
            toMove = false;
        }
        if (toMove == true)
        {
            GetInteraction();
        }
    }

    void GetInteraction()
    {

        if (tmpMousePosition != Input.mousePosition)
        {
            tmpMousePosition = Input.mousePosition;
            _animator.CrossFade("Walk", 0);
            Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit interactionInfo;
            if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
            {
                GameObject interactedObject = interactionInfo.collider.gameObject;
                if (interactedObject.tag == "Interactable Object")
                {
                    interactedObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
                }
                else
                {
                    playerAgent.destination = interactionInfo.point;
                }
            }
        }
        else
        {
            _animator.CrossFade("Idle", 0);
        }
    }
}

我正在使用变量 tmpMousePosition 来检查鼠标是否在移动。问题是当它在移动并且玩家处于“步行”模式时,玩家每隔一秒左右就会出现口吃。

这个想法是当鼠标移动时移动玩家,当鼠标不移动时让玩家处于空闲状态。

在更新功能中,我使用 bool 来停止/继续交互,就像使用鼠标左/右按钮进行切换一样。但现在我想使用鼠标移动来让玩家走/闲置。

【问题讨论】:

  • 我不确定我是否理解问题所在;只需比较帧之间的鼠标位置以查看鼠标是否在移动(如果您希望在鼠标停止后有一个冷却时间,则可能必须更复杂),然后仅在位置发生变化时才移动。我错过了什么?
  • 如果鼠标没有移动或点击,当达到 30(或多少秒)然后玩家空闲时,将计时器增加 deltaTime。如果鼠标确实移动了,请将计时器重置为 0。

标签: c# unity3d


【解决方案1】:

只需通过Input.GetAxis("Mouse X") 获取运动,如果它不运动,则玩空闲

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    相关资源
    最近更新 更多