private void Update() { if (target == null) { target = FindTarget(); } }
public override void MoveTowards(AttackableBuilding target) { var direction = target.PathFrom(transform.position); if (direction == MoveDirection.Left || direction == MoveDirection.Down) { _lastHorizontal = direction; } if (_lastHorizontal == MoveDirection.Left) { transform.localScale = new Vector3(Math.Abs(transform.localScale.x) * -1, transform.localScale.y, transform.localScale.z); } else if (target.transform.position.x > transform.position.x + _monster.Attributes.Range) { transform.localScale = new Vector3(Math.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z); } switch (direction) { case MoveDirection.Left: case MoveDirection.Right: var forceDir = direction == MoveDirection.Left ? Vector3.left : Vector3.right; _rigidbody2D.AddForce(forceDir * _monster.Attributes.MoveForce); if (Mathf.Abs(_rigidbody2D.velocity.x) > _monster.Attributes.MaxSpeed) { _rigidbody2D.velocity = new Vector2( Mathf.Sign(_rigidbody2D.velocity.x) * _monster.Attributes.MaxSpeed, _rigidbody2D.velocity.y); } break; case MoveDirection.Up: _rigidbody2D.AddForce(new Vector2(0, _monster.Attributes.JumpForce)); break; case MoveDirection.Down: case MoveDirection.None: // change animation to idle _rigidbody2D.velocity = new Vector2(0, _rigidbody2D.velocity.y); break; default: throw new ArgumentOutOfRangeException(); } }
public override void MoveTowards(AttackableBuilding target) { if (!_knowsPosition) { _cell = _monster.Placeables.WorldToCell(transform.position); _knowsPosition = true; } if (_wait <= 0) { _wait = WaitAfterMoveS; _cell = target.NextCell(_cell, steps); // make sure worm is on top of a ground tile var cellOnTopOfGround = _cell; const int min = -1000; while (_monster.Terrain.GetTile(cellOnTopOfGround + Vector3Int.down) == null && cellOnTopOfGround.y > min) { cellOnTopOfGround += Vector3Int.down; } if (cellOnTopOfGround.y > min) { _cell = cellOnTopOfGround; } var targetPos = _monster.Placeables.CellToWorld(_cell); // perturbate position a bit transform.position = new Vector3(targetPos.x + Random.Range(-0.5f, 0.5f), targetPos.y + Random.Range(0.4f, 0.8f), transform.position.z); } else { _wait -= Time.fixedDeltaTime; } }
/// <summary> /// Call in FixedUpdate to Move the monster /// </summary> /// <param name="attackableBuilding"></param> public abstract void MoveTowards(AttackableBuilding attackableBuilding);