/// <summary> /// 扫描坦克 /// </summary> private void ScanTank() { //侧边发出射线,扫描目标 mHit = Physics.Raycast(GameObject.transform.position, GameObject.transform.right, out mRaycastHit, mScanRange * mFireMissile.Size); if (mHit) { if (mRaycastHit.transform.tag == "Tank") { Debug.Log(mRaycastHit.GetType()); if (Vector3.Distance(GameObject.transform.position, mRaycastHit.transform.position) > mMinScanRange) //如果目标太近,不进行瞄准动作 { mIsScanTrue = true; //发现目标 mAimTankPos = mRaycastHit.transform.position; //保存发现目标的位置 mAimHashCode = mRaycastHit.transform.gameObject.GetHashCode(); } } } }
// Update is called once per frame void Update() { forwardDirection = this.transform.forward; if (currentCell != null) { if (Input.GetKeyDown(KeyCode.W)) { //if (currentCell.north != null) //{ //Debug.Log("Setting target cell..."); //transform.position = currentCell.north.transform.position; //targetCell = currentCell.north; moveDirection += 1; turnDirection = 0; lastAction = Action.Step; //} } if (Input.GetKeyDown(KeyCode.S)) { //if (currentCell.south != null) //{ //Debug.Log("Setting target cell..."); //transform.position = currentCell.south.transform.position; //targetCell = currentCell.south; moveDirection -= 1; turnDirection = 0; lastAction = Action.Step; //} } if (Input.GetKeyDown(KeyCode.A)) { //if (currentCell.west != null) //{ //Debug.Log("Setting target cell..."); //transform.position = currentCell.north.transform.position; //targetCell = currentCell.west; turnDirection -= 1; moveDirection = 0; lastAction = Action.Turn; //} } if (Input.GetKeyDown(KeyCode.D)) { //if (currentCell.east != null) //{ //Debug.Log("Setting target cell..."); //transform.position = currentCell.south.transform.position; //targetCell = currentCell.east; turnDirection += 1; moveDirection = 0; lastAction = Action.Turn; //} } if (Input.GetMouseButton(0)) { //lastAction = Action.Click; lastClickRay = TouchRay; Debug.Log("Attempting Raycast..."); //Ray ray = lastClickRay; lastAction = (Physics.Raycast(lastClickRay, out lastHit, reach)) ? Action.Click : Action.None; // make sure raycast is valid } if (_gameManager.gameTimer.playerActFirstFrame)// && targetCell != null) { if (lastAction == Action.Step) { finalDirection = moveDirection * forwardDirection; angle = Mathf.Atan2(finalDirection.x, finalDirection.z) * Mathf.Rad2Deg; if (finalDirection.magnitude > 0 && angle >= -45 && angle < 45 && currentCell.north != null && !(currentCell.north.Content.Type == GameEnum.GridCellContentType.Wall || currentCell.north.Content.Type == GameEnum.GridCellContentType.Item)) { targetCell = currentCell.north; } if (finalDirection.magnitude > 0 && (angle > 135 || angle < -135) && currentCell.south != null && !(currentCell.south.Content.Type == GameEnum.GridCellContentType.Wall || currentCell.south.Content.Type == GameEnum.GridCellContentType.Item)) { targetCell = currentCell.south; } if (finalDirection.magnitude > 0 && (angle > 45 && angle < 135) && currentCell.east != null && !(currentCell.east.Content.Type == GameEnum.GridCellContentType.Wall || currentCell.east.Content.Type == GameEnum.GridCellContentType.Item)) { targetCell = currentCell.east; } if (finalDirection.magnitude > 0 && (angle > -135 && angle < -45) && currentCell.west != null && !(currentCell.west.Content.Type == GameEnum.GridCellContentType.Wall || currentCell.west.Content.Type == GameEnum.GridCellContentType.Item)) { targetCell = currentCell.west; } if (targetCell != null) { Debug.Log("!!!!Setting Current Cell!!!!"); CurrentCell = targetCell; targetCell = null; progressThruMoveAnimation = 0; animatingMove = true; } } else if (lastAction == Action.Turn) { if (Mathf.Abs(turnDirection) > 0) { if (turnDirection > 1) { turnDirection = 1; } if (turnDirection < -1) { turnDirection = -1; } Debug.Log("!!!!SETTING TURN!!!!"); targetDirection = Quaternion.AngleAxis(turnDirection * 90, Vector3.up) * forwardDirection; lastRotation = this.transform.rotation; targetRotation = Quaternion.AngleAxis(turnDirection * 90, Vector3.up) * this.transform.rotation; // SHOULD ACTUALLY ALIGN TO A CARDINAL DIRECTION (declare four quaternions at top) progressThruTurnAnimation = 0; animatingTurn = true; } } else if (lastAction == Action.Click) { // assumes valid raycast Debug.Log("Hit!! " + lastHit.GetType()); GameObject thing = lastHit.collider.gameObject; if (thing.transform.parent != null) { GridCellContent content = thing.GetComponentInParent <GridCellContent>(); if (content != null) { if (content.Type == GameEnum.GridCellContentType.Item) { content.MyCell.Content = content.OriginFactory.Get(GameEnum.GridCellContentType.Empty); //Debug.Log("Number left: " + content.Recycle()); } } } //Transform objectHit = hit.transform; // Do something with the object that was hit by the raycast. } moveDirection = 0; turnDirection = 0; //lastHit = null; //lastClickRay = null; lastAction = Action.None; } } if (animatingMove) { //this.transform.position += new Vector3(0,Mathf.Sin(progressThruMoveAnimation / _gameManager.gameTimer.executionTime)); progressThruMoveAnimation += Time.deltaTime; if (progressThruMoveAnimation / _gameManager.gameTimer.executionTime > 1) { progressThruMoveAnimation = _gameManager.gameTimer.executionTime; animatingMove = false; } Vector3 vel = new Vector3(); this.transform.position = Vector3.Lerp(lastCell.transform.position, currentCell.transform.position, progressThruMoveAnimation / _gameManager.gameTimer.executionTime); this.transform.position += new Vector3(0, .05f * Mathf.Cos(stepMultiplier * Mathf.PI * progressThruMoveAnimation / _gameManager.gameTimer.executionTime) * Mathf.Sin(2 * Mathf.PI * progressThruMoveAnimation / _gameManager.gameTimer.executionTime), 0); debugValue = progressThruMoveAnimation / _gameManager.gameTimer.executionTime; } if (animatingTurn) { progressThruTurnAnimation += Time.deltaTime; if (progressThruTurnAnimation > 1) { progressThruTurnAnimation = 1; animatingTurn = false; } this.transform.rotation = Quaternion.Slerp(lastRotation, targetRotation, progressThruTurnAnimation / _gameManager.gameTimer.executionTime); } }