相机碰撞检测

问题描述 投票:0回答:1

我在使用我的相机脚本时遇到了麻烦。摄像机通过使用指定为摄像机子级的枢轴围绕播放器旋转。这可以使用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个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.