zoukankan      html  css  js  c++  java
  • Unity3D学习_摄像机跟随脚本(及进阶版)_2018Oct

    当目标点移动后,摄像机稍晚一步跟随移动,并在目标点停止时出现"平缓制动"效果.

    脚本实现: 

        public float Speed;           //跟随速度
        public Transform TargetPos;   //目标点信息
    
        private Vector3 _distance;    //距离
        void Start()
        {
            _distance = transform.position - TargetPos.position;
            Speed = 5;
        }
    
        void LateUpdate()
        {
            Vector3 newPos = TargetPos.position + _distance;      
            transform.position = Vector3.Lerp(transform.position, newPos, Speed * Time.deltaTime);
        }

     进阶版本:(实现了更多功能)

    public class MouseFollow_ForRPG_CSharp : MonoBehaviour
    {
        public GameObject target;
        public float ZoomSpeed = 30;//镜头缩放速率
        public float MovingSpeed = 1;//镜头移动速率
        public float RotateSpeed = 1;  //镜头旋转速率
        public float distance = 20;//设置距离角色的距离
        public float ViewAngle = 30;//设置镜头斜视的角度
        void Start()
        {
            if (target)
            {
                transform.rotation = Quaternion.Euler(ViewAngle, target.transform.rotation.eulerAngles.y, 0);
                transform.position = transform.rotation * new Vector3(0, 0, -distance) + target.transform.position;
            }
        }
        void Update()
        {
            Quaternion rotation;
            Vector3 position;
            float delta_x, delta_y;
            float delta_rotation_x, delta_rotation_y;
            if (target)
            {
                if (Input.GetMouseButton(0))
                {
                    delta_x = Input.GetAxis("Mouse X") * MovingSpeed;
                    delta_y = Input.GetAxis("Mouse Y") * MovingSpeed;
                    rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
                    transform.position = rotation * new Vector3(-delta_x, 0, -delta_y) + transform.position;
                    Debug.Log(-delta_x+"  "+-delta_y);
                }
                else
                {
                    if (Input.GetAxis("Mouse ScrollWheel") != 0)
                    {
                        distance += -Input.GetAxis("Mouse ScrollWheel") * ZoomSpeed;
                    }
                    if (Input.GetMouseButton(1))
                    {
                        delta_rotation_x = Input.GetAxis("Mouse X") * RotateSpeed;
                        delta_rotation_y = -Input.GetAxis("Mouse Y") * RotateSpeed;
                        transform.Rotate(0, delta_rotation_x, 0, Space.World);
                        transform.Rotate(delta_rotation_y, 0, 0);
                    }
                    else
                    {
                        transform.rotation = Quaternion.Slerp(transform.rotation,
                        Quaternion.Euler(transform.rotation.eulerAngles.x, target.transform.rotation.eulerAngles.y, 0
                        ), Time.deltaTime * 2);
                    }
                    transform.position = transform.rotation * new Vector3(0, 0, -distance) + target.transform.position;
                }
            }
        }
    }

    相关知识:(网页截图)   

  • 相关阅读:
    Java 抽象类和抽象方法
    java中的不为空判断
    表单form action的url写法
    对称加密和非对称加密
    spring全注解项目
    java自定义异常
    Spring data jpa学习
    SpringMVC实现简单应用
    MySql与Java的时间类型
    java反射
  • 原文地址:https://www.cnblogs.com/RainPaint/p/9834880.html
Copyright © 2011-2022 走看看