【问题标题】:Camera collision detection相机碰撞检测
【发布时间】:2020-06-09 14:57:57
【问题描述】:

我的相机脚本有问题。摄像机通过使用指定为摄像机子项的枢轴围绕玩家旋转。这适用于允许相机左右旋转的 2 个箭头(我正在为手机开发这款游戏,所以它们是触摸箭头)。

问题是当相机在墙壁或巨大物体后面并且看不到任何东西时。我在寻找解决方案,发现许多开发人员使用 RaycastHit 或类似的东西。

这是我的代码,目标是让相机在靠近墙壁时更靠近枢轴,以避免阻挡相机视图。谁能帮帮我?

public Transform target1;
public Transform pivot;

protected ButtonLeft buttonLeft;
protected ButtonRight buttonRight;

public Vector3 offset;
public bool useOffsetValues;

public float rotateSpeed;  

private void Start()
{
    buttonLeft = FindObjectOfType<ButtonLeft>();
    buttonRight = FindObjectOfType<ButtonRight>();

    if (!useOffsetValues)
    {
        offset = target1.position - transform.position;
    }

    pivot.transform.position = target1.transform.position;

    //pivot.transform.parent = target.transform;

    //USE IF U WANT TO DISAPPEAR THE CURSOR
    //Cursor.lockState = CursorLockMode.Locked;

    //pivot.transform.parent = target.transform;
    pivot.transform.parent = null;

    // usa questa dopo la costruzione del livello1
    //pivot.transform.position = target.transform.position;
}

private void Update()
{
    pivot.transform.position = target1.transform.position;

    if (buttonLeft.Pressed)
    {
        pivot.Rotate(0, -90 * Time.deltaTime, 0);
        Debug.Log("rotate left");
    }

    if (buttonRight.Pressed)
    {
        pivot.Rotate(0, 90 * Time.deltaTime, 0);
        Debug.Log("rotate left");
    }

    Ray ray = new Ray(pivot.transform.position, pivot.transform.position - transform.position);
    RaycastHit hit;

    /*float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
    pivot.Rotate(0, horizontal, 0);
    pivot.Rotate(0, horizontal, 0);
    Use this to make the camera rotate on Mouse Y axes*/
    /*float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
    target.Rotate(vertical, 0, 0); */

    //move camera based on the current rotation of the target and the original offset

    float desiredYAngle = pivot.eulerAngles.y;
    //Use this float to set the x angle of player
    float desiredXAngle = pivot.eulerAngles.x;
    //Use this rotation only if you want to rotate on Y
    //Quaternion rotation = Quaternion.Euler(0, desiredYAngle, 0);
    //Use this if u want to rotate up&down on x axes
    Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
    transform.position = target1.position - (rotation * offset);

    //transform.position = target.position - offset;

    transform.LookAt(target1);
}

【问题讨论】:

    标签: c# unity3d camera collision raycasting


    【解决方案1】:

    好的方法是从枢轴到摄像机进行光线投射,如果发现一些障碍物,将摄像机放在光线上,比命中点更靠近枢轴。像这样:(伪代码,未测试):

    Vector3 origin =  pivot.transform.position;
    Vector3 direction = transform.position - pivot.transform.position;
    
    float maxCameraDistance = 10;
    
    Ray ray = new Ray(origin, direction);
    RaycastHit hit;
    if (Physics.Raycast(ray, maxCameraDistance))
    {
        Vector3 offsetFromObstacle = -direction.normalized * 0.1f;
        transform.position = hit.point + offsetFromObstacle;
    }
    

    【讨论】:

    • 您可以在此处添加一个 lerp,或者将相机不直接移动到命中点,而是移动到它正上方的点,在相机和枢轴之间保持一定距离。例如,在我的代码中设置位置后添加这一行:transform.position = new Vector3(transform.position.x, 5, transform.position.z);假设您的相机围绕 Y 轴旋转,并且 5 是您的相机高度。
    猜你喜欢
    • 2019-04-30
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多