unity 实现摄像机绕某点旋转一周
程序员文章站
2022-06-15 15:50:30
在update函数中执行:if (turnaround) { playercamera.rotatearound(transform.localposition, transform.up,...
在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(); } } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
上一篇: vue组件内部引入外部js文件的方法
下一篇: jQuery实现动态操作table行