射线广播过去曾阻止播放器穿过墙壁仅一半时间

时间:2020-04-08 14:19:37

标签: c# unity3d raycasting

我正在处理一个项目,该项目使用光线投射来检测墙壁,以防止玩家穿过墙壁。在这个项目中,我有一个处理所有相机运动的脚本。用户可以像场景查看器一样统一控制摄像机。我正在使用相同的光线投射方法来防止用户前进,并且大部分时间都在起作用,但是当用户在向左或向右平移时(例如,向左或向右平移)尝试使用相同的方法时,它只能使用大约一半的时间。如果您继续闯入墙壁,最终您将经历。如果有人对我的工作有任何建议或改进的方法,我将不胜感激。

    //if the middle button is activated and player moves it left or right
    if (horizontalTranslation.isActivated())
    {
        //this will move the camera left or right when horizontal translation is activated  
        float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
        transform.Translate(translateX, 0, 0);
        //variables used for the raycast
        origin = transform.position;
        Vector3 eLeft = -GetComponent<Camera>().transform.right;
        Vector3 eRight = GetComponent<Camera>().transform.right;
        RaycastHit hitLeft;
        RaycastHit hitRight;
        //this will send a raycast right if player moves camera right
        if (translateX > 0)
        {
            Debug.LogWarning("right");
            if (Physics.SphereCast(origin, sphereRadius, eRight, out hitRight, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
            {
                //sets variable to position before hitting wall
                Debug.LogWarning("right initial hit " + hitRight.distance);
                if ((hitRight.distance < 5) && (hitRight.distance > 1))
                {
                    originalRayPos = transform.position;
                    originalRayRot = transform.rotation;
                    Debug.LogWarning("right set " + hitRight.distance);
                }
                //uses set variable to reset position back after hitting the threshold
                if (hitRight.distance < 1)
                {
                    transform.position = originalRayPos;
                    transform.rotation = originalRayRot;
                    Debug.LogWarning("right use " + hitRight.distance);
                }
            }
        }
        //this will send raycast left if player moves camera left
        if (translateX < 0)
        {
            Debug.LogWarning("left");
            if (Physics.SphereCast(origin, sphereRadius, eLeft, out hitLeft, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
            {
                Debug.LogWarning("left initial hit " + hitLeft.distance);
                if ((hitLeft.distance < 5) && (hitLeft.distance > 1))
                {
                    originalRayPos = transform.position;
                    originalRayRot = transform.rotation;
                    Debug.LogWarning("left set " + hitLeft.distance);
                }
                if (hitLeft.distance < 1)
                {
                    transform.position = originalRayPos;
                    transform.rotation = originalRayRot;
                    Debug.LogWarning("left use " + hitLeft.distance);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我找到了一种更好的方法来完成这项工作。我改变了我的方法。我没有使用变量来存储先前的位置,而是对其进行了设置,因此,如果通过raycast注册命中,则不会发生转换,但是如果未检测到命中,则将发生转换。我还从原始来源http://wiki.unity3d.com/index.php/MouseCameraControl

修改了此代码
    if (horizontalTranslation.isActivated())
    {
        //sets translate to be used for transformation
        float translateX = Input.GetAxis(mouseHorizontalAxisName) * horizontalTranslation.sensitivity;
        //variables for raycasting
        origin = transform.position;
        Vector3 eLeft = -GetComponent<Camera>().transform.right;
        Vector3 eRight = GetComponent<Camera>().transform.right;
        RaycastHit hitLeft;
        RaycastHit hitRight;
        //raycast sent to right
        if (translateX > 0)
        {
            if (Physics.SphereCast(origin, sphereRadius, eRight, out hitRight, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
            {
                //do nothing if hit is detected
            }
            else
            {
                //transform position if no hit detected
                transform.Translate(translateX, 0, 0);
            }
        }
        //raycast sent to left
        if (translateX < 0)
        {
            if (Physics.SphereCast(origin, sphereRadius, eLeft, out hitLeft, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
            {
                //transform.Translate(translateX, 0, 0);
            }
            else
            {
                transform.Translate(translateX, 0, 0);
            }
        }    
    }