public void EnemyMove() { dyst = new Vector2(Player.x - transform.position.x, Player.y - transform.position.y); dystE = new Vector2(Eagle.x - transform.position.x, Eagle.y - transform.position.y); dyst2 = new Vector2(Player2.x - transform.position.x, Player2.y - transform.position.y); if (Application.loadedLevel == 8 || Application.loadedLevel > 9) { if ((Mathf.Abs(dyst2.x) + Mathf.Abs(dyst2.y)) < (Mathf.Abs(dyst.x) + Mathf.Abs(dyst.y))) { dyst = dyst2; } } else { if ((Mathf.Abs(dystE.x) + Mathf.Abs(dystE.y)) / 1.5f < (Mathf.Abs(dyst.x) + Mathf.Abs(dyst.y))) { dyst = dystE; } } for (int i = 0; i < tabColl.Length; i++) //wchodzimy tylko jak napotka juz przeszkode { if (tabColl[i] && obstacle == 4) { obstacle = i; } else if (!tabColl[0] && !tabColl[1] && !tabColl[2] && !tabColl[3] && obstacle != 4) { obstacle = 4; } } if (obstacle != 4) // && obstacle2 == 4 { switch (obstacle) { case 0: //gora case 2: //dol if (dyst.x > 0) { dir = lastDirection.Right; rb2d.velocity = Vector2.Scale(Vector2.right, tankSpeed); Debug.Log("Velocity " + rb2d.velocity); animator.SetBool("FacingRight", true); ResetFacesCustom("FacingRight"); } else if (dyst.x < 0) { dir = lastDirection.Left; rb2d.velocity = Vector2.Scale(Vector2.left, tankSpeed); animator.SetBool("FacingLeft", true); ResetFacesCustom("FacingLeft"); } break; case 1: //prawo case 3: //lewo if (dyst.y > 0) { dir = lastDirection.Up; rb2d.velocity = Vector2.Scale(Vector2.up, tankSpeed); animator.SetBool("FacingUp", true); ResetFacesCustom("FacingUp"); } else if (dyst.y < 0) { dir = lastDirection.Down; rb2d.velocity = Vector2.Scale(Vector2.down, tankSpeed); animator.SetBool("FacingDown", true); ResetFacesCustom("FacingDown"); } break; } return; } if (obstacle == 4) { if (dyst.x < 0 && dyst.y < 0) // tu dodamy && zeby tylko woda //czy obie ujemne { if (dyst.x < dyst.y) //ruch w lewo { dir = lastDirection.Left; rb2d.velocity = Vector2.Scale(Vector2.left, tankSpeed); animator.SetBool("FacingLeft", true); ResetFacesCustom("FacingLeft"); } else //ruch w dol { dir = lastDirection.Down; rb2d.velocity = Vector2.Scale(Vector2.down, tankSpeed); animator.SetBool("FacingDown", true); ResetFacesCustom("FacingDown"); } } else // ruch w prawo i w górę { if (Mathf.Abs(dyst.x) > Mathf.Abs(dyst.y))//determinuje nam czy mozemy ruszyc sie w prawo { if (dyst.x > 0) { dir = lastDirection.Right; rb2d.velocity = Vector2.Scale(Vector2.right, tankSpeed); animator.SetBool("FacingRight", true); ResetFacesCustom("FacingRight"); } else if (dyst.x < 0) { dir = lastDirection.Left; rb2d.velocity = Vector2.Scale(Vector2.left, tankSpeed); animator.SetBool("FacingLeft", true); ResetFacesCustom("FacingLeft"); } } else//determinuje nam czy mozemy ruszyc sie w górę { if (dyst.y > 0) { dir = lastDirection.Up; rb2d.velocity = Vector2.Scale(Vector2.up, tankSpeed); animator.SetBool("FacingUp", true); ResetFacesCustom("FacingUp"); } else if (dyst.y < 0) { dir = lastDirection.Down; rb2d.velocity = Vector2.Scale(Vector2.down, tankSpeed); animator.SetBool("FacingDown", true); ResetFacesCustom("FacingDown"); } } } } }
void Update() { float x = Input.GetAxisRaw("Horizontal2"); float y = Input.GetAxisRaw("Vertical2"); Vector2 direction = new Vector2(x, y); if (x > 0) { animator.SetBool("FacingRight", true); lastDir = lastDirection.Right; } else if (x < 0) { animator.SetBool("FacingLeft", true); lastDir = lastDirection.Left; } else if (y > 0) { animator.SetBool("FacingUp", true); lastDir = lastDirection.Up; } else if (y < 0) { animator.SetBool("FacingDown", true); lastDir = lastDirection.Down; } switch (lastDir) { case lastDirection.Up: transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0)); ResetFacesCustom("FacingUp"); break; case lastDirection.Down: transform.rotation = Quaternion.Euler(new Vector3(0, 180f, 180f)); ResetFacesCustom("FacingDown"); break; case lastDirection.Right: transform.rotation = Quaternion.Euler(new Vector3(0, 0, -90f)); ResetFacesCustom("FacingRight"); break; case lastDirection.Left: transform.rotation = Quaternion.Euler(new Vector3(180f, 0, 90f)); ResetFacesCustom("FacingLeft"); break; default: break; } if (Mathf.Abs(direction.x) > 0 || Mathf.Abs(direction.y) > 0) { Move(direction); } else if (Mathf.Abs(direction.x) > 0 && Mathf.Abs(direction.y) > 0) { Move(direction); } else { animator.SetBool("Moving", false); ResetFaces(); } }
void FixedUpdate() { if (perfectAimTime) { Debug.Log("perfectAim"); if ((tabColl[1] || tabColl[3]) && ((transform.position.y >= (Player.y - zasieg)) && (transform.position.y < (Player.y + zasieg)))) { Debug.Log(tabColl + " TP X " + transform.position.x + " TP Y " + transform.position.y + " Player X " + Player.x + " Player Y " + Player.y); if (transform.position.x < Player.x) { dir = lastDirection.Right; rb2d.velocity = Vector2.Scale(Vector2.zero, tankSpeed); animator.SetBool("FacingRight", true); ResetFacesCustom("FacingRight"); } else if (transform.position.x > Player.x) { dir = lastDirection.Left; rb2d.velocity = Vector2.Scale(Vector2.zero, tankSpeed); animator.SetBool("FacingLeft", true); ResetFacesCustom("FacingLeft"); } } else if ((tabColl[0] || tabColl[2]) && ((transform.position.x >= (Player.x - zasieg)) && (transform.position.x < (Player.x + zasieg)))) { Debug.Log(tabColl + " TP X " + transform.position.x + " TP Y " + transform.position.y + " Player X " + Player.x + " Player Y " + Player.y); if (transform.position.y < Player.y) { dir = lastDirection.Up; rb2d.velocity = Vector2.Scale(Vector2.zero, tankSpeed); animator.SetBool("FacingUp", true); ResetFacesCustom("FacingUp"); } else if (transform.position.y > Player.y) { dir = lastDirection.Up; rb2d.velocity = Vector2.Scale(Vector2.zero, tankSpeed); animator.SetBool("FacingDown", true); ResetFacesCustom("FacingDown"); } } perfectAimTime = false; return; } Rigidbody2D bulletInstance = new Rigidbody2D(); Vector3 bullet_pos = new Vector3(0, 0, 0); if (!bulletFired) { switch (dir) { case lastDirection.Right: bulletInstance = Instantiate(bulletPrefEnemy, transform.position, Quaternion.Euler(new Vector3(0, 0, 0))) as Rigidbody2D; bulletInstance.velocity = new Vector2(bulletSpeedEnemy, 0); bullet_pos = bulletInstance.transform.position; bullet_pos.x += 0.6f; bulletInstance.transform.position = bullet_pos; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 180f, 90f)); break; case lastDirection.Left: bulletInstance = Instantiate(bulletPrefEnemy, transform.position, Quaternion.Euler(new Vector3(0, 0, 180f))) as Rigidbody2D; bulletInstance.velocity = new Vector2(-bulletSpeedEnemy, 0); bullet_pos = bulletInstance.transform.position; bullet_pos.x -= 0.6f; bulletInstance.transform.position = bullet_pos; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90f)); break; case lastDirection.Up: bulletInstance = Instantiate(bulletPrefEnemy, transform.position, Quaternion.Euler(new Vector3(180f, 0, 0))) as Rigidbody2D; bulletInstance.velocity = new Vector2(0, bulletSpeedEnemy); bullet_pos = bulletInstance.transform.position; bullet_pos.y += 0.6f; bulletInstance.transform.position = bullet_pos; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0)); break; case lastDirection.Down: bulletInstance = Instantiate(bulletPrefEnemy, transform.position, Quaternion.Euler(new Vector3(0, 180f, 0))) as Rigidbody2D; bulletInstance.velocity = new Vector2(0, -bulletSpeedEnemy); bullet_pos = bulletInstance.transform.position; bullet_pos.y -= 0.6f; bulletInstance.transform.position = bullet_pos; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 180f)); break; } Destroy(bulletInstance.gameObject, 5); bulletFired = true; } if (Time.time > nextUsage) { nextUsage = Time.time + delayMove; EnemyMove(); } if (Time.time > nextFire && !slowed) { Debug.Log("TU"); nextFire = Time.time + bulletCd; bulletFired = false; } if (Time.time > nextAim) { nextAim = Time.time + delayPerfectAim; perfectAimTime = true; } }
void Update() { float x = Input.GetAxisRaw("Horizontal"); float y = Input.GetAxisRaw("Vertical"); Vector2 direction = new Vector2(x, y); if (x > 0) { animator.SetBool("FacingRight", true); lastDir = lastDirection.Right; } else if (x < 0) { animator.SetBool("FacingLeft", true); lastDir = lastDirection.Left; } else if (y > 0) { animator.SetBool("FacingUp", true); lastDir = lastDirection.Up; } else if (y < 0) { animator.SetBool("FacingDown", true); lastDir = lastDirection.Down; } switch (lastDir) { case lastDirection.Up: transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0)); ResetFacesCustom("FacingUp"); break; case lastDirection.Down: transform.rotation = Quaternion.Euler(new Vector3(0, 180f, 180f)); ResetFacesCustom("FacingDown"); break; case lastDirection.Right: transform.rotation = Quaternion.Euler(new Vector3(0, 0, -90f)); ResetFacesCustom("FacingRight"); break; case lastDirection.Left: transform.rotation = Quaternion.Euler(new Vector3(180f, 0, 90f)); ResetFacesCustom("FacingLeft"); break; default: break; } if (Mathf.Abs(direction.x) > 0 || Mathf.Abs(direction.y) > 0) { Move(direction); } else if (Mathf.Abs(direction.x) > 0 && Mathf.Abs(direction.y) > 0) { Move(direction); } else { animator.SetBool("Moving", false); ResetFaces(); } if (Input.GetButtonDown("Fire1") && bulletLevel != 4) { PlaySound(0); Rigidbody2D bulletInstance = new Rigidbody2D(); if (lastDir == lastDirection.Right) { bulletInstance = Instantiate(bulletPref[bulletLevel], transform.position, transform.rotation) as Rigidbody2D; bulletInstance.velocity = new Vector2(bulletSpeed, 0); Vector3 temp = bulletInstance.transform.position; temp.x += 0.7f; bulletInstance.transform.position = temp; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 180f, 90f)); } else if (lastDir == lastDirection.Left) { bulletInstance = Instantiate(bulletPref[bulletLevel], transform.position, transform.rotation) as Rigidbody2D; bulletInstance.velocity = new Vector2(-bulletSpeed, 0); Vector3 temp = bulletInstance.transform.position; temp.x -= 0.7f; bulletInstance.transform.position = temp; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90f)); } else if (lastDir == lastDirection.Up) { bulletInstance = Instantiate(bulletPref[bulletLevel], transform.position, transform.rotation) as Rigidbody2D; bulletInstance.velocity = new Vector2(0, bulletSpeed); Vector3 temp = bulletInstance.transform.position; temp.y += 0.7f; bulletInstance.transform.position = temp; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0)); } else if (lastDir == lastDirection.Down) { bulletInstance = Instantiate(bulletPref[bulletLevel], transform.position, transform.rotation) as Rigidbody2D; bulletInstance.velocity = new Vector2(0, -bulletSpeed); Vector3 temp = bulletInstance.transform.position; temp.y -= 0.7f; bulletInstance.transform.position = temp; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 180f)); } Destroy(bulletInstance.gameObject, 5); } }
public void EnemyMove() { dyst = new Vector2(Player.x - transform.position.x, Player.y - transform.position.y); dystE = new Vector2(Eagle.x - transform.position.x, Eagle.y - transform.position.y); dyst2 = new Vector2(Player2.x - transform.position.x, Player2.y - transform.position.y); if (Application.loadedLevel == 8 || Application.loadedLevel > 9) { if ((Mathf.Abs(dyst2.x) + Mathf.Abs(dyst2.y)) < (Mathf.Abs(dyst.x) + Mathf.Abs(dyst.y))) { dyst = dyst2; } } else { if ((Mathf.Abs(dystE.x) + Mathf.Abs(dystE.y)) / 1.5f < (Mathf.Abs(dyst.x) + Mathf.Abs(dyst.y))) { dyst = dystE; } } for (int i = 0; i < tabColl.Length; i++) { if (tabColl[i] && obstacle == 4) { obstacle = i; } else if (!tabColl[0] && !tabColl[1] && !tabColl[2] && !tabColl[3] && obstacle != 4) { obstacle = 4; } } if (obstacle != 4) { switch (obstacle) { case 0: //gora case 2: //dol if (dyst.x > 0) { dir = lastDirection.Right; rb2d.velocity = Vector2.Scale(Vector2.right, tankSpeed); animator.SetBool("FacingRight", true); ResetFacesCustom("FacingRight"); } else if (dyst.x < 0) { dir = lastDirection.Left; rb2d.velocity = Vector2.Scale(Vector2.left, tankSpeed); animator.SetBool("FacingLeft", true); ResetFacesCustom("FacingLeft"); } break; case 1: //prawo case 3: //lewo if (dyst.y > 0) { dir = lastDirection.Up; rb2d.velocity = Vector2.Scale(Vector2.up, tankSpeed); animator.SetBool("FacingUp", true); ResetFacesCustom("FacingUp"); } else if (dyst.y < 0) { dir = lastDirection.Down; rb2d.velocity = Vector2.Scale(Vector2.down, tankSpeed); animator.SetBool("FacingDown", true); ResetFacesCustom("FacingDown"); } break; } return; } if (obstacle == 4) { if (dyst.x < 0 && dyst.y < 0) { if (dyst.x < dyst.y) { dir = lastDirection.Left; rb2d.velocity = Vector2.Scale(Vector2.left, tankSpeed); animator.SetBool("FacingLeft", true); ResetFacesCustom("FacingLeft"); } else { dir = lastDirection.Down; rb2d.velocity = Vector2.Scale(Vector2.down, tankSpeed); animator.SetBool("FacingDown", true); ResetFacesCustom("FacingDown"); } } else { if (Mathf.Abs(dyst.x) > Mathf.Abs(dyst.y)) { if (dyst.x > 0) { dir = lastDirection.Right; rb2d.velocity = Vector2.Scale(Vector2.right, tankSpeed); animator.SetBool("FacingRight", true); ResetFacesCustom("FacingRight"); } else if (dyst.x < 0) { dir = lastDirection.Left; rb2d.velocity = Vector2.Scale(Vector2.left, tankSpeed); animator.SetBool("FacingLeft", true); ResetFacesCustom("FacingLeft"); } } else { if (dyst.y > 0) { dir = lastDirection.Up; rb2d.velocity = Vector2.Scale(Vector2.up, tankSpeed); animator.SetBool("FacingUp", true); ResetFacesCustom("FacingUp"); } else if (dyst.y < 0) { dir = lastDirection.Down; rb2d.velocity = Vector2.Scale(Vector2.down, tankSpeed); animator.SetBool("FacingDown", true); ResetFacesCustom("FacingDown"); } } } } }
void UpdateGameplay(GameTime deltaTime) { // Respond to user actions in the game. // Update enemies // Handle collisions //Hide the mouse pointer IsMouseVisible = false; #region Moiveing The Troop #region Standing Still, Right, Left, Up, Down //Setsplayer to idle switch (_lastDirection) { case lastDirection.Left: frameRow = walkingLeftFrameRow; totalFrames = 0; break; case lastDirection.Right: frameRow = walkingRightFrameRow; totalFrames = 0; break; case lastDirection.Up: frameRow = walkingUpFrameRow; totalFrames = 0; break; case lastDirection.Down: frameRow = walkingDownFrameRow; totalFrames = 0; break; } if (Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D)) { TroopPosition.X += movingSpeed; TroopBodyCollider.X += movingSpeed; TroopFeetCollider.X += movingSpeed; frameRow = walkingRightFrameRow; totalFrames = 8; _lastDirection = lastDirection.Right; //Get what the troop is intersecting with //if water stop moveing if (troopCollisionDectector(2)) { TroopPosition.X -= movingSpeed; TroopBodyCollider.X -= movingSpeed; TroopFeetCollider.X -= movingSpeed; } //if sand slow down if (troopCollisionDectector(3)) { movingSpeed = sandSpeed; framePlayTime = sandPlaySpeed; } else { movingSpeed = grassSpeed; framePlayTime = normalPlaySpeed; } } else if (Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A)) { TroopPosition.X -= movingSpeed; TroopBodyCollider.X -= movingSpeed; TroopFeetCollider.X -= movingSpeed; frameRow = walkingLeftFrameRow; totalFrames = 8; _lastDirection = lastDirection.Left; //Get what the troop is intersecting with //if water stop moveing if (troopCollisionDectector(2)) { TroopPosition.X += movingSpeed; TroopBodyCollider.X += movingSpeed; TroopFeetCollider.X += movingSpeed; } //if sand slow down if (troopCollisionDectector(3)) { movingSpeed = sandSpeed; framePlayTime = sandPlaySpeed; } else { movingSpeed = grassSpeed; framePlayTime = normalPlaySpeed; } } else if (Keyboard.GetState().IsKeyDown(Keys.Up) || Keyboard.GetState().IsKeyDown(Keys.W)) { TroopPosition.Y -= movingSpeed; TroopBodyCollider.Y -= movingSpeed; TroopFeetCollider.Y -= movingSpeed; frameRow = walkingUpFrameRow; totalFrames = 8; _lastDirection = lastDirection.Up; //Get what the troop is intersecting with //if water stop moveing if (troopCollisionDectector(2)) { TroopPosition.Y += movingSpeed; TroopBodyCollider.Y += movingSpeed; TroopFeetCollider.Y += movingSpeed; } //if sand slow down if (troopCollisionDectector(3)) { movingSpeed = sandSpeed; framePlayTime = sandPlaySpeed; } else { movingSpeed = grassSpeed; framePlayTime = normalPlaySpeed; } } else if (Keyboard.GetState().IsKeyDown(Keys.Down) || Keyboard.GetState().IsKeyDown(Keys.S)) { TroopPosition.Y += movingSpeed; TroopBodyCollider.Y += movingSpeed; TroopFeetCollider.Y += movingSpeed; frameRow = walkingDownFrameRow; totalFrames = 8; _lastDirection = lastDirection.Down; //Get what the troop is intersecting with //if water stop moveing if (troopCollisionDectector(2)) { TroopPosition.Y -= movingSpeed; TroopBodyCollider.Y -= movingSpeed; TroopFeetCollider.Y -= movingSpeed; } //if sand slow down if (troopCollisionDectector(3)) { movingSpeed = sandSpeed; framePlayTime = sandPlaySpeed; } else { movingSpeed = grassSpeed; framePlayTime = normalPlaySpeed; } } #endregion #region Troop Game Boarder Collider if (TroopBodyCollider.X + TROOP_BODY_SIZE_X > GAME_BOARD_WIDTH * TILE_SIZE) { TroopPosition.X -= movingSpeed; TroopBodyCollider.X -= movingSpeed; TroopFeetCollider.X -= movingSpeed; } if (TroopBodyCollider.X < 0) { TroopPosition.X += movingSpeed; TroopBodyCollider.X += movingSpeed; TroopFeetCollider.X += movingSpeed; } if (TroopBodyCollider.Y + TROOP_BODY_SIZE_Y > GAME_BOARD_LENGTH * TILE_SIZE) { TroopPosition.Y -= movingSpeed; TroopBodyCollider.Y -= movingSpeed; TroopFeetCollider.Y -= movingSpeed; } if (TroopBodyCollider.Y < 0) { TroopPosition.Y += movingSpeed; TroopBodyCollider.Y += movingSpeed; TroopFeetCollider.Y += movingSpeed; } #endregion #region Troop Finish Colider if (troopCollisionDectector(4)) { _state = GameState.EndOfGame; } #endregion #region Troop Animation //Animation // Process elapsed time timeShown += (float)deltaTime.ElapsedGameTime.TotalSeconds; while (timeShown > framePlayTime) { // Play the next frame in the SpriteSheet frameIndex++; // reset elapsed time timeShown = 0f; } if (frameIndex > totalFrames) { frameIndex = 0; } #endregion #endregion //Set Collider Positions //if (playerDied) // _state = GameState.EndOfGame; }
void Update() { tempxx = lastDir.ToString(); // Debuging purpose float x = Input.GetAxisRaw("Horizontal"); float y = Input.GetAxisRaw("Vertical"); Vector2 direction = new Vector2(x, y); if (x > 0 || TouchDirection.x > 0) { animator.SetBool("FacingRight", true); lastDir = lastDirection.Right; } else if (x < 0 || TouchDirection.x < 0) { animator.SetBool("FacingLeft", true); lastDir = lastDirection.Left; } else if (y > 0 || TouchDirection.y > 0) { animator.SetBool("FacingUp", true); lastDir = lastDirection.Up; } else if (y < 0 || TouchDirection.y < 0) { animator.SetBool("FacingDown", true); lastDir = lastDirection.Down; } switch (lastDir) { case lastDirection.Up: transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0)); ResetFacesCustom("FacingUp"); break; case lastDirection.Down: transform.rotation = Quaternion.Euler(new Vector3(0, 180f, 180f)); ResetFacesCustom("FacingDown"); break; case lastDirection.Right: transform.rotation = Quaternion.Euler(new Vector3(0, 0, -90f)); ResetFacesCustom("FacingRight"); break; case lastDirection.Left: transform.rotation = Quaternion.Euler(new Vector3(180f, 0, 90f)); ResetFacesCustom("FacingLeft"); break; default: break; } #region fire if (Input.GetButtonDown("Fire1")) { //animator.SetTrigger("Shoot"); Rigidbody2D bulletInstance = new Rigidbody2D(); if (lastDir == lastDirection.Right) { // ... instantiate the bullet_pref Facing right and set it's velocity to the right. bulletInstance = Instantiate(bullet_pref, transform.position, Quaternion.Euler(new Vector3(0, 0, 0))) as Rigidbody2D; bulletInstance.velocity = new Vector2(bullet_speed, 0); Vector3 temp = bulletInstance.transform.position; temp.x += 0.2f; bulletInstance.transform.position = temp; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 180f, 90f)); } else if (lastDir == lastDirection.Left) { // Otherwise instantiate the bullet_pref Facing left and set it's velocity to the left. bulletInstance = Instantiate(bullet_pref, transform.position, Quaternion.Euler(new Vector3(0, 0, 180f))) as Rigidbody2D; bulletInstance.velocity = new Vector2(-bullet_speed, 0); Vector3 temp = bulletInstance.transform.position; temp.x -= 0.2f; bulletInstance.transform.position = temp; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90f)); } else if (lastDir == lastDirection.Up) { bulletInstance = Instantiate(bullet_pref, transform.position, Quaternion.Euler(new Vector3(180f, 0, 0))) as Rigidbody2D; bulletInstance.velocity = new Vector2(0, bullet_speed); Vector3 temp = bulletInstance.transform.position; temp.y += 0.2f; bulletInstance.transform.position = temp; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0)); } else if (lastDir == lastDirection.Down) { bulletInstance = Instantiate(bullet_pref, transform.position, Quaternion.Euler(new Vector3(0, 180f, 0))) as Rigidbody2D; bulletInstance.velocity = new Vector2(0, -bullet_speed); Vector3 temp = bulletInstance.transform.position; temp.y -= 0.2f; bulletInstance.transform.position = temp; bulletInstance.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 180f)); } Destroy(bulletInstance.gameObject, 5); } #endregion if (IsTouching == false) { if (Mathf.Abs(direction.x) > 0 || Mathf.Abs(direction.y) > 0) { Move(direction); } else if (Mathf.Abs(direction.x) > 0 && Mathf.Abs(direction.y) > 0) { Move(direction); } else { animator.SetBool("Moving", false); ResetFaces(); } } else { // Debug.Log("ruch z toucha: " + TouchDirection.ToString()); Move(TouchDirection); } }
public void EnemyMove() { #region old_code //przeciez x enemey i punktu c zawsze są równe /* if (Mathf.Abs(transform.position.x - temp2.x) > 0.5f) * { * Debug.Log("jestem w x"); * temp.y = 0; * if (transform.position.x - temp2.x > 0) temp.x -= 1; * else temp.x += 1; * }*/ /* * if (Mathf.Abs(transform.position.y - temp2.y) == 0 && Mathf.Abs(transform.position.x - temp2.x) != 0) * { * Debug.Log("jestem w x"); * temp.y = 0; * if (transform.position.x - Player.x > 0) temp.x = -1; * else temp.x = 1; * } * else if(Mathf.Abs(transform.position.y - temp2.y) == 0 && Mathf.Abs(transform.position.x - temp2.x) == 0) * { * Debug.Log("jestem w y"); * temp.x = 0; * if (transform.position.y - temp2.y > 0) temp.y = -1; * else temp.y = 1; * } */ #endregion // Debug.Log("Temp2: " + temp2 + " , enemy: " + transform.position); #region random_movement /* * int asdf = Random.Range(1, 4); * switch (asdf) * { * case 1: //gora * temp.y = 0.5f; * temp.x = 0; * break; * * case 2: //prawo * temp.y = 0; * temp.x = 0.5f; * break; * * case 3: //dol * temp.y = -0.5f; * temp.x = 0; * break; * * case 4: //lewo * temp.y = 0; * temp.x = -0.5f; * break; * * } */ #endregion //animator.SetBool("Moving", true); Vector2 P = new Vector2(Player.x, Player.y); Vector2 E = new Vector2(transform.position.x, transform.position.y); bool test = (Mathf.Abs(Mathf.Abs(P.x) - Mathf.Abs(E.x)) > Mathf.Abs(Mathf.Abs(P.y) - Mathf.Abs(E.y))); if (Mathf.Abs(Mathf.Abs(P.x) - Mathf.Abs(E.x)) > Mathf.Abs(Mathf.Abs(P.y) - Mathf.Abs(E.y))) //go horizontal { // Debug.Log(); // Debug.Log("Go Left/Right " + "Longer Distance!"); temp.y = 0; if (P.x > E.x) { dir = lastDirection.Right; animator.SetBool("FacingRight", true); ResetFacesCustom("FacingRight"); temp.x = speed; } else if (P.x <= E.x) { dir = lastDirection.Left; animator.SetBool("FacingLeft", true); ResetFacesCustom("FacingLeft"); temp.x = -speed; } else { Debug.Log("OMG WTF"); } } else if (Mathf.Abs(Mathf.Abs(P.x) - Mathf.Abs(E.x)) <= Mathf.Abs(Mathf.Abs(P.y) - Mathf.Abs(E.y))) //go vertical { // Debug.Log("Go Up/Down, Longer Distance!"); temp.x = 0; if (P.y > E.y) { dir = lastDirection.Up; animator.SetBool("FacingUp", true); ResetFacesCustom("FacingUp"); temp.y = speed; } else if (P.y <= E.y) { dir = lastDirection.Down; animator.SetBool("FacingDown", true); ResetFacesCustom("FacingDown"); temp.y = -speed; } else { Debug.Log("OMG WTF"); } } else { Debug.Log("OMG, HUSTON WE HAVE A PROBLEMS"); } rb2d.velocity = temp; }