当前位置:   article > 正文

Unity镜头特写效果

Unity镜头特写效果

改自:http://blog.csdn.net/fzhlee/article/details/8667251

  1. public GameObject[] obj;//观察对象集合
  2. private int num=0;
  3. // public Transform target;
  4. public float distance = 3.0f;//摄像机正对物体的距离
  5. public float height = 1.0f;//摄像机正对物体的高度
  6. public float damping = 5.0f;//摄像机位移速度
  7. private bool smppthRotation = true;//是否平滑转动角度
  8. public float rotationDamping = 10.0f;//摄像机角度转动的速度
  9. public float x_=0f;//摄像机距离物体x轴的距离
  10. private Vector3 targetLookAtOffset;//
  11. public float bumperDistanceCheck = 2.5f;
  12. public float bumperCameraHeight = 1.0f;
  13. private Vector3 bumperRayOffset;
  14. // Update is called once per frame
  15. void Update () {
  16. lookatobj(obj[num].transform);
  17. }
  18. void lookatobj(Transform target)
  19. {
  20. Vector3 wantedPosition = target.TransformPoint(x_, height, -distance);
  21. RaycastHit hit;
  22. //若摄像机和物体之间有障碍物 则将摄像机拉到障碍物之前
  23. //Vector3 back = target.transform.TransformDirection(-1 * Vector3.forward);
  24. //if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, out hit, bumperDistanceCheck))
  25. //{
  26. // // clamp wanted position to hit position
  27. // wantedPosition.x = hit.point.x;
  28. // wantedPosition.z = hit.point.z;
  29. // // wantedPosition.y = Mathf.Lerp(wantedPosition.y, hit.point.y + bumperCameraHeight, Time.deltaTime * damping);
  30. //}
  31. transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);//摄像机位移
  32. Vector3 lookPosition = target.position;//target.TransformPoint(targetLookAtOffset);
  33. Quaternion wantedRotation = Quaternion.LookRotation(lookPosition - transform.position, target.up);
  34. if (smppthRotation)
  35. {
  36. //平滑转动摄像机
  37. transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
  38. }
  39. else transform.rotation = wantedRotation;
  40. //将障碍物隐藏
  41. if (Vector3.Distance(transform.position, wantedPosition) <=2f)
  42. {
  43. Vector3 dir=transform.TransformDirection(Vector3.forward);
  44. if (Physics.Raycast(transform.position, dir, out hit))
  45. {
  46. if (hit.collider.gameObject.name != target.name)
  47. {
  48. hit.collider.gameObject.SetActive(false);
  49. }
  50. }
  51. }
  52. if (transform.position == wantedPosition && transform.rotation == wantedRotation)//循环观察
  53. {
  54. num++;
  55. num %= obj.Length;
  56. obj[num].SetActive(true);//显示对象
  57. }
  58. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/636952
推荐阅读
相关标签
  

闽ICP备14008679号