这篇文章主要介绍了unity 实现摄像机绕某点旋转一周,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
在Update函数中执行:
if (turnAround)
{
playerCamera.RotateAround(transform.localPosition, transform.up, Time.deltaTime * rotateSpeed);
degree += Time.deltaTime * rotateSpeed;
if (degree >= 360)
{
turnAround = false;
degree = 0;
}
}
补充:unity 摄像机围绕某个物体进行旋转放大缩小
脚本通过以一个物体为中心点
来控制摄像机围绕物体旋转缩放 ,脚本挂在摄像机上即可,target是需要观看的物体
public Transform Camera2;
public GameObject target;
private float dis;
public float xSpeed = 200, ySpeed = 200, mSpeed = 10; //移动速度
public float yMinLimit = -50, yMaxLimit = 50; //摄像机的Y轴移动最小最大限制
public float distance = 7, minDistance = 2, maxDistance = 30; //摄像机与目标物体的距离
public bool needDamping = true; //阻尼默认开启
float damping = 5.0f; //默认阻尼为5.0F
public float x = 0.0f; //X轴
public float y = 0.0f; //Y轴
// Use this for initialization
void Start() {
instance = this;
Camr = Camera2Rotation.Close;
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
private void Update()
{
}
void LateUpdate()
{
//使用按下鼠标左键移动物体
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * disVector + target.transform.position;
//adjust the camera
if (needDamping)
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
}
else
{
transform.rotation = rotation;
transform.position = position;
}
}
/// <summary>
/// 旋转角度的控制
/// </summary>
/// <param name="angle">旋转的角度</param>
/// <param name="min">最小角度</param>
/// <param name="max">最大角度</param>
/// <returns></returns>
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
void MoveCameraToTarget()
{
if (target != null)
{
Camera2.LookAt(target.transform);
Camera2.position = Vector3.MoveTowards(Camera2.position, target.transform.position, 5 * Time.deltaTime);
dis = Vector3.Distance(Camera2.transform.position, target.transform.position);
if (dis < 1.5f)
{
Camr = Camera2Rotation.Open;
CancelInvoke();
}
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持得得之家。如有错误或未考虑完全的地方,望不吝赐教。
沃梦达教程
本文标题为:unity 实现摄像机绕某点旋转一周
基础教程推荐
猜你喜欢
- unity实现动态排行榜 2023-04-27
- C#类和结构详解 2023-05-30
- C#控制台实现飞行棋小游戏 2023-04-22
- ZooKeeper的安装及部署教程 2023-01-22
- C# windows语音识别与朗读实例 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- C# List实现行转列的通用方案 2022-11-02
- C# 调用WebService的方法 2023-03-09
- winform把Office转成PDF文件 2023-06-14
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26