Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function CalculateNewPosition(speed : float, turnSpeedDegrees : float, time : float) {
- var turnSpeedRadians = turnSpeedDegrees * Mathf.Deg2Rad;
- var travelDistance = speed * time;
- var arcRadians = Mathf.Abs(turnSpeedRadians) * time;
- var radius = travelDistance / arcRadians;
- // Start by assuming the vehicle is at the origin
- var circleCenter : Vector3 = Vector3.zero;
- var newPos : Vector3 = circleCenter;
- if(turnSpeedDegrees > 0) { // Turning left
- circleCenter.x -= radius;
- newPos.x = circleCenter.x + radius * Mathf.Cos(arcRadians);
- } else { // Turning right
- circleCenter.x += radius;
- newPos.x = circleCenter.x - radius * Mathf.Cos(arcRadians);
- }
- newPos.z = radius * Mathf.Sin(arcRadians);
- //finally, transform it relative to the vehicle (might mess up if your vehicle
- //isn't at a 1x1x1 scale)
- this.transform.position = transform.TransformPoint(newPos);
- // And rotate the vehicle along the curve
- this.transform.rotation = this.transform.rotation * Quaternion.Euler(0, Mathf.Lerp(0, -turnSpeedDegrees, time), 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement