2017-05-03 68 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")剛剛得到的運動,如果它不動,打空閒

相關問題