| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System.Collections;
- using System.Collections.Generic;
- using CodeMonkey.Utils;
- using UnityEngine;
- public class TestingPathfinding : MonoBehaviour {
- [SerializeField] PathfindingVisual pathfindingVisual;
- [SerializeField] PathfindingDebugStepVisual pathfindingDebugStepVisual;
- [SerializeField] LayerMask mousePlaneLayerMask;
- [SerializeField] Transform trackingBall;
- private Pathfinding pathfinding;
- private void Start() {
- pathfinding = new Pathfinding(10, 10);
- pathfindingVisual.SetGrid(pathfinding.GetGrid());
- }
- private void Update() {
- Vector3 mouseWorldPosition = GetMousePosition3d();
- trackingBall.position = mouseWorldPosition;
- if (Input.GetMouseButtonDown(0)) {
- // Vector3 moustWorldPosition = UtilsClass.GetMouseWorldPosition(); // funkar i 2d, inte i 3d
- pathfinding.GetGrid().GetXZ(mouseWorldPosition, out int x, out int z);
- List<PathNode> path = pathfinding.FindPath(0, 0, x, z);
- if (path != null) {
- for (int i = 0; i < path.Count - 1; i++) {
- Debug.DrawLine(new Vector3(path[i].x, 0, path[i].z) * 10f + new Vector3(1, 0, 1) * 5f, new Vector3(path[i + 1].x, 0, path[i + 1].z) * 10f + new Vector3(1, 0, 1) * 5f, Color.green, 10f);
- }
- }
- }
- if (Input.GetMouseButtonDown(1)) {
- // Vector3 mouseWorldPosition = GetMousePosition3d();
- pathfinding.GetGrid().GetXZ(mouseWorldPosition, out int x, out int z);
- pathfinding.GetNode(x, z).SetIsWalkable(!pathfinding.GetNode(x, z).isWalkable);
- }
- }
- private Vector3 GetMousePosition3d() {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- Physics.Raycast(ray, out RaycastHit raycastHit, float.MaxValue, mousePlaneLayerMask);
- return raycastHit.point;
- }
- }
|