So, im making really simple 2d platformer game, and im making super basic enemy, that detects if there is a wall in front of it, and it there is, it changes directions. But when i start the game, after 1 sec for the 1st raycast, the enemy stops moving.
I tried A LOT of diffrent things, I think this is like 4th version of the script.
private enum Moving
{
MovingToRight,
MovingToLeft,
Idle
}
private void Update()
{
timer += Time.deltaTime;
if (timer > 1f)
{
Debug.Log("Raycast fired!");
raycastHitLeft = Physics2D.Raycast(transform.position, new Vector3(-9, -3.5f, 0), 2);
raycastHitRight = Physics2D.Raycast(transform.position, new Vector3(9, -3.5f, 0), 2);
timer = 0;
}
if (raycastHitRight.collider != null)
{
Debug.Log("Right Raycast Not Hitting Anything, Should Move!");
moving = Moving.MovingToRight;
}
else
{
moving = Moving.Idle;
}
if (raycastHitLeft.collider != null)
{
Debug.Log("Left Raycast Not Hitting Anything, Should Move!");
moving = Moving.MovingToLeft;
}
else
{
moving = Moving.Idle;
}
while (moving == Moving.MovingToRight && moving != Moving.MovingToLeft)
{
transform.position += new Vector3(1, 0, 0) * Time.deltaTime;
return;
}
while (moving == Moving.MovingToLeft && moving != Moving.MovingToRight)
{
transform.position += new Vector3(-1, 0, 0) * Time.deltaTime;
return;
}
while (moving == Moving.MovingToRight && moving == Moving.MovingToLeft)
{
//If both true, move to right
transform.position += new Vector3(1, 0, 0) * Time.deltaTime;
return;
}
New contributor
Svak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.