如何在不移动鼠标时让播放器空闲?

时间:2017-05-03 16:15:41

标签: c# unity3d

当我移动鼠标时,播放器正面向鼠标光标。 现在我想做的是,如果我没有移动鼠标让玩家闲置。

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来停止/继续交互,就像使用鼠标左/右按钮的开关一样。但现在我想使用鼠标移动来漫步/闲置播放器。

1 个答案:

答案 0 :(得分:1)

只需通过Input.GetAxis("Mouse X")进行移动,如果它没有移动,请播放空闲