Day 45 — Improvements: dodging enemy

private bool _dodgeRight = false;
private bool _dodgeLeft = false;
private bool _dodging = false;
public void DodgeLaserRight()
{
if (_dodging == false)
{
StartCoroutine(DodgeLaserRightRoutine());
}
else
{
return;
}
}
public void DodgeLaserLeft()
{
if (_dodging == false)
{
StartCoroutine(DodgeLaserLeftRoutine());
}
else
{
return;
}
}
IEnumerator DodgeLaserRightRoutine()
{
_dodging = true;
_dodgeRight = true;
yield return new WaitForSeconds(0.5f);
_dodgeRight = false;
_dodging = false;
}
IEnumerator DodgeLaserLeftRoutine()
{
_dodging = true;
_dodgeLeft = true;
yield return new WaitForSeconds(0.5f);
_dodgeLeft = false;
_dodging = false;
}
private float _horizontalSpeed = 4.0f;if (_dodgeRight == true)
{
transform.Translate(new Vector3(1, 0, 0) * _horizontalSpeed * Time.deltaTime );
if (transform.position.x >= 9.7f || transform.position.x <= -9.7f)
{
_dodgeRight = false;
}
}
else if (_dodgeLeft == true)
{
transform.Translate(new Vector3(-1, 0, 0) * _horizontalSpeed * Time.deltaTime );
if (transform.position.x >= 9.7f || transform.position.x <= -9.7f)
{
_dodgeLeft = false;
}
}
else if (_dodgeLeft == false && _dodgeRight == false)
{
transform.Translate(new Vector3(0, -1, 0) * _verticalSpeed * Time.deltaTime);
}
if (transform.position.y < -5.3f)
{
_dodgeRight = false;
_dodgeLeft = false;
transform.position = new Vector3(Random.Range(-9.5f, 9.5f), 6.2f, 0);
}
private DodgeEnemy _dodgeEnemy;if (GameObject.Find("DodgingEnemy(Clone)") != null)
{
_dodgeEnemy = GameObject.Find("DodgingEnemy(Clone)").GetComponent<DodgeEnemy>();
}
if (_dodgeEnemy != null)
{
float distanceY = _dodgeEnemy.transform.position.y - transform.position.y;
float distanceX = _dodgeEnemy.transform.position.x - transform.position.x;
Debug.Log(distanceX);

if (distanceY < 5.0f)
{
if (distanceX > 0f && distanceX < 1.0f)
{
_dodgeEnemy.DodgeLaserRight();
}

if (distanceX < 0f && distanceX > -1.0f)
{
_dodgeEnemy.DodgeLaserLeft();
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store