Skip to content

Instantly share code, notes, and snippets.

@Bishwas-py
Last active November 11, 2021 12:16
Show Gist options
  • Save Bishwas-py/4497d510f1904b9b2e96884e892e5701 to your computer and use it in GitHub Desktop.
Save Bishwas-py/4497d510f1904b9b2e96884e892e5701 to your computer and use it in GitHub Desktop.
Enemy Follow Player Unity 2D
IEnumerator GiveJumpingJerks()
{
while (isPlayerAlive)
{
yield return new WaitForSeconds(Random.Range(1, 3));
try
{
var playerToTriangle = (player.transform.position - transform.position);
if (allowJumping)
{
myBody.AddForce(new Vector2(
playerToTriangle.normalized.x * Random.Range(playerToTriangle.magnitude - 5,
playerToTriangle.magnitude + 5),
Random.Range(1, jumpingJerkRange)), ForceMode2D.Impulse);
myBody.rotation += rotationSpeed * 3;
}
allowJumping = false;
}
catch(MissingReferenceException fException)
{
isPlayerAlive = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment