//This is a simple obstacle avoidance function I am working on. Its still in development //and not currently being used. public void AvoidObstactle() { selectedTurn = UnityEngine.Random.Range(0, 2); turnTimer = 0; GameObject prefabSpline = turnList [selectedTurn]; currentSpline = GameObject.Instantiate(prefabSpline, transform.position, transform.rotation); followerScript = this.gameObject.AddComponent <Dreamteck.Splines.SplineFollower> (); followerScript.enabled = true; followerScript.followSpeed = rBody.velocity.magnitude; followerScript.computer = currentSpline.GetComponent <Dreamteck.Splines.SplineComputer> (); splineLength = currentSpline.GetComponent <Dreamteck.Splines.SplineComputer> ().CalculateLength(); turnTime = splineLength / rBody.velocity.magnitude; turnWatch = 0; isdoingManeuver = true; ChangeState(SHIPAISTATES.TURN); }
void OutRunState() { //This state is very similiar to the moves state. The only difference is that //this state includes a maneuver section. In this state, the ship can do fancy //flying manuevers to try to escape the enemy. The manuever section behaves //just like the turn section, it just uses a different list and different timer varaibles. turnTimer += Time.deltaTime; maneuverTimer += Time.deltaTime; if (turnTimer >= currentTurnTime) { selectedTurn = UnityEngine.Random.Range(0, turnList.Count); turnTimer = 0; GameObject prefabSpline = turnList [selectedTurn]; currentSpline = GameObject.Instantiate(prefabSpline, transform.position, transform.rotation); followerScript = this.gameObject.AddComponent <Dreamteck.Splines.SplineFollower> (); followerScript.enabled = true; followerScript.followSpeed = rBody.velocity.magnitude; followerScript.computer = currentSpline.GetComponent <Dreamteck.Splines.SplineComputer> (); splineLength = currentSpline.GetComponent <Dreamteck.Splines.SplineComputer> ().CalculateLength(); //print (splineLength); turnTime = splineLength / rBody.velocity.magnitude; isdoingManeuver = true; CheckForCallForHelp(); ChangeState(SHIPAISTATES.TURN); } if (maneuverTimer >= currentManeuverTime) { selectedManuever = UnityEngine.Random.Range(0, maneuverList.Count); turnTimer = 0; maneuverTimer = 0; GameObject prefabSpline = maneuverList [selectedManuever]; currentSpline = GameObject.Instantiate(prefabSpline, transform.position, transform.rotation); followerScript = this.gameObject.AddComponent <Dreamteck.Splines.SplineFollower> (); followerScript.enabled = true; followerScript.followSpeed = rBody.velocity.magnitude; followerScript.computer = currentSpline.GetComponent <Dreamteck.Splines.SplineComputer> (); splineLength = currentSpline.GetComponent <Dreamteck.Splines.SplineComputer> ().CalculateLength(); turnTime = splineLength / rBody.velocity.magnitude; isdoingManeuver = true; ChangeState(SHIPAISTATES.TURN); } }
//Handles the straigh forward movement of the ship void MoveState() { //Keeps track of time until next turn turnTimer += Time.deltaTime; //The turns and manuevers that the ships preform all use splines. I purchased a spline //pack from the Unity Asset Store. For the ships to move on the splines, they require a component //from the package to be attached to the ship. These components can only be associated with spline however. //So each time the ship completes a spline,the old component has to be romved and a new one added. //This removes the old component. if (followerScript != null) { Destroy(this.gameObject.GetComponent <Dreamteck.Splines.SplineFollower> ()); } //When it is time to turn... if (turnTimer >= currentTurnTime) { //Randomly select turn from list of turns selectedTurn = UnityEngine.Random.Range(0, turnList.Count); //reset turn timer for next time. turnTimer = 0; //Spawn selected turn spline GameObject prefabSpline = turnList [selectedTurn]; currentSpline = GameObject.Instantiate(prefabSpline, transform.position, transform.rotation); //Adds new component that allows the ship to follow the spline followerScript = this.gameObject.AddComponent <Dreamteck.Splines.SplineFollower> (); followerScript.enabled = true; //Sets speed at which to follow the spline based on the current speed of the ship followerScript.followSpeed = rBody.velocity.magnitude; //Concects the spline follow component and the spline followerScript.computer = currentSpline.GetComponent <Dreamteck.Splines.SplineComputer> (); //Determine the distance the ship will travel while following the spline splineLength = currentSpline.GetComponent <Dreamteck.Splines.SplineComputer> ().CalculateLength(); //Finds the length of time it will take the ship to reach the end of the spline, by taking the //lenght of the time and dividing it by the ships speed. Time = distance/speed turnTime = splineLength / rBody.velocity.magnitude; //The "turnWatch" is basically a stop watch that keeps track of how long the ship has been on the spline turnWatch = 0; isdoingManeuver = true; ChangeState(SHIPAISTATES.TURN); } }