// Update is called once per frame void Update() { tempReloadTime -= 10.0f * Time.deltaTime; if (tempReloadTime < 0.0f) { // Get all human players GameObject[] players = GameObject.FindGameObjectsWithTag("Player"); //RAINAgent ai = gameObject.GetComponent<RAINAgent>(); AIRig ai = gameObject.transform.Find("AI").GetComponent <AIRig>(); foreach (GameObject player in players) { var targetDir = player.transform.position - _transform.position; var forward = _transform.forward; var angle = Vector3.Angle(targetDir, forward); // Get ammo count from AI // int ammo = ai.Agent.actionContext.GetContextItem<int>("ammo"); int ammo = ai.AI.WorkingMemory.GetItem <int>("ammo"); if (angle < enemyShootAngle && ammo > 0) { GameObject bullet = PhotonNetwork.Instantiate("BomfabEnemy", _transform.position + (spawnDistanceForward * _transform.forward) + (spawnDistanceUp * _transform.up), _transform.rotation, 0); BulletAi controller = bullet.GetComponent <BulletAi>(); controller.enabled = true; tempReloadTime = reloadTime; // Debug.Log( "AI ammo " + ammo); // Set ammo count to AI // ai.Agent.actionContext.SetContextItem<int>("ammo", ammo-1); ai.AI.WorkingMemory.SetItem <int>("ammo", ammo - 1); } } } }
// fixed timing, Physics uses this instead of normal update for more consistand simulation // TODO: check if Time.deltaTime isn't always the same in Fixed update. void FixedUpdate() { // Cool downs for Player weapon tempReloadTime -= 10.0f * Time.deltaTime; float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); // Accelerate forward or Backwards if (verticalInput > 0.0) // Forwards { if (forwardSpeed < maxForwardSpeed) { forwardSpeed += 10.0f; } } if (verticalInput < 0.0) // Backwards { if (forwardSpeed > maxBackwardSpeed) { forwardSpeed -= 10.0f; } } if (verticalInput == 0) // come to a halt { if (forwardSpeed > 0) { forwardSpeed -= 10.0f; } else if (forwardSpeed < 0) { forwardSpeed += 10.0f; } } Vector3 moveDirection = new Vector3(0, 0, forwardSpeed); if (_transform.rotation.x < 0.05f && _transform.rotation.x > -0.05f && _transform.rotation.z < 0.05f && _transform.rotation.z > -0.05f) { _rigidbody.AddRelativeForce(moveDirection, ForceMode.Acceleration); } // Rotate left of right Vector3 a = _transform.eulerAngles; float rotation = 0; if (horizontalInput > 0) { rotation = a.y + ((forwardSpeed / 2.0f) * Time.deltaTime); _transform.eulerAngles = new Vector3(a.x, rotation, a.z); } else if (horizontalInput < 0) { rotation = a.y - ((forwardSpeed / 1.5f) * Time.deltaTime); _transform.eulerAngles = new Vector3(a.x, rotation, a.z); } if (Application.platform == RuntimePlatform.MetroPlayerX64 || Application.platform == RuntimePlatform.MetroPlayerX86 || Application.platform == RuntimePlatform.MetroPlayerARM || Application.platform == RuntimePlatform.WP8Player || Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) { Rect joystickRect = new Rect(0, 0, Screen.width / 3, Screen.height / 2); Rect joystickRectMenu = new Rect(0, Screen.height / 2, Screen.width / 3, Screen.height * 1.0f); Rect joystickRectRight = new Rect(Screen.width - (Screen.width / 3), 0, Screen.width / 3, Screen.height / 2); Rect joystickRectRightFire = new Rect(Screen.width - (Screen.width / 3), Screen.height / 2, Screen.width / 3, Screen.height / 8); Rect joystickRectRightBrake = new Rect(Screen.width - (Screen.width / 3), Screen.height / 1.61f, Screen.width / 3, Screen.height / 8); // do joystick stuff int count = Input.touchCount; guiVerticalInput = 0.0f; // check all fingers for (var i = 0; i < count; i++) { Touch touch = Input.GetTouch(i); // if joystick finger presses the back button if (joystickRectMenu.Contains(touch.position)) { if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) { Application.LoadLevel("Menu"); } } // How ON earth does this touch zone start in the top right corner ???? if (joystickRect.Contains(touch.position)) { // and finger has moved if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) { if (touch.position.x < Screen.width / 3) { float turnAmount = touch.position.x - ((Screen.width / 3) / 2); guiRotation = a.y + ((turnAmount / 2) * Time.deltaTime); } } } else if (joystickRectRight.Contains(touch.position)) { if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) { guiVerticalInput = 1.0f; } } else if (joystickRectRightBrake.Contains(touch.position)) { if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) { guiVerticalInput = -1.0f; } } else if (joystickRectRightFire.Contains(touch.position)) { if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) { if (tempReloadTime < 0.0f) { GameObject bullet = PhotonNetwork.Instantiate("Bomfab", _transform.position + (spawnDistanceForward * _transform.forward) + (spawnDistanceUp * _transform.up), _transform.rotation, 0); BulletAi controller = bullet.GetComponent <BulletAi>(); controller.enabled = true; tempReloadTime = reloadTime; } } } } Vector3 guiMoveDirection = new Vector3(0, 0, guiVerticalInput * forwardSpeed); if (guiVerticalInput > 0.1) { if (_transform.rotation.x < 0.05f && _transform.rotation.x > -0.05f && _transform.rotation.z < 0.05f && _transform.rotation.z > -0.05f) { _rigidbody.AddRelativeForce(guiMoveDirection, ForceMode.Acceleration); } } // this works but is not the right way to do it. _transform.eulerAngles = new Vector3(a.x, guiRotation, a.z); } }