// Update is called once per frame void Update() { //input stuff if (Input.GetKeyDown(KeyCode.T)) { launch = true; } if (Input.GetKey(KeyCode.Y)) { launch = true; } if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { transform.Rotate(new Vector3(0f, 0f, moveSpeed)); } if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { transform.Rotate(new Vector3(0f, 0f, -moveSpeed)); } if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) { force += moveSpeed / 10f; } if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) { force -= moveSpeed / 10f; } force = Mathf.Clamp(force, 1f, 20f); if (launch) { launch = false; Launch(); } //set line duration to delta time so that it only lasts the length of a frame tp.debugLineDuration = Time.unscaledDeltaTime; //tell the predictor to predict a 2d line. this will also cause it to draw a prediction line //because drawDebugOnPredict is set to true tp.Predict2D(launchPoint.position, launchPoint.right * force, Physics2D.gravity); //this static method can be used as well to get line info without needing to have a component and such //TrajectoryPredictor.GetPoints2D(launchPoint.position, launchPoint.right * force, Physics2D.gravity); //info text stuff if (infoText) { //this will check if the predictor has a hitinfo and then if it does will update the onscreen text //to say the name of the object the line hit; if (tp.hitInfo2D) { infoText.text = "Hit Object: " + tp.hitInfo2D.collider.gameObject.name; } } }
///<summary> ///Given these values, get a RaycastHit2D of where the trajectory collides with an object, /// without needing to create an instance of the class or use it as a component. ///</summary> public static RaycastHit2D GetHitInfo2D(Rigidbody2D rb, float accuracy = 0.985f, int iterationLimit = 150, bool stopOnCollision = true) { GameObject tObj = new GameObject(); tObj.name = "TrajectoryPredictionObj"; TrajectoryPredictor pt = tObj.AddComponent <TrajectoryPredictor>(); pt.accuracy = accuracy; pt.iterationLimit = iterationLimit; pt.stopOnCollision = stopOnCollision; pt.predictionType = predictionMode.Prediction2D; pt.Predict2D(rb); Destroy(tObj); return(pt.hitInfo2D); }
///<summary> ///Given these values, get an array of points representing the trajectory in 2D without needing to create an instance of the class or use it as a component. ///</summary> public static Vector3[] GetPoints2D(Rigidbody2D rb, float accuracy = 0.985f, int iterationLimit = 150, bool stopOnCollision = true, int rayCastMask = -1) { GameObject tObj = new GameObject(); tObj.name = "TrajectoryPredictionObj"; TrajectoryPredictor pt = tObj.AddComponent <TrajectoryPredictor>(); pt.raycastMask = rayCastMask; pt.accuracy = accuracy; pt.iterationLimit = iterationLimit; pt.checkForCollision = stopOnCollision; pt.predictionType = predictionMode.Prediction2D; pt.Predict2D(rb); Destroy(tObj); return(pt.predictionPoints.ToArray()); }
///<summary> ///Given these values, get a RaycastHit2D of where the trajectory collides with an object, /// without needing to create an instance of the class or use it as a component. ///</summary> public static RaycastHit2D GetHitInfo2D(Vector3 startPos, Vector2 velocity, Vector2 gravity, float linearDrag = 0f, float accuracy = 0.985f, int iterationLimit = 150, bool stopOnCollision = true) { GameObject tObj = new GameObject(); tObj.name = "TrajectoryPredictionObj"; TrajectoryPredictor pt = tObj.AddComponent <TrajectoryPredictor>(); pt.accuracy = accuracy; pt.iterationLimit = iterationLimit; pt.stopOnCollision = stopOnCollision; pt.predictionType = predictionMode.Prediction2D; pt.Predict2D(startPos, velocity, gravity, linearDrag); Destroy(tObj); return(pt.hitInfo2D); }
///<summary> ///Given these values, get an array of points representing the trajectory in 2D without needing to create an instance of the class or use it as a component. ///</summary> public static Vector3[] GetPoints2D(Vector3 startPos, Vector2 velocity, Vector2 gravity, float linearDrag = 0f, float accuracy = 0.985f, int iterationLimit = 150, bool stopOnCollision = true, int rayCastMask = -1) { GameObject tObj = new GameObject(); tObj.name = "TrajectoryPredictionObj"; TrajectoryPredictor pt = tObj.AddComponent <TrajectoryPredictor>(); pt.raycastMask = rayCastMask; pt.accuracy = accuracy; pt.iterationLimit = iterationLimit; pt.checkForCollision = stopOnCollision; pt.predictionType = predictionMode.Prediction2D; pt.Predict2D(startPos, velocity, gravity, linearDrag); Destroy(tObj); return(pt.predictionPoints.ToArray()); }