public IEnumerator AIShoot() { if (Time.time - shootLastTime < shootInterval) { yield break; } bool doShoot = false; //can kick ball now. if (footballPos.x < playerPos.x && footballPos.x > playerPos.x - 2f && footballPos.y < playerPos.y + 1.3f && footballPos.y > playerPos.y - 0.3f) { doShoot = true; } //can kick ball a little time later. if (newFootballPos.x < playerPos.x && newFootballPos.x > playerPos.x - 2f && newFootballPos.y < playerPos.y + 1.3f && newFootballPos.y > playerPos.y - 0.3f) { doShoot = true; } if (doShoot == true) { shootLastTime = Time.time; yield return(new WaitForSeconds(delay)); controller2D.DoShoot(); } }
public void AIShoot() { if (Time.time - shootLastTime < shootInterval) { return; } bool doShoot = false; if (doShoot == true) { Debug.Log("AIShoot"); controller2D.DoShoot(); shootLastTime = Time.time; } return; }
// 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 }