用来阻止播放器穿过墙的光线播放只有一半的时间

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

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

    //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);
                }
            }
        }
    }
c# unity3d raycasting
1个回答
0
投票
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); } } }
© www.soinside.com 2019 - 2024. All rights reserved.