2017-05-03 86 views
0

当我在玩家周围移动鼠标时,正朝着鼠标光标行走。 现在我想说如果我不移动鼠标使播放器闲置。如何在不移动鼠标的情况下让播放器闲置?

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来检查鼠标是否在移动。问题在于当它处于移动状态并且玩家处于“走路”模式时,玩家每秒钟都会口吃。

这个想法是当鼠标移动时,然后移动播放器,当鼠标不移动使播放器处于空闲状态。

在Update函数中,我使用bool来停止/继续交互,就像使用鼠标左/右按钮的开关一样。但是现在我想用鼠标移动来散步/空闲播放器。

+0

我不知道我理解的问题是什么;只需比较帧之间的鼠标位置以查看鼠标是否正在移动(如果您希望鼠标停止时有一个冷却期,则可能必须更加复杂),然后仅在位置发生变化时才移动。我错过了什么? –

+0

如果鼠标没有移动或点击,当达到30(或者很多秒)*,然后*玩家空闲时,通过'deltaTime'增加一个计时器。如果鼠标DID移动,将计时器重置为0。 – Draco18s

回答

1

通过Input.GetAxis("Mouse X")刚刚得到的运动,如果它不动,打空闲

相关问题