void Move() { var delta = movement.Move(); transform.Translate(delta); }
void MoveUpdate() { var delta = movement.Move(); var cameraDelta = Vector3.zero; var size = spriteRenderer.bounds.size; var range = new Rect(0f, 0f, 220f, 200f); var cp = mainCamera.transform.position; var p1 = gameObject.transform.position; var p2 = buddy.transform.position; var p1d = p1 + delta; //right if (cp.x + range.width / 2 < p1d.x) { var rx = p2.x + range.width; var deltaXd = Mathf.Min(p1d.x, rx) - p1.x; var p1dx = p1.x + deltaXd; var cx = p1dx - range.width / 2; delta.x = deltaXd; cameraDelta.x = deltaXd; } //left if (p1d.x < cp.x - range.width / 2) { var lx = p2.x - range.width; var deltaXd = Mathf.Max(p1d.x, lx) - p1.x; var p1dx = p1.x + deltaXd; var cx = p1dx + range.width / 2; delta.x = deltaXd; cameraDelta.x = deltaXd; } //up if (cp.y + range.height / 2 < p1d.y) { var uy = p2.y + range.height; var deltaYd = Mathf.Min(p1d.y, uy) - p1.y; var p1dy = p1.y + deltaYd; var cy = p1dy - range.height / 2; delta.y = deltaYd; cameraDelta.y = deltaYd; } //down if (p1d.y < cp.y - range.height / 2) { var dy = p2.y - range.height; var deltaYd = Mathf.Max(p1d.y, dy) - p1.y; var p1dy = p1.y + deltaYd; var cy = p1dy + range.height / 2; delta.y = deltaYd; cameraDelta.y = deltaYd; } //TODO: Maybe there are bug.. var background = GameObject.Find("/Layer/Background"); var sr = background.GetComponent <SpriteRenderer>(); //right if (sr.bounds.size.x / 2 <= p1.x + size.x / 2) { delta.x = p1.x - sr.bounds.size.x / 2 - size.x / 2; //TODO: Maybe It is bug.. } //left if (p1.x - size.x / 2 <= -sr.bounds.size.x / 2) { delta.x = -sr.bounds.size.x / 2 - p1.x + size.x / 2 + 1; //TODO: Maybe It is bug too.. } //up if (sr.bounds.size.y / 2 <= p1.y) { delta.y = p1.y - sr.bounds.size.y / 2 - size.y / 4; //TODO: Maybe It is bug too.. } //down if (p1.y <= -sr.bounds.size.y / 2) { delta.y = -sr.bounds.size.y / 2 - p1.y + size.x / 4; //TODO: Maybe It is bug too.. } mainCamera.transform.Translate(cameraDelta); transform.Translate(delta); }