private void Update() { if (m_Camera == null) { return; } Vector3 mousePosition = Input.mousePosition; Ray ray = m_Camera.ScreenPointToRay(mousePosition); if (Physics.Raycast(ray, out RaycastHit hit)) { if (hit.transform != transform) { return; } m_Cursor.SetActive(true); Vector3 hitPosition = hit.point; Vector3 difference = hitPosition - m_Offset; int x = (int)(difference.x / m_NodeSize); int y = (int)(difference.z / m_NodeSize); Vector3 targetPosition = m_Offset + new Vector3((x + 0.5f) * m_NodeSize, hitPosition.y, (y + 0.5f) * m_NodeSize); if (Input.GetMouseButtonDown(1)) { m_MovementAgent.SetTarget(targetPosition); } m_Cursor.transform.position = targetPosition; } else { m_Cursor.SetActive(false); } }
private void Update() { // We ask GridHolder info about coordinates Vector3?mousePosition = m_GridHolder.GetMousePosition(); Vector3?nodePosition = m_GridHolder.GetNodePosition(); if (Input.GetMouseButton(0)) { // Try move the object to another point if (nodePosition.HasValue) { m_MovementAgent.SetTarget(nodePosition.Value); } } // Draw cursor if (mousePosition.HasValue && nodePosition.HasValue) { // If close to node centre then pin to centre Vector3 newPos = ((mousePosition.Value - nodePosition.Value).sqrMagnitude < m_TieThreshold) ? nodePosition.Value : mousePosition.Value; m_Cursor.SetActive(true); m_Cursor.transform.position = newPos; } else { m_Cursor.SetActive(false); } }
private void Update() { Vector3 mousePosition = Input.mousePosition; Ray ray = m_Camera.ScreenPointToRay(mousePosition); if (Physics.Raycast(ray, out RaycastHit hit)) // Raycast camera -> cursor { // Did not hit the plane -> do nothing // Not really okay to simply return imho, but fine so far if (hit.transform != transform) { return; } Vector3 hitPosition = hit.point; Vector3 difference = hitPosition - m_Offset; int x = (int)(difference.x / m_NodeSize); int y = (int)(difference.z / m_NodeSize); Vector3 hitNodeCenter = m_Offset + new Vector3( m_NodeSize * x + m_NodeSize * 0.5f, 0f, m_NodeSize * y + m_NodeSize * 0.5f); // Set a new target for the object if the lmb was pressed if (Input.GetMouseButton(0) && m_MovementAgent != null) { m_MovementAgent.SetTarget(hitNodeCenter); } if (m_Cursor != null) // Move the cursor { m_Cursor.SetActive(true); m_Cursor.transform.position = hitNodeCenter; //Debug.Log("Cursor enabled and set at " + hitNodeCenter); } // Print the grid coordinates in console Debug.Log(x + " " + y); } else { // We assume that the only time we need to disable cursor // is when it doesn't point to the grid or object if (m_Cursor != null) { //Debug.Log("CURSOR DISABLED"); m_Cursor.SetActive(false); } } }