void Awake() { Movement = GetComponent <Movement>(); navAgent = GetComponent <NavMeshAgent>(); navAgent.updatePosition = false; navAgent.updateRotation = false; navState = NAVSTATE.NONE; }
void Update() { if ((navAgent.destination - transform.position).magnitude == 0) { navState = NAVSTATE.NONE; } if (navState == NAVSTATE.NAVIGATE) { executeNavigation(); } }
// ============================================================================== // Reset // ============================================================================== private void Reset() { RotationReset(); rotation = rotationOrg; // restore these values for a reset orbitCamDistance = orbitCamDistanceOrg; panVector = Vector3.zero; navState = NAVSTATE.IDLE; lastNavState = NAVSTATE.IDLE; lastTouchCount = 0; lastClickTime = -10e37f; inputState = INPUTSTATE.RELEASED; }
private void executeNavigation() { Vector2 inputVec = new Vector2((new Vector3(navAgent.desiredVelocity.x, 0, navAgent.desiredVelocity.z)).normalized.x, (new Vector3(navAgent.desiredVelocity.x, 0, navAgent.desiredVelocity.z)).normalized.z); float speedCap = 1f; if ((navAgent.destination - transform.position).magnitude < breakRadius + navAccRadius) { speedCap = ((navAgent.destination - transform.position).magnitude + navAccRadius) / (breakRadius + navAccRadius); } Movement.move(speedCap * inputVec, false); if ((navAgent.destination - transform.position).magnitude < navAccRadius) { navAgent.destination = transform.position; navState = NAVSTATE.REACHEDGOAL; } Movement.rotate(inputVec, false); navAgent.nextPosition = transform.position; }
// ============================================================================== // HandleInput // ============================================================================== private void HandleInput() { // --- // --- Get the current state of the input device ---------------------- // --- inputState = INPUTSTATE.RELEASED; if (inputPressedLast == true) { inputState = INPUTSTATE.KEEPPRESSING; } if (inputPressed == true && inputPressedLast == false) { inputPressedLast = true; inputState = INPUTSTATE.JUSTPRESSED; } // --- device button Released ---- if (inputPressed == false && inputPressedLast == true) { inputPressedLast = false; inputState = INPUTSTATE.JUSTRELEASED; } navState = NAVSTATE.IDLE; // --- handle multitouch device ---- if (isMultitouchDevice == true) { if (steeringMode == STEERINGMODE.TWO_FINGERS_ZOOM_THREE_FINGERS_PAN) { if (touchCount <= 1) { navState = NAVSTATE.ROTATE; } else if (touchCount == 2) { navState = NAVSTATE.ZOOM; } else if (touchCount > 2) { navState = NAVSTATE.PAN; } } else { if (touchCount <= 1) { navState = NAVSTATE.ROTATE; } else if (touchCount >= 2) { navState = NAVSTATE.ZOOM_AND_PAN; } } } // --- handle singletouch device ---- else { if (touchCount <= 1) { if (Input.GetMouseButton(1) == false) // right mouse button clicked? { navState = NAVSTATE.ROTATE; } else { navState = NAVSTATE.PAN; } } if (mouseWheelValue != 0.0f) { navState = NAVSTATE.ZOOM; } } // --- handle the device state ---------------------------------------- // --- // --- ROTATE --- // --- if (navState == NAVSTATE.ROTATE) { if (inputState == INPUTSTATE.JUSTPRESSED) { SetRotationStart(mousePosition.x, mousePosition.y); } else if (inputState == INPUTSTATE.KEEPPRESSING) { SetRotationCurrent(mousePosition.x, mousePosition.y); } else if (inputState == INPUTSTATE.JUSTRELEASED) { RotationEnded(); } } // --- // --- ZOOM --- // --- else if (navState == NAVSTATE.ZOOM) { RotationStop(); if (isMultitouchDevice == true) { if (inputState == INPUTSTATE.JUSTPRESSED || lastNavState != navState) { zoomStartorbitCamDistance = orbitCamDistance; zoomStartTouchDistance = (touchPositions[1] - touchPositions[0]).magnitude; // distance between the 2 touch points } else if (inputState == INPUTSTATE.KEEPPRESSING) { float zoomEndTouchDistance = (touchPositions[1] - touchPositions[0]).magnitude; // distance between the 2 touch points float scale = (float)zoomStartTouchDistance / (float)zoomEndTouchDistance; // scale factor orbitCamDistance = zoomStartorbitCamDistance * scale; } } else { orbitCamDistance -= mouseWheelValue * mouseZoomSensitivity; } orbitCamDistance = Mathf.Clamp(orbitCamDistance, orbitCamDistanceMin, orbitCamDistanceMax); } // --- // --- PAN --- // --- else if (navState == NAVSTATE.PAN) { RotationStop(); if (inputState == INPUTSTATE.JUSTPRESSED || lastNavState != navState) { if (isMultitouchDevice == true) { panStartTouch = GetCenterOfTouches(); } else { panStartTouch = mousePosition; } } else if (inputState == INPUTSTATE.KEEPPRESSING) { if (isMultitouchDevice == true) { panEndTouch = GetCenterOfTouches(); } else { panEndTouch = mousePosition; } panDiffTouch = panEndTouch - panStartTouch; panStartTouch = panEndTouch; panVector += orbitCamera.transform.right * (-panDiffTouch.x * panningSensitivity); panVector += orbitCamera.transform.up * (panDiffTouch.y * panningSensitivity); } } // --- // --- ZOOM & PAN (multitouch only!) --- // --- else if (navState == NAVSTATE.ZOOM_AND_PAN) { RotationStop(); if (inputState == INPUTSTATE.JUSTPRESSED || lastNavState != navState) { zoomStartorbitCamDistance = orbitCamDistance; zoomStartTouchDistance = (touchPositions[1] - touchPositions[0]).magnitude; // distance between the 2 touch points panStartTouch = GetCenterOfTouches(); } else if (inputState == INPUTSTATE.KEEPPRESSING) { float zoomEndTouchDistance = (touchPositions[1] - touchPositions[0]).magnitude; // distance between the 2 touch points float scale = (float)zoomStartTouchDistance / (float)zoomEndTouchDistance; // scale factor orbitCamDistance = zoomStartorbitCamDistance * scale; panEndTouch = GetCenterOfTouches(); panDiffTouch = panEndTouch - panStartTouch; panStartTouch = panEndTouch; panVector += orbitCamera.transform.right * (-panDiffTouch.x * panningSensitivity); panVector += orbitCamera.transform.up * (panDiffTouch.y * panningSensitivity); } orbitCamDistance = Mathf.Clamp(orbitCamDistance, orbitCamDistanceMin, orbitCamDistanceMax); } // --- // --- DOUBLE CLICK --- // --- // register double clicks if (lastTouchCount == 1 && inputState == INPUTSTATE.JUSTRELEASED) { if (systemTime <= lastClickTime + doubleClickDiffTime && // double click within defined time? (mousePosition - lastDoublClickClickPos).magnitude < (Screen.height / 20.0f)) // and the click distance is within a reasonable distance { Reset(); return; // no further processing } lastClickTime = systemTime; lastDoublClickClickPos = mousePosition; } // remember this as lastState lastNavState = navState; lastTouchCount = touchCount; }
public void navigateTo(Vector3 destination) { navState = NAVSTATE.NAVIGATE; navAgent.destination = destination; }