- Home /
Question by
szymonides · May 20, 2019 at 01:04 PM ·
2dscripting beginnerpathfindingastar
Trouble with pathfinding in 2d
I was trying to repurpose the A* script by Sebastian Lague to work in 2D, I got stuck on ep.8 of his tutorial and I can't get the weight lines in the right positions. What am I doing wrong here?
These are all the scripts that were to be edited for that episode:
Line:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct Line {
const float verticalLineGradient = 1e5f;
float gradient;
float y_intercept;
Vector2 pointOnLine_1;
Vector2 pointOnLine_2;
//float gradientPerpendicular;
bool approachSide;
public Line(Vector2 pointOnLine, Vector2 pointPerpendicularToLine) {
float dx = pointOnLine.x - pointPerpendicularToLine.x;
float dy = pointOnLine.y - pointPerpendicularToLine.y;
if (dy == 0) {
gradient = verticalLineGradient;
} else {
gradient = -dx / dy;
}
y_intercept = pointOnLine.y - gradient * pointOnLine.x;
pointOnLine_1 = pointOnLine;
pointOnLine_2 = pointOnLine + new Vector2 (1, gradient);
approachSide = false;
approachSide = GetSide (pointPerpendicularToLine);
}
bool GetSide(Vector2 p) {
return (p.x - pointOnLine_1.x) * (pointOnLine_2.y - pointOnLine_1.y) > (p.y - pointOnLine_1.y) * (pointOnLine_2.x - pointOnLine_1.x);
}
public bool HasCrossedLine(Vector2 p) {
return GetSide (p) != approachSide;
}
public void DrawWithGizmos(float length) {
Vector2 lineDir = new Vector2 (gradient ,1).normalized;
Vector2 lineCentre = new Vector2 (pointOnLine_1.x, pointOnLine_1.y);
Gizmos.DrawLine (lineCentre - lineDir * length / 2f , lineCentre + lineDir * length / 2f);
}
}
Path:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Path {
public readonly Vector2[] lookPoints;
public readonly Line[] turnBoundaries;
public readonly int finishLineIndex;
public Path(Vector2[] waypoints, Vector2 startPos, float turnDst) {
lookPoints = waypoints;
turnBoundaries = new Line[lookPoints.Length];
finishLineIndex = turnBoundaries.Length - 1;
Vector2 previousPoint = startPos;
for (int i = 0; i < lookPoints.Length; i++) {
Vector2 currentPoint = lookPoints [i];
Vector2 dirToCurrentPoint = (currentPoint - previousPoint).normalized;
Vector2 turnBoundaryPoint = (i == finishLineIndex)?currentPoint : currentPoint - dirToCurrentPoint * turnDst;
turnBoundaries [i] = new Line (turnBoundaryPoint, previousPoint - dirToCurrentPoint * turnDst);
previousPoint = turnBoundaryPoint;
}
}
public void DrawWithGizmos() {
Gizmos.color = Color.black;
foreach (Vector3 p in lookPoints) {
Gizmos.DrawCube (p , Vector3.one*0.2f);
}
Gizmos.color = Color.white;
foreach (Line l in turnBoundaries) {
l.DrawWithGizmos (0.3f);
}
}
}
Unit:
using UnityEngine;
using System.Collections;
public class Unit : MonoBehaviour {
public Transform target;
public float speed = 0.5f;
public float turnDst = 5;
Path path;
//Vector2[] path;
//int targetIndex;
MovementController movementController;
void Update()
{
if (Input.GetButtonDown("Fire1") && Vector2.Distance(transform.position, target.position) > 1.75f){
PathRequestManager.RequestPath(transform.position,target.position, OnPathFound);
}
}
void Start() {
PathRequestManager.RequestPath(transform.position,target.position, OnPathFound);
movementController = GetComponent<MovementController>();
}
public void OnPathFound(Vector2[] waypoints, bool pathSuccessful) {//newPath
if (pathSuccessful) {
path = new Path(waypoints,transform.position, turnDst);
//targetIndex = 0;
StopCoroutine("FollowPath");
StartCoroutine("FollowPath");
}
}
IEnumerator FollowPath() {
//Vector2 currentWaypoint = path[0];
while (true) {
// if (Vector2.Distance(transform.position, currentWaypoint) < 0.1f) {
// targetIndex ++;
// if (targetIndex >= path.Length) {
// yield break;
// }
// currentWaypoint = path[targetIndex];
// }
// movementController.MoveWithController(transform.position,currentWaypoint,speed);
//
yield return null;
}
}
public void OnDrawGizmos() {
if (path != null) {
path.DrawWithGizmos();
// for (int i = targetIndex; i < path.Length; i ++) {
// Gizmos.color = Color.green;
// Gizmos.DrawCube(path[i], Vector2.one*(0.1f));
// if (i == targetIndex) {
// Gizmos.DrawLine(transform.position, path[i]);
// }
// else {
// Gizmos.DrawLine(path[i-1],path[i]);
// }
}
}
}
2dastargoof.png
(13.9 kB)
Comment