Unity—移动
轉載自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.旋轉/俯仰/縮放
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;} } View Code?
轉載于:https://www.cnblogs.com/bananana/p/8761083.html
總結
- 上一篇: 网络IO超时的几种实现
- 下一篇: python概念(一)python基本数