Unity实现摄像机围绕物体旋转
2017-10-20 17:17
585 查看
rotateY和rotateX的初始坐标与要围绕旋转的物体坐标一致,比如(0,0,0)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraCtrl : MonoBehaviour
{
public GameObject rotateY;
public GameObject rotateX;
private Quaternion targetTransY;
private Quaternion targetTransX;
private float mouseY;
private float mouseX;
private float YAngleChange;
private float XAngleChange;
private float smoothSpeed = 10f;
void Update()
{
if (Input.GetMouseButton(0))
{
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
RotateByX(mouseY);
RotateByY(mouseX);
}
}
void RotateByY(float xValue)
{
YAngleChange += xValue * smoothSpeed * Time.deltaTime * 90;
targetTransY = Quaternion.Euler(0, YAngleChange, 0);
rotateY.transform.localRotation = Quaternion.Slerp(rotateY.transform.localRotation, targetTransY, 2);
}
void RotateByX(float yValue)
{
XAngleChange -= yValue * smoothSpeed * Time.deltaTime * 90;
XAngleChange = Mathf.Clamp(XAngleChange, -70f, 40f);
targetTransX = Quaternion.Euler(XAngleChange, 0, 0);
rotateX.transform.localRotation = Quaternion.Slerp(rotateX.transform.localRotation, targetTransX, 2);
}
// 也可以实现
//void RotateByYT(float xValue)
//{
// rotateY.transform.Rotate(Vector3.up * xValue * Time.deltaTime * smoothSpeed);
//}
//void RotateByXT(float yValue)
//{
// rotateX.transform.Rotate(Vector3.right * yValue * Time.deltaTime * smoothSpeed * (-1));
//}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraCtrl : MonoBehaviour
{
public GameObject rotateY;
public GameObject rotateX;
private Quaternion targetTransY;
private Quaternion targetTransX;
private float mouseY;
private float mouseX;
private float YAngleChange;
private float XAngleChange;
private float smoothSpeed = 10f;
void Update()
{
if (Input.GetMouseButton(0))
{
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
RotateByX(mouseY);
RotateByY(mouseX);
}
}
void RotateByY(float xValue)
{
YAngleChange += xValue * smoothSpeed * Time.deltaTime * 90;
targetTransY = Quaternion.Euler(0, YAngleChange, 0);
rotateY.transform.localRotation = Quaternion.Slerp(rotateY.transform.localRotation, targetTransY, 2);
}
void RotateByX(float yValue)
{
XAngleChange -= yValue * smoothSpeed * Time.deltaTime * 90;
XAngleChange = Mathf.Clamp(XAngleChange, -70f, 40f);
targetTransX = Quaternion.Euler(XAngleChange, 0, 0);
rotateX.transform.localRotation = Quaternion.Slerp(rotateX.transform.localRotation, targetTransX, 2);
}
// 也可以实现
//void RotateByYT(float xValue)
//{
// rotateY.transform.Rotate(Vector3.up * xValue * Time.deltaTime * smoothSpeed);
//}
//void RotateByXT(float yValue)
//{
// rotateX.transform.Rotate(Vector3.right * yValue * Time.deltaTime * smoothSpeed * (-1));
//}
}
相关文章推荐
- unity 鼠标控制摄像机围绕物体旋转
- 【Unity 3D学习】获取鼠标滑动方向(上下左右)-控制摄像机围绕物体旋转
- Unity入门系列||Unity中如何用脚本实现物体的循环移动和旋转
- 鼠标控制摄像机围绕物体旋转拍摄
- three.js实现围绕某物体旋转
- Unity的摄像机拉近拉远和旋转脚本实现
- 【狼】unity 鼠标拖拽物体实现任意角度自旋转
- 我的Unity(3)一点一滴 实现物体移动和旋转的方法总结(1)
- Unity3D开发之控制摄像机围绕物体旋转 平移 缩放
- unity 相机围绕某物体自动旋转
- 让物体围绕自身某一点旋转的方法 Unity
- unity-实现摄像机跟随物体(Vector3.SmoothDamp)
- unity之RotateAround 一个物体围绕另外一个物体旋转
- Unity物体或摄像机的旋转缩放
- Unity——Rigidbody实现真实的物体旋转效果/解决物体添加刚体后不随父物体移动/hingejoint
- unity 摄像机围绕物体转动(有万向锁)
- unity 3d中简单巧妙的实现拖动鼠标旋转物体
- Unity3d实现物体围绕某一点进行旋转
- Unity 3D 中实现对物体 位置(position) 旋转(rotation) 大小(scale) 的全面控制
- 摄像机围绕物体旋转观察