- Home /
 
How to get fps out of water
So, I'm working on a project and I need my character to be able to swim. I wrote a script (see below) that allows the player to go lower (dive) when 'e' is pressed and rise when 'q' is pressed. On getting to the water surface, I can move horizontally, but cannot get out of the water. After a few tries, I noticed in the scene view that an invincible wall (for lack of better terms) is obstructing the player and as such its stuck there. I would like to know how I can get around this. Suggestions for my code will also be appreciated. (All assets used are the standard ones that came with unity.)
Here is a link to a sample scene that best highlights the problem (please ignore the annoying background music) http://myweb.ttu.edu/fanya/scene/scene.html
And this is my swimming script: public class Swimming : MonoBehaviour { public float maxSwimSpeed = 200f; public float extraHeight = 20f;
         private CharacterMotor chMotor;
         private Fog fog;
         private int underWaterLevel;
         public bool isUnderwater;
         public bool canGoDown;
         private Rigidbody rig;
         private float length = 0.2f;
      
         // Default values
         private float dGravity;
         private float dmaxFallSpeed;
         private float dmaxForwardSpeed;
         private float dmaxSidewaysSpeed;
      
         void Start()
         {
             // Assign references
             chMotor = GetComponent<CharacterMotor>();
      
             // Fog stores the underwater level
             fog = GetComponentInChildren<Fog>();
             underWaterLevel = fog.underwaterLevel;
      
             rig = GetComponent<Rigidbody>();
      
             // Get defaults
             dGravity = chMotor.movement.gravity;
             dmaxFallSpeed = chMotor.movement.maxFallSpeed;
             dmaxForwardSpeed = chMotor.movement.maxForwardSpeed;
             dmaxSidewaysSpeed = chMotor.movement.maxSidewaysSpeed;
         }
      
         void Update()
         {
             isUnderwater = transform.position.y <= underWaterLevel + extraHeight; ;
      
             if (isUnderwater)
             {
                 // Slow down movement
                 chMotor.movement.gravity = 0;
                 chMotor.movement.maxFallSpeed = 5;
                 chMotor.movement.maxForwardSpeed = 4;
                 chMotor.movement.maxSidewaysSpeed = 4;
      
                 // Cast ray downward. If it collides with another collider, disable downward motion
                 if (Physics.Raycast(transform.position, Vector3.down, length))
                     canGoDown = false;
                 else
                     canGoDown = true;
                 Debug.DrawRay(transform.position, Vector3.down * length);
             }
             else
             {
                 // Reset all these
                 chMotor.movement.gravity = dGravity;
                 chMotor.movement.maxFallSpeed = dmaxFallSpeed;
                 chMotor.movement.maxForwardSpeed = dmaxForwardSpeed;
                 chMotor.movement.maxSidewaysSpeed = dmaxSidewaysSpeed;
      
                 canGoDown = false;
             }
         }
      
         void FixedUpdate()
         {
             // If underwater, slow down motion
             if (isUnderwater)
             {
                 // If underwater, and not on ground, exert downward force to push player down
                 if (Input.GetKey(KeyCode.E) && canGoDown)
                 {
                     rig.AddForce(Vector3.down * maxSwimSpeed, ForceMode.Acceleration);
                 }
      
                 // Allow player to move upward if underwater
                 else if (Input.GetKey(KeyCode.Q))
                 {
                     rig.AddForce(Vector3.up * maxSwimSpeed, ForceMode.Acceleration);
                 }
             }
         }
     }
 
 
              Your answer