Advertisement
Guest User

Untitled

a guest
Apr 5th, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function CalculateNewPosition(speed : float, turnSpeedDegrees : float, time : float) {
  2.     var turnSpeedRadians = turnSpeedDegrees * Mathf.Deg2Rad;
  3.     var travelDistance = speed * time;
  4.     var arcRadians = Mathf.Abs(turnSpeedRadians) * time;
  5.     var radius = travelDistance / arcRadians;
  6.     // Start by assuming the vehicle is at the origin
  7.     var circleCenter : Vector3 = Vector3.zero;
  8.     var newPos : Vector3 = circleCenter;
  9.     if(turnSpeedDegrees > 0) {      // Turning left
  10.         circleCenter.x -= radius;
  11.         newPos.x = circleCenter.x + radius * Mathf.Cos(arcRadians);
  12.     } else {                        // Turning right
  13.         circleCenter.x += radius;
  14.         newPos.x = circleCenter.x - radius * Mathf.Cos(arcRadians);
  15.     }
  16.     newPos.z = radius * Mathf.Sin(arcRadians);
  17.  
  18.     //finally, transform it relative to the vehicle (might mess up if your vehicle
  19.     //isn't at a 1x1x1 scale)
  20.     this.transform.position = transform.TransformPoint(newPos);
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement