欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Unity Third Person CameraController

程序员文章站 2022-03-26 18:06:40
...
void CameraMovement()
    {
        if (currentTarget == null)
            return;
        /*defaultDistance是2.5f,随着不断刷新,
        distance会逐渐接近defaultDistance,实现鼠标缓慢跟随鼠标的效果*/
        distance = Mathf.Lerp(distance, defaultDistance, smoothFollow * Time.deltaTime);
        /*剔除的距离,0*/
        cullingDistance = Mathf.Lerp(cullingDistance, distance, Time.deltaTime);
        /*计算相机的方向,再标准化,targetLookAt只是中间变量,和currentTarget一致*/
        var camDir = (forward * targetLookAt.forward) + (rightOffset * targetLookAt.right);

        camDir = camDir.normalized;

        /*根据currentTarget的当前中心点的位置,但好像是0,有点问题*/
        var targetPos = new Vector3(currentTarget.position.x, currentTarget.position.y + offSetPlayerPivot, currentTarget.position.z);
        Debug.Log("offSetPlayerPivot"+offSetPlayerPivot);

        
        currentTargetPos = targetPos;
        /*height = 1.4f;*/
        desired_cPos = targetPos + new Vector3(0, height, 0);
        current_cPos = currentTargetPos + new Vector3(0, currentHeight, 0);
        RaycastHit hitInfo;

        ClipPlanePoints planePoints = _camera.NearClipPlanePoints(current_cPos + (camDir * distance), clipPlaneMargin);
        ClipPlanePoints oldPoints = _camera.NearClipPlanePoints(desired_cPos + (camDir * distance), clipPlaneMargin);

        //Check if Height is not blocked,一般都是false
        if (Physics.SphereCast(targetPos, checkHeightRadius, Vector3.up, out hitInfo, cullingHeight + 0.2f, cullingLayer))
        {
            var t = hitInfo.distance - 0.2f;
            t -= height;
            t /= (cullingHeight - height);
            cullingHeight = Mathf.Lerp(height, cullingHeight, Mathf.Clamp(t, 0.0f, 1.0f));
        }

        //Check if desired target position is not blocked 
        //culling 剔除      
        if (CullingRayCast(desired_cPos, oldPoints, out hitInfo, distance + 0.2f, cullingLayer, Color.blue))
        {
            distance = hitInfo.distance - 0.2f;
            if (distance < defaultDistance)
            {
                var t = hitInfo.distance;
                t -= cullingMinDist;
                t /= cullingMinDist;
                currentHeight = Mathf.Lerp(cullingHeight, height, Mathf.Clamp(t, 0.0f, 1.0f));
                current_cPos = currentTargetPos + new Vector3(0, currentHeight, 0);
            }
        }
        else
        {
            currentHeight = height;
        }
        //Check if target position with culling height applied is not blocked
        if (CullingRayCast(current_cPos, planePoints, out hitInfo, distance, cullingLayer, Color.cyan)) {
                distance = Mathf.Clamp(cullingDistance, 0.0f, defaultDistance);
        }
        
        /*lookPoint,target的位置往前2个单位*/
        var lookPoint = current_cPos + targetLookAt.forward * 2f;
        /*lookPoint再往右,Vector3.Dot也叫点积,它返回1个-1.0~1.0之间的一个值*/
        lookPoint += targetLookAt.right * Vector3.Dot(camDir * distance, targetLookAt.right);
       // Debug.Log();
        targetLookAt.position = current_cPos;

        Quaternion newRot = Quaternion.Euler(mouseY, mouseX, 0);
        Debug.Log(targetLookAt);
        targetLookAt.rotation = Quaternion.Slerp(targetLookAt.rotation, newRot, smoothCameraRotation * Time.deltaTime);
        /*current_cPos可以认为就是目标的位置,distance就是相机和目标之间的距离,这个值应该是恒定的2.5f*/
        transform.position = current_cPos + (camDir * distance);
        Debug.Log("distance"+distance);

        var rotation = Quaternion.LookRotation((lookPoint) - transform.position);

        transform.rotation = rotation;
        movementSpeed = Vector2.zero;
    }

 

相关标签: web 3D开发