public void AIMove() { if (Time.time - moveLastTime < moveInterval) { return; } int moveAmount = 0; if (footballPos.x < playerPos.x - 0.5f) { moveAmount = -1; } if (footballPos.x > playerPos.x + 0.5f) { moveAmount = 1; } if (Mathf.Abs(footballPos.x - playerPos.x) < 2f) { if (Random.Range(0f, 1f) < 0.5f) { moveAmount = Random.Range(-1, 2); } } if (moveAmount != 0) { moveLastTime = Time.time; } controller2D.DoMoveAmount(moveAmount); }
public IEnumerator AIMove() { if (Time.time - moveLastTime < moveInterval) { yield break; } int moveAmount = 0; if (footballPos.x < playerPos.x - 1.3f) { moveAmount = -1; // can't touch ball. } else { if (footballPos.x < playerPos.x - 0.7f - posDeltaX) //powerful push distance. { if (footballPos.y > playerPos.y + 2.3f || footballPos.y < playerPos.y - 0.3f) { moveAmount = 0; } else if (footballPos.x > playerPos.x - 0.65f - posDeltaX) { moveAmount = 0; } else { moveAmount = -1; } } else if (footballPos.x < playerPos.x && footballPos.y > playerPos.y - 0.3f && footballPos.y < playerPos.y + 2.3f) { moveAmount = -1; //if football is in front of AI and AI can touch it. } else if (footballPos.x > playerPos.x - 0.65f - posDeltaX && footballPos.x < playerPos.x - 0.75f - posDeltaX) { moveAmount = 0; // To avoid AI shake. } else { moveAmount = 1; } } Vector3 posDelta = oppoPlayerTrans.position - playerPos; if (playerPos.x < -3f && Mathf.Abs(posDelta.x) < 1f && posDelta.y > 1f) { moveAmount = 1; } // Keep little distance from the players goal if (playerPos.x > -6.2f && playerPos.x < -5.8f && footballPos.x < -8.8f && moveAmount == -1) { moveAmount = 0; } else if (playerPos.x < -6f && footballPos.x < -8.8f && moveAmount == -1) { moveAmount = 1; } // don't go into goal if (playerPos.x < -8.8f && playerPos.x > -9f && footballPos.x < -8.7f) { moveAmount = 0; } else if (playerPos.x < -8.9f) { moveAmount = 1; } if (AIType == defendAIType) { if (footballPos.x < 0.9f && footballVec.x > 1.5f) { moveAmount = 1; } else if (playerPos.x < 1f && playerPos.x > 0.8f && footballPos.x < 0.9f) { moveAmount = 0; } else if (playerPos.x < 0.9f) { moveAmount = 1; } } if (playerPos.x > 8.8f && playerPos.x < 9f && footballPos.x > 8.7f) { moveAmount = 0; } else if (playerPos.x > 8.9f) { moveAmount = -1; } if (AIType == GoalKeeperAIType) { if (playerPos.x > 8.75f && playerPos.x < 9f) { moveAmount = 0; } else if (playerPos.x > 8.9f) { moveAmount = -1; } else if (playerPos.x < 8.9f) { moveAmount = 1; } } if (AIType == lazyAIType && footballPos.x < playerPos.x - 4f && moveAmount == -1 && football.rigidbody.velocity.magnitude > 1) { moveAmount = 0; } if (moveAmount != 0) { moveLastTime = Time.time; } controller2D.DoMoveAmount(moveAmount); }
// Update is called once per frame void Update() { if (GameController.Instance.pannelManager.CurrentPanel != null && GameController.Instance.pannelManager.CurrentPanel.name == "BtnControllPanel") { #if (UNITY_EDITOR) if (Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)) { if (Input.mousePosition.x < Screen.width / 2f) { ray = guiCamera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 2000) && hit.collider.tag == "_ControllPanel") { fingerPos = hit.point; if (Input.GetMouseButtonDown(0)) { sliderPos = fingerPos; if (sliderPos.x < minRayX) { sliderPos = new Vector3(minRayX, sliderPos.y, 0f); } else if (sliderPos.x > maxRayX) { sliderPos = new Vector3(maxRayX, sliderPos.y, 0f); } else { sliderPos = new Vector3(sliderPos.x, sliderPos.y, 0f); } Slider.transform.position = sliderPos; Slider.SetActiveRecursively(true); SliderButton.transform.position = new Vector3(fingerPos.x, fingerPos.y, 0f); } if (Input.GetMouseButtonUp(0)) { Slider.SetActiveRecursively(false); } else { float delta = fingerPos.x - sliderPos.x; delta = Mathf.Clamp(delta, -sliderwidth / 2f, sliderwidth / 2f); SliderButton.transform.position = sliderPos + new Vector3(delta, 0f, 0f); float moveAmount = delta / (sliderwidth / 2f); moveAmount = Mathf.Clamp(moveAmount, -1f, 1f); controller2D.DoMoveAmount(moveAmount); } } else { Slider.SetActiveRecursively(false); } } else { Slider.SetActiveRecursively(false); } } #endif foreach (Touch touch in Input.touches) { if (touch.position.x < Screen.width / 2f || touch.fingerId == curFingerID) { ray = guiCamera.ScreenPointToRay(touch.position); if (Physics.Raycast(ray, out hit, 2000) && hit.collider.tag == "_ControllPanel") { fingerPos = hit.point; switch (touch.phase) { case TouchPhase.Began: if (curFingerID == -1) { curFingerID = touch.fingerId; sliderPos = fingerPos; if (sliderPos.x < minRayX) { sliderPos = new Vector3(minRayX, sliderPos.y, 0f); } else if (sliderPos.x > maxRayX) { sliderPos = new Vector3(maxRayX, sliderPos.y, 0f); } else { sliderPos = new Vector3(sliderPos.x, sliderPos.y, 0f); } Slider.transform.position = sliderPos; Slider.SetActiveRecursively(true); SliderButton.transform.position = new Vector3(fingerPos.x, fingerPos.y, 0f); } goto default; case TouchPhase.Ended: if (touch.fingerId == curFingerID) { curFingerID = -1; Slider.SetActiveRecursively(false); } break; default: if (touch.fingerId == curFingerID) { float delta = fingerPos.x - sliderPos.x; delta = Mathf.Clamp(delta, -sliderwidth / 2f, sliderwidth / 2f); SliderButton.transform.position = sliderPos + new Vector3(delta, 0f, 0f); float moveAmount = delta / (sliderwidth / 2f); moveAmount = Mathf.Clamp(moveAmount, -1f, 1f); controller2D.DoMoveAmount(moveAmount); } break; } } else { if (touch.fingerId == curFingerID) { curFingerID = -1; Slider.SetActiveRecursively(false); } } } } } /// For PC Detect if (Input.GetButton("Jump")) { controller2D.DoJump(); } if (Input.GetKey("space")) { controller2D.DoShoot(); } if (Input.GetAxis("Horizontal") != 0) { controller2D.DoMoveAmount(Input.GetAxis("Horizontal")); } if (Input.GetKey("return")) { GameController.Instance.UseSkillMe(); } /// For iOS Detect /// Cause by the UIButton }