Unity3d学习记录(一)摄像机跟随人物移动,并且注视人物

来源:互联网 发布:vscode git插件 编辑:程序博客网 时间:2024/05/29 14:28
[csharp] view plaincopy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class camera : MonoBehaviour {  
  5.   
  6. // 设定绑定目标  
  7. public Transform target;  
  8. // 设置隔离目标的距离  
  9. float distance = 10.0f;  
  10. // 设置隔离目标的高度  
  11. float height = 5.0f;  
  12. //转动的速度  
  13. float heightDamping = 2.0f;  
  14. float rotationDamping = 3.0f;  
  15.   
  16. void LateUpdate () {  
  17. // Early out if we don't have a target  
  18. if (!target)  
  19. return;  
  20.   
  21. // 想要旋转的角度和高度  
  22. //float wantedRotationAngle = target.eulerAngles.y;  
  23. float wantedHeight = target.position.y + height;  
  24.   
  25.   
  26. //当前的高度和欧拉角  
  27. //float currentRotationAngle = transform.eulerAngles.y;  
  28. float currentHeight = transform.position.y;  
  29.   
  30. //从当前的欧拉角旋转到  
  31. //currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);  
  32.   
  33. //从当前的高度到想到的高度  
  34. currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);  
  35.   
  36. //这里是我修改的,直接让它等于1,  
  37. //摄像机就不会旋转。  
  38. float currentRotation = 1;  
  39.   
  40. // 设置于目标的Y轴的距离  
  41. transform.position = target.position;//先让目标的位置和摄像机的位置一致  
  42. transform.position -= currentRotation * Vector3.forward * distance;//改变摄像机的Z轴  
  43.   
  44. // 设置摄像机的位置  
  45. transform.position = new Vector3( transform.position.x, currentHeight, transform.position.z);  
  46.   
  47. //摄像机总是注视目标  
  48. transform.LookAt (target);  
  49. }  
  50. }  
0 0
原创粉丝点击