zoukankan      html  css  js  c++  java
  • 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
    余生很长,愿我们都活成自己喜欢的样子
  • 相关阅读:
    dapper hepler帮助类
    别人在用你的什么技术在赚钱.其实你天天在做
    把asp.net mvc5 controller 单独放置在一个项目实例
    重写和动态创建 asp.net mvc 的控制器
    Linux 配置环境变量,环境变量优先级
    Redhat/ Centos/Ubuntu Linux sudo 权限配置,添加root权限
    Linux和window的文件传输 ( NFS篇 )
    安装R软件,R-3.5.3软件的下载、编译及安装
    yum 源
    瓜大无人船踩坑记3
  • 原文地址:https://www.cnblogs.com/bananana/p/8761083.html
Copyright © 2011-2022 走看看