public void SimulateFlight(CannonScript theCannon) { Vector3 startPos = new Vector3(0, 0, 0); Vector3 endPos = new Vector3(0, 0, 0); Vector3 moveDelta = new Vector3(0, 0, 0); Vector3 simVelocity = new Vector3(0, 0, 0); Vector3 hitSurfaceNormal = new Vector3(0, 1, 0); const float deltaTime = 1.0f / 10.0f; bool hitTarget = false; bool hitGround = false; int sanity = 0; if (theReticule == null) { return; } if (theCannon != null) { theCannon.GetSpawnBallPosition(out startPos); GetLaunchVelocity(theCannon.localElevationAngle, theCannon.localRotationAngle, out simVelocity); while (!hitGround && !hitTarget) { DoMotion(startPos, deltaTime, ref simVelocity, out moveDelta); DoCollisionDetection(startPos, moveDelta, out endPos, out hitSurfaceNormal, out hitTarget, out hitGround); startPos = endPos; } if (hitTarget) { theReticule.renderer.material = greenReticuleMaterial; } else { theReticule.renderer.material = redReticuleMaterial; } print("hitTarget - " + hitTarget + " endPos = " + endPos); //where should the reticule go, and what direction should it face? theReticule.newPos = endPos; theReticule.newOrientationInEulerAngles = Vector3.zero; float angleFromVertical = Vector3.Angle(hitSurfaceNormal, Vector3.up); if ((angleFromVertical > 1.0f) && (angleFromVertical < 45.0f)) { // bit of a hack... I know the orientation of the hill, // so if the reticule is hitting anything other than flat ground, // set it to the orientation of the hill theReticule.newOrientationInEulerAngles = theGameManager.theHill.transform.eulerAngles; } } }
public void SimulateFlight(CannonScript theCannon) { Vector3 startPos = new Vector3(0,0,0); Vector3 endPos = new Vector3(0,0,0); Vector3 moveDelta = new Vector3(0,0,0); Vector3 simVelocity = new Vector3(0,0,0); Vector3 hitSurfaceNormal = new Vector3(0,1,0); const float deltaTime = 1.0f / 10.0f; bool hitTarget = false; bool hitGround = false; int sanity = 0; if (theReticule == null) { return; } if (theCannon != null) { theCannon.GetSpawnBallPosition(out startPos); GetLaunchVelocity(theCannon.localElevationAngle, theCannon.localRotationAngle, out simVelocity); while(!hitGround && !hitTarget) { DoMotion(startPos, deltaTime, ref simVelocity, out moveDelta); DoCollisionDetection(startPos, moveDelta, out endPos, out hitSurfaceNormal, out hitTarget, out hitGround); startPos = endPos; } if (hitTarget) { theReticule.renderer.material = greenReticuleMaterial; } else { theReticule.renderer.material = redReticuleMaterial; } print("hitTarget - " + hitTarget + " endPos = " + endPos); //where should the reticule go, and what direction should it face? theReticule.newPos = endPos; theReticule.newOrientationInEulerAngles = Vector3.zero; float angleFromVertical = Vector3.Angle(hitSurfaceNormal, Vector3.up); if ((angleFromVertical > 1.0f) && (angleFromVertical < 45.0f)) { // bit of a hack... I know the orientation of the hill, // so if the reticule is hitting anything other than flat ground, // set it to the orientation of the hill theReticule.newOrientationInEulerAngles = theGameManager.theHill.transform.eulerAngles; } } }
// Update is called once per frame void Update() { if (theBall == null) { //hint: if your game UI never displays, you might want to check this condition somehow... return; } switch (theGameState) { case GameStates.GS_INITIALIZATION: //calculate information about the hill HILL_HALF_WIDTH = HILL_UNSCALED_HALF_WIDTH * theHill.transform.localScale.x; Restart(); //needed to avoid a race condition with the ball's Start() function break; case GameStates.GS_USER_INPUT: //increase or decreate the cannon's elevation by a rate of 10 degrees per second if (Input.GetKey(KeyCode.UpArrow)) { theCannon.localElevationAngle = Mathf.Min(90.0f, theCannon.localElevationAngle + 10.0f * Time.deltaTime); } if (Input.GetKey(KeyCode.DownArrow)) { theCannon.localElevationAngle = Mathf.Max(0.0f, theCannon.localElevationAngle - 10.0f * Time.deltaTime); } //increase or decreate the cannon's angle by a rate of 10 degrees per second if (Input.GetKey(KeyCode.RightArrow)) { theCannon.localRotationAngle = Mathf.Min(60.0f, theCannon.localRotationAngle + 10.0f * Time.deltaTime); } if (Input.GetKey(KeyCode.LeftArrow)) { theCannon.localRotationAngle = Mathf.Max(-60.0f, theCannon.localRotationAngle - 10.0f * Time.deltaTime); } theBall.SimulateFlight(theCannon); //check for game start if (Input.GetKeyDown(KeyCode.Space)) { Vector3 spawnPos = new Vector3(0, 0, 0); theCannon.GetSpawnBallPosition(out spawnPos); theBall.ApplyMovement(spawnPos); theBall.LaunchBall(theCannon.localElevationAngle, theCannon.localRotationAngle); theGameState = GameStates.GS_GAME_IN_PROGRESS; } break; case GameStates.GS_GAME_IN_PROGRESS: break; case GameStates.GS_ENDGAME_VICTORY: //check for new game if (Input.GetKeyDown(KeyCode.Space)) { Restart(); } break; case GameStates.GS_ENDGAME_DEFEAT: //check for new game if (Input.GetKeyDown(KeyCode.Space)) { Restart(); } break; } }