0
votes

I have a project where the user controls the camera like how you can control the camera in the scene editor in Unity. I am using raycasts to prevent the camera from going through walls, floors and ceilings. The way it works is I send a raycast out at a certain range and if there isn't a hit then the transform will happen normally but won't happen when there is a hit detected. I am using this method for panning left and right and it works perfect for stopping the player from going through walls.I am also using this same method for the mouse wheel zoom and it works perfect as well. The problem occurs when I use this method for stopping the user from going through the ceiling or floor when the user uses vertical translation. The user will sometimes get stuck on the ceiling or floor and I'm not sure why. I have tried a bunch of different things to fix this issue with no luck. If anyone has any idea why this is happening and a possible work around I would appreciate it. Here is my code for vertical translation with the raycasting.

   //if the user moves the camera veritcally
   if (verticalTranslation.isActivated())
   {
            float translateY = Input.GetAxis(mouseVerticalAxisName) * verticalTranslation.sensitivity;
            RaycastHit hit;
            //sends raycast up veritcally
            if (translateY > 0)
            {
                //if raycast hit detected do nothing, else transform position 
                if (Physics.Raycast(transform.position, Vector3.up, out hit, maxDistance))
                {
                    Debug.LogWarning("stop movement");
                    Debug.LogWarning(hit.distance);
                }
                else
                {
                    transform.Translate(0, translateY, 0);
                }
            }
            //sends raycast down veritcally
            if (translateY < 0)
            {
                if (Physics.Raycast(transform.position, -Vector3.up, out hit, maxDistance))
                {
                    Debug.LogWarning("stop movement");
                    Debug.LogWarning(hit.distance);
                }
                else
                {
                    transform.Translate(0, translateY, 0);
                }
            }
    }
1
I am just thinking whether the case translateY == 0 would interest you.Gintas
Thats actually a very good point and I'm really glad you brought that upAunger1205

1 Answers

0
votes

If anyone else is having a similar issue Gintas made a great point and I followed his advice putting a case for translateY == 0. This has fixed the issue. Here is the new code.

   if (verticalTranslation.isActivated())
   {
            position = transform.position;
            float translateY = Input.GetAxis(mouseVerticalAxisName) * verticalTranslation.sensitivity;

            if (position.y < 12284)
            {
                position.y = 12284;
                transform.position = position;
            }

            RaycastHit hit;
            if (translateY > 0)
            {

            if (Physics.SphereCast(origin, sphereRadius, Vector3.up, out hit, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
            {
                Debug.LogWarning("stop movement");
                Debug.LogWarning(hit.distance);
                }
                else
                {
                    transform.Translate(0, translateY, 0);
                }
            }
            if (translateY < 0)
            {
            if (Physics.SphereCast(origin, sphereRadius, -Vector3.up, out hit, maxDistance, layerMask, QueryTriggerInteraction.UseGlobal))
            {
                Debug.LogWarning("stop movement");
                    Debug.LogWarning(hit.distance);
                }
                else
                {
                    transform.Translate(0, translateY, 0);
                }
            }
            if(translateY == 0)
            {
                transform.Translate(0, translateY, 0);
            }
    }