Question by
Foldaren · Jul 17, 2020 at 08:56 PM ·
unity 2dlinerandomizationrandom gen
Trying to draw a line between two randomly generated point
Hello, I'm trying to draw vertical or horizontal lines between point a and point b, and repeat the process until they have reached a final destination. Line length is random. So far, I believe I have managed to make the logic, but I'm hitting a wall now that it comes to actually generating the lines. Note that there aren't any objects at every intersection, the only objects that are present is the "Goal" which is generated inside the screen and the "spawnpoints" which are generated on the screen borders. Here is my code logic. I've tried using linerenderer, but it doesn't seem to be working.
using UnityEngine;
public class Pathing : MonoBehaviour
{
Vector3 newposition, oldposition, goalposition;
bool changey;
float targetdistance, newx, newy;
GameObject goal;
// Start is called before the first frame update
void Start()
{
///Initiating variables
goal = GameObject.Find("Goal(Clone)");//find the object in scene
goalposition = goal.transform.position;//target to get to
newposition = transform.position;//initial start point
///Determines Directions in X and Y
if(transform.position.x == -8.4f || transform.position.x == 8.4f)//spawner is horizontal aligned and moves horizontally first
{
changey = false;
}
if(transform.position.y == -4.5 || transform.position.y == 4.5)//spawner is vertical aligned and moves vertically first
{
changey = true;
}
while (targetdistance != 0)//as long as distance between the new point to draw the line to and the goal is not 0, continue
{
///Determines if line directions will be positive or negative
if(changey == false)//in case of x move turn
{
if (transform.position.x > goalposition.x)
{
newx = Random.Range(goalposition.x, oldposition.x - 2);//generate x between position x of goal and old generated y position -2, for minimum movement
targetdistance = oldposition.x - goalposition.x;
}
if (transform.position.x < goalposition.x)
{
newx = Random.Range(oldposition.x + 2, goalposition.x);//generate x between old generated x position +2 and position x of goal, for minimum movement
targetdistance = goalposition.x - oldposition.x;
}
}
if(changey == true)//in case of y move turn
{
if (transform.position.y > goalposition.y)
{
newy = Random.Range(goalposition.y, oldposition.y - 2);//generate y between position y of goal and old generated y position -2, for minimum movement
targetdistance = oldposition.y - goalposition.y;
}
if (transform.position.y < goalposition.y)
{
newy = Random.Range(oldposition.y + 2, goalposition.y);//generate y between old generated y position +2 and position y of goal, for minimum movement
targetdistance = goalposition.y - oldposition.y;
}
}
oldposition = newposition;//save the previous position
///Generate the new target position & the line
if (changey == false)//horizontal (x)
{
if (targetdistance < 1f && targetdistance > -1f)//if distance is less than 1
{
newx = goalposition.x;
}
newposition = new Vector3(newx, oldposition.y);
changey = true;//change direction
//Draw the line between oldposition and newposition
}
if (changey == true)//vertical (y)
{
if (targetdistance < 1f && targetdistance > -1f)//if distance is less than 1
{
newy = goalposition.y;
}
newposition = new Vector3(oldposition.x, newy);
changey = false;//change direction
//Draw the line between oldposition and newposition
}
}
}
Comment