I don't want to find the shortest way and not moving in diagonals. The start position and end position are both on the edges walls so in the start i can't move back and in the end i can't move forward (depending on what direction the player is facing when getting to the end position ).
I want to move only left,right,forward but randomly. Randomly i mean that in the start i have only 3 directions possible moves: Forward,Left,Right so i want to store the 3 directions and then to pick one random direction for example Left to rotate the player facing to the left and then moving to the waypoint on the left the nearest waypoint on the left.
Now if he moved left now he have two directions to move right or keep forward and again to pick random direction from this two.
I want that the player will never go back to the last waypoint he visited.
So in any case the player will have 2 or 3 directions to move and each time to pick random direction to move to.
The red is the start position the blue is the end position. So i want to create a random path from the red to the blue.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.IO;
public class PathFinder : MonoBehaviour
{
private Transform start;
private Transform end;
public void FindPath()
{
GenerateStartEnd();
GenerateRandomPath();
}
private void GenerateRandomPath()
{
GridGenerator gridgenerator = GetComponent<GridGenerator>();
List<float> distances = new List<float>();
List<float> rotations = new List<float>();
for (int i = 0; i < gridgenerator.allBlocks.Length; i++)
{
var distance = Vector3.Distance(start.localPosition, gridgenerator.allBlocks[i].transform.localPosition);
if (distance == 1.5f)
{
var angle = Vector3.Angle(start.localPosition, gridgenerator.allBlocks[i].transform.localPosition);
rotations.Add(angle);
}
}
distances.Sort();
}
void SmoothLook(Vector3 newDirection)
{
start.rotation = Quaternion.Lerp(start.rotation, Quaternion.LookRotation(newDirection), Time.deltaTime);
}
private List<Vector3> GenerateStartEnd()
{
GameObject walls = GameObject.Find("Walls");
List<Transform> wallsParents = new List<Transform>();
List<Vector3> startEndPos = new List<Vector3>();
foreach (Transform child in walls.transform)
{
wallsParents.Add(child);
}
for (int i = 0; i < 2; i++)
{
wallsParents.Remove(wallsParents[Random.Range(0, wallsParents.Count)]);
}
var childsWall0 = wallsParents[0].GetComponentsInChildren<Transform>().ToList();
var childsWall1 = wallsParents[1].GetComponentsInChildren<Transform>().ToList();
childsWall0.RemoveAt(0);
childsWall1.RemoveAt(0);
start = childsWall0[Random.Range(0, childsWall0.Count)];
end = childsWall1[Random.Range(0, childsWall1.Count)];
startEndPos.Add(start.position);
startEndPos.Add(end.position);
start.GetComponent<Renderer>().material.color = Color.red;
end.GetComponent<Renderer>().material.color = Color.blue;
return startEndPos;
}
}
In GenerateStartEnd i pick two random walls. Then i'm getting the children of each wall the blocks(cubes). I remove the parents. Then i pick random block from each wall childs. I set the start and end.
Then in GenerateRandomPath this is where i'm trying to generate the random path between start and end. This is the whole part of the random path:
private void GenerateRandomPath()
{
GridGenerator gridgenerator = GetComponent<GridGenerator>();
List<float> distances = new List<float>();
List<float> rotations = new List<float>();
for (int i = 0; i < gridgenerator.allBlocks.Length; i++)
{
var distance = Vector3.Distance(start.localPosition, gridgenerator.allBlocks[i].transform.localPosition);
if (distance == 1.5f)
{
var angle = Vector3.Angle(start.localPosition, gridgenerator.allBlocks[i].transform.localPosition);
rotations.Add(angle);
}
}
distances.Sort();
}
void SmoothLook(Vector3 newDirection)
{
start.rotation = Quaternion.Lerp(start.rotation, Quaternion.LookRotation(newDirection), Time.deltaTime);
}
The logic is simple find what directions you can move pick random direction move one waypoint in that direction don't stop continue to the next random waypoint. The next waypoint can be the nearest or even far away i don't mind but once he is reaching to the next waypoint find the directions pick random one and rotate and move to that direction to another rndom waypoint on this direction.
And son on until the player will get to the end. And i don't mind if the path will be long.
I'm just not sure how to translate it to the script.
allBlocks is GameObject[] array containing all the grid blocks(cubes).