当前位置:   article > 正文

[Unity3D] 视角旋转学习笔记_unity 相机环绕

unity 相机环绕

目录

前言

一、基础旋转

1.1  相机环绕角色旋转

Declaration

Description

1.2  LookAt()函数

二、通过鼠标拖拽距离来旋转视角


前言

学习笔记,仅供学习,不做商用,如有侵权,联系我删除即可。

参考博文:

Unity 自由视角的惯性旋转icon-default.png?t=N3I4https://haiyue.blog.csdn.net/article/details/123297435

一、基础旋转

1.1  相机环绕角色旋转

使用Transform.RotateAround,axis为轴,方向旋转

Declaration

public void RotateAround(Vector3 point, Vector3 axis, float angle);

Description

Rotates the transform about axis passing through point in world coordinates by angle degrees.

This modifies both the position and the rotation of the transform.

  1. using UnityEngine;
  2. //Attach this script to a GameObject to rotate around the target position.
  3. public class Example : MonoBehaviour
  4. {
  5. //Assign a GameObject in the Inspector to rotate around
  6. public GameObject target;
  7. void Update()
  8. {
  9. // Spin the object around the target at 20 degrees/second.
  10. transform.RotateAround(target.transform.position, Vector3.up, 20 * Time.deltaTime);
  11. }
  12. }

1.2  LookAt()函数

根据官方的文档描述,该函数的功能是,旋转自身,使得当前对象的正z轴指向目标对象target所在的位置。

而对于worldUp的描述是,在完成上面的旋转之后,继续旋转自身,使得当前对象的正y轴朝向与worldUp所指向的朝向一致.

可以使用LookAt函数修正,让相机正确地朝向角色

二、通过鼠标拖拽距离来旋转视角

按下鼠标右键时,每一帧都获取鼠标的位置,通过两帧之间的鼠标位置之差,即可求得两帧之间的鼠标速度。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class PlayerTransform : MonoBehaviour
  5. {
  6. public Transform HeadTransform;
  7. private float CamDepthSmooth = 200f;
  8. private Vector3 RelativePos;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. RelativePos = transform.position - HeadTransform.position;
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. transform.position = HeadTransform.position + RelativePos;
  18. DragToRotateView_Distance();
  19. // 视角的拉近和放远
  20. if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) ||
  21. (Input.mouseScrollDelta.y > 0) && Camera.main.fieldOfView <= 80)
  22. {
  23. Camera.main.fieldOfView += Input.mouseScrollDelta.y * CamDepthSmooth * Time.deltaTime;
  24. }
  25. }
  26. /* 通过鼠标拖拽距离来旋转视角 */
  27. Vector3 Point1;
  28. Vector3 Point2;
  29. // 旋转每度在一帧中需要拖拽的距离
  30. int DragDistancePerAngle = 8;
  31. void DragToRotateView_Distance()
  32. {
  33. if (Input.GetMouseButtonDown(1)) // 按下右键的瞬间记录起始位置
  34. {
  35. Point1 = Input.mousePosition;
  36. //StartPoint = Point1;
  37. }
  38. if (Input.GetMouseButton(1)) // 按下右键每一帧都执行
  39. {
  40. Point2 = Input.mousePosition;
  41. float dx = Point2.x - Point1.x;
  42. float dy = Point2.y - Point1.y;
  43. float AngleX = dx / DragDistancePerAngle;
  44. float AngleY = dy / DragDistancePerAngle;
  45. transform.RotateAround(HeadTransform.position, Vector3.up, AngleX);
  46. transform.RotateAround(HeadTransform.position, -transform.right, AngleY);
  47. transform.LookAt(HeadTransform);
  48. RelativePos = transform.position - HeadTransform.position;
  49. Point1 = Point2;
  50. Point2 = Vector3.zero;
  51. }
  52. }
  53. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号