I was making a Ai which jumps at random direction at a random time and it only goes in one direction
public float speed;
private Rigidbody2D rb;
float jumpTime; // this value is set to random so enemy jumps in random time interval
float nextJumpTime;
float jumpdir;// this is set to random so that player jumps in a random directon
void Awake()
{
rb = GetComponent<Rigidbody2D>();
nextJumpTime = 2;
jumpdir = 1;
}
void FixedUpdate()
{
jumpTime = Random.Range(3, 7);//random time code;
jumpdir = Random.Range(1, 2);//random direction code;
Debug.Log(jumpdir);
if (jumpdir ==2 && nextJumpTime < Time.time)//to jump left in random time
{
nextJumpTime = jumpTime + Time.time + 1f;
rb.AddForce(new Vector2(1f, 1f) * speed, ForceMode2D.Impulse);
Debug.Log("jumped right");
}
else if (jumpdir==1 && nextJumpTime < Time.time) // to jump right in random time
{
nextJumpTime = jumpTime + Time.time + 1f;
rb.AddForce(new Vector2(-1f, 1f) * speed, ForceMode2D.Impulse);
Debug.Log("jumped left");
}
}
}