SHOW:
|
|
- or go back to the newest paste.
1 | function CalculateNewPosition(speed : float, turnSpeedDegrees : float, time : float) { | |
2 | - | var turnSpeedRadians = turnSpeedDegrees * Mathf.Deg2Rad; // Convert the turn speed to radians |
2 | + | var turnSpeedRadians = turnSpeedDegrees * Mathf.Deg2Rad; |
3 | - | var travelDistance = speed * time; // Get the distance given the time value passed to us |
3 | + | var travelDistance = speed * time; |
4 | - | var arcRadians = Mathf.Abs(turnSpeedRadians) * time; // Get the radians turned given the time value passed to us |
4 | + | var arcRadians = Mathf.Abs(turnSpeedRadians) * time; |
5 | - | var radius = travelDistance / arcRadians; // Discover the radius of the circle we are rotating about. |
5 | + | var radius = travelDistance / arcRadians; |
6 | - | // Start by assuming the vehicle is at the origin |
6 | + | // Start by assuming the vehicle is at the origin |
7 | - | var circleCenter : Vector3 = Vector3.zero; |
7 | + | var circleCenter : Vector3 = Vector3.zero; |
8 | - | var newPos : Vector3 = circleCenter; |
8 | + | var newPos : Vector3 = circleCenter; |
9 | - | if(turnSpeedDegrees > 0) { // Turning left |
9 | + | if(turnSpeedDegrees > 0) { // Turning left |
10 | - | circleCenter.x -= radius; |
10 | + | circleCenter.x -= radius; |
11 | - | newPos.x = circleCenter.x + radius * Mathf.Cos(arcRadians); |
11 | + | newPos.x = circleCenter.x + radius * Mathf.Cos(arcRadians); |
12 | - | } else { // Turning right |
12 | + | } else { // Turning right |
13 | - | circleCenter.x += radius; |
13 | + | circleCenter.x += radius; |
14 | - | newPos.x = circleCenter.x - radius * Mathf.Cos(arcRadians); |
14 | + | newPos.x = circleCenter.x - radius * Mathf.Cos(arcRadians); |
15 | - | } |
15 | + | } |
16 | - | newPos.z = radius * Mathf.Sin(arcRadians); |
16 | + | newPos.z = radius * Mathf.Sin(arcRadians); |
17 | ||
18 | - | //finally, transform it relative to the vehicle (might mess up if your vehicle |
18 | + | //finally, transform it relative to the vehicle (might mess up if your vehicle |
19 | - | //isn't at a 1x1x1 scale) |
19 | + | //isn't at a 1x1x1 scale) |
20 | - | this.transform.position = transform.TransformPoint(newPos); |
20 | + | this.transform.position = transform.TransformPoint(newPos); |
21 | - | // And rotate the vehicle along the curve |
21 | + | // And rotate the vehicle along the curve |
22 | this.transform.rotation = this.transform.rotation * Quaternion.Euler(0, Mathf.Lerp(0, -turnSpeedDegrees, time), 0); | |
23 | } |