Debug.DrawRay and console
I have a script for an enemy, that moves left and right, and when close to a wall, moves in another direction.
This is the part of the script that handles Raycasts, in Update
timer += Time.deltaTime;
Debug.Log("Right is " + right);
Debug.Log("Left is " + left);
if (timer > 1f)
{
raycastHitLeft = Physics2D.Raycast(new Vector2(transform.position.x - 1, transform.position.y), new Vector2(-2, 0));
Debug.DrawRay(new Vector3(transform.position.x - 1, transform.position.y, transform.position.z), new Vector3 (-2, 0, 0), Color.red, 1f);
raycastHitRight = Physics2D.Raycast(new Vector2(transform.position.x + 1, transform.position.y), new Vector2(2, 0));
Debug.DrawRay(new Vector3(transform.position.x + 1, transform.position.y, transform.position.z), new Vector3(2, 0, 0), Color.blue, 1f);
timer = 0;
Debug.Log("Raycast fired!");
}
I tried moving the starting position of a raycast away form the enemy, in case this would cause problems. And quadruple checked that all walls have box colliders, and that there arent any random colliders anywere.
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.