日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity—移动

發(fā)布時間:2025/4/16 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity—移动 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載自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,在規(guī)定時間內(nèi)移動到某個目標(biāo)位置
?? ?//在time時間內(nèi)移動物體
?? ?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,以規(guī)定的速度移動到某個位置
?//以指定速度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,攝像機(或者其他物體)追隨某個目標(biāo)移動——有緩沖的移動
?? ? ?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);

?

我的移動旋轉(zhuǎn)代碼:

1.旋轉(zhuǎn)/俯仰/縮放

public class Fun_InsideMove : Function {/// <summary>/// For汽車內(nèi)部視角的轉(zhuǎn)動/// </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

?

轉(zhuǎn)載于:https://www.cnblogs.com/bananana/p/8761083.html

總結(jié)

以上是生活随笔為你收集整理的Unity—移动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。