- Home /
Question by
vitelksnys · Apr 12, 2021 at 09:00 PM ·
raycastai
Implementing BUG2 algorithm
Hello! I'm trying to implement robotic BUG algorithm. My starting position is point A and my finish is point B. Robot should always follow straight line to the point B and if any obstacles are encountered robot must turn left. I already have reposition algorithm which I need for robot to get back into straight line. I have everything I need but I don't know how to make a robot go smoothly around obstacles. Raycasting does not seems to work as I expected because at the corner of the obstacle it starts to move half through the obstacle, like this: This is how robot should move according to algorithm: This is my code:
public Vector3 pradzia;
public Vector3 tikslas;
public Vector3 startCoords;
public float linearFunc;
public float divisor;
public float dist;
RaycastHit2D E;
RaycastHit2D N;
RaycastHit2D W;
RaycastHit2D S;
// Start is called before the first frame update
void Start()
{
startCoords = GameObject.Find("Robotukas").transform.position;
tikslas = GameObject.Find("Tikslas").transform.position;
}
// Update is called once per frame
void Update()
{
// tiese p1(-24;0), p2(24;-5)
//dabartinis -2; 2
//dist > 0.004
pradzia = GameObject.Find("Robotukas").transform.position;
var dir1 = Quaternion.Euler(0, 45, 0) * transform.forward;
var dir2 = Quaternion.Euler(0, 135, 0) * transform.forward;
var dir3 = Quaternion.Euler(0, 225, 0) * transform.forward;
var dir4 = Quaternion.Euler(0, 315, 0) * transform.forward;
E = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.right), 1f);
N = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.up), 1f);
W = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.left), 1f);
S = Physics2D.Raycast(transform.position, transform.TransformDirection(Vector2.down), 1f);
RaycastHit2D NE = Physics2D.Raycast(transform.position, dir1, 1f);
RaycastHit2D SE = Physics2D.Raycast(transform.position, dir2, 1f);
RaycastHit2D SW = Physics2D.Raycast(transform.position, dir3, 1f);
RaycastHit2D NW = Physics2D.Raycast(transform.position, dir4, 1f);
linearFunc = Math.Abs((startCoords.y - tikslas.y) * pradzia.x + (startCoords.x - -pradzia.x) * pradzia.y + ((startCoords.x * tikslas.y) - (tikslas.x * startCoords.y)));
divisor = (float)Math.Sqrt(Math.Pow((startCoords.y - tikslas.y), 2) + Math.Pow((tikslas.x - startCoords.x), 2));
dist = linearFunc / divisor;
if (E && !N)
{
transform.Translate(Vector3.up * Time.deltaTime * 3f);
}
else if (S && !E)
{
transform.Translate(Vector3.right * Time.deltaTime * 3f);
}
else if (N)
{
if (!W)
transform.Translate(Vector3.left * Time.deltaTime * 3f);
}
else if (W)
{
if (!S)
transform.Translate(Vector3.down * Time.deltaTime * 3f);
}
else
transform.position = Vector3.MoveTowards(pradzia, tikslas, Time.deltaTime * 3f);
screenshot-2021-04-12-101113.jpg
(2.7 kB)
untitled.jpg
(66.0 kB)
Comment
Your answer
Follow this Question
Related Questions
AI Field of vision 1 Answer
Another Raycast Issue. 0 Answers
Enemy raycast detection 0 Answers
2D Field of View 2 Answers