摄像机视角优化

    public GameObject target;
    public float ZoomSpeed = 30;//镜头缩放速率
    public float MovingSpeed = 1;//镜头移动速率
    public float RotateSpeed = 3;  //镜头旋转速率
    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 LateUpdate()
    {
        Quaternion rotation;
        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;

            }
            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;
            }
        }
   
        Vector3 startPos = target.transform.position;
        Vector3 dir = (target.transform.position - transform.position).normalized;
        Vector3 aim = startPos - dir * transform.localEulerAngles.y;
        Debug.DrawLine(startPos, aim, Color.red);
        RaycastHit hit;
        if (Physics.Linecast(startPos, aim, out hit))
        {
            if (hit.collider.gameObject.tag != "MainCamera")
            {
                Debug.Log(11);
                transform.position = hit.point + transform.forward* 0.5f;
            }
        }
        transform.LookAt(target.transform);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值