转载自bgzhangzhan等博客;
http://www.ceeger.com/forum/read.php?tid=594491
https://blog.csdn.net/u012194332/article/details/48831017 ----1
1,直接移动到某个位置
void Start () {
Cube.Translate(new Vector3(1, 1, 1));
}
2,在规定时间内移动到某个目标位置
//在time时间内移动物体
private IEnumerator MoveObject(Vector3 startPos, Vector3 endPos, float time)
{
var dur = 0.0f;
while (dur <= time)
{
dur += Time.deltaTime;
Cube.position = Vector3.Lerp(startPos, endPos, dur / time);
yield return null;
}
}
3,以规定的速度移动到某个位置
//以指定速度speed移动物体
private IEnumerator MoveObject_Speed(Vector3 startPos, Vector3 endPos, float speed)
{
float startTime = Time.time;
float length = Vector3.Distance(startPos, endPos);
float frac = 0;
while (frac < 1.0f)
{
float dist = (Time.time - startTime) * speed;
frac = dist / length;
transform.position = Vector3.Lerp(startPos, endPos, frac);
yield return null;
}
}
4,摄像机(或者其他物体)追随某个目标移动——有缓冲的移动
public Transform target; //摄像机要跟随的人物
public float smoothTime = 0.01f; //摄像机平滑移动的时间
private Vector3 cameraVelocity = Vector3.zero;
private Camera mainCamera; //主摄像机(有时候会在工程中有多个摄像机,但是只能有一个主摄像机吧)
void Awake()
{
mainCamera = Camera.main;
}
void Update()
{
mainCamera.transform.position = Vector3.SmoothDamp(mainCamera.transform.position, target.position + new Vector3(0, 0, -5), ref cameraVelocity, smoothTime);
我的移动旋转代码:
1.旋转/俯仰/缩放
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Fun_InsideMove : Function { /// <summary> /// For汽车内部视角的转动 /// </summary> public float sensitivity_RotaH = 0.007f, sensitivity_RotaV = 0.004f; public float scrollSenty = 10; public float minimumY = -60F; public float maximumY = 60F; Vector3 clickPos; Vector3 currentPos; internal static float x = 0f, y = 0f; private void LateUpdate() { if (Fun_View.View_instance == ViewState.Inside) { Car_Move(); } //else { x = 0; y = 0; } } protected sealed override void Car_Move() { if (Input.GetMouseButtonDown(0)) { clickPos = Input.mousePosition; currentPos = Input.mousePosition; } if (Fun_View.View_instance == ViewState.Inside) { RotateView(); ScrollView(); } } private void RotateView() { if (Input.GetMouseButton(0)) { if (currentPos - Input.mousePosition == Vector3.zero) { clickPos = Input.mousePosition; } else { currentPos = Input.mousePosition; } Vector3 offset = clickPos - currentPos; x += offset.x * sensitivity_RotaH; y += offset.y * sensitivity_RotaV; y = Mathf.Clamp(y, minimumY, maximumY); } Quaternion mRotation = Quaternion.Euler(-y, x, 0); //this.transform.rotation = Quaternion.Slerp(transform.rotation,transform.rotation,Time.deltaTime); Camera.main.transform.rotation = Quaternion.Euler(Fun_View.instance.cmInside.eulerAngles.x - y, Fun_View.instance.cmInside.eulerAngles.y + x, Fun_View.instance.cmInside.eulerAngles.z); } private void ScrollView() { float fov = Camera.main.fieldOfView; fov += Input.GetAxis("Mouse ScrollWheel") * scrollSenty; fov = Mathf.Clamp(fov, 30, 75); Camera.main.fieldOfView = fov; } }