今天给大家介绍下关于unity smoothfollow,下面我会放出详细的代码内容供大家参考,学习。


  1. // The target we are following var target : Transform; 
  2. // The distance in the x-z plane to the target var distance = 5.0; 
  3. // the height we want the camera to be above the target var height = 5.0; 
  4. // How much we  
  5. var heightDamping = 2.0; var rotationDamping = 3.0;  
  6. // Place the script in the Camera-Control group in the component menu @script AddComponentMenu("Camera-Control/Smooth Follow")   
  7. function LateUpdate () 
  8. {  
  9. // Early out if we don't have a target  if (!target)   return;   
  10.  // Calculate the current rotation angles  wantedRotationAngle = target.eulerAngles.y;  
  11. wantedHeight = target.position.y + height-0.1;  
  12. currentRotationAngle = transform.eulerAngles.y; 
  13.  currentHeight = transform.position.y;   
  14.  // Damp the rotation around the y-axis  currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); 
  15.   // Damp the height  currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
  16.   // Convert the angle into a rotation 
  17.  currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);   
  18.  // Set the position of the camera on the x-z plane to: 
  19.  // distance meters behind the target  transform.position = target.position; 
  20.  transform.position -= currentRotation * Vector3.forward * distance;  
  21.  // Set the height of the camera  transform.position.y = currentHeight;   
  22.  // Always look at the target  transform.LookAt (target); 
  23. }