C# Swimming Script Problem

Swimming isn’t working correctly and I don’t understand what I am doing wrong.
When I jump in the water it’s not applying the gravity…

using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class swim : MonoBehaviour {

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        float height = 0;
        height = transform.position.y;
        if (height < 41.0) {
            this.GetComponent<Rigidbody> ().useGravity = false;
        } else {
            this.GetComponent<Rigidbody> ().useGravity = true;
        }


   

    }
}
using UnityEngine;
using System.Collections;

public class Swimming : MonoBehaviour {
    public float thrust;
    public float jump;
    public Rigidbody rb;
    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody>();
    }
   
    // Update is called once per frame
    void Update () {

        if (Input.GetKey("w")){

            transform.position += transform.forward * Time.deltaTime * thrust;
        }
        if (Input.GetKey("a")){

            transform.position += -transform.right * Time.deltaTime * thrust;
        }
        if (Input.GetKey("d")){

            transform.position += transform.right * Time.deltaTime * thrust;
        }
        if (Input.GetKey("s")){
            Debug.Log ("forwards");
            transform.position += -transform.forward * Time.deltaTime * thrust;
        }
        float height = 0;
        height = transform.position.y;
        if (height < 41.0) {
        if (Input.GetKey("space")){
            Debug.Log ("up");
            rb.AddRelativeForce(transform.up * jump);
        }
            if(Input.GetKey(KeyCode.LeftControl)){
                Debug.Log ("down");
                rb.AddRelativeForce(transform.up * -jump);
            }



        }
    }

   
}

If you jump into the “water” you should continue to fall because even if you turn off the gravity, you’re still moving in that direction and you won’t stop until you hit something… think outer space.

What you probably want is “when you’re in the water” friction is higher so you slow down pretty fast. Then you want to apply a proportional force to the object according to how far under the water the object is.

if inWater
add an upward force of (surface - how deep you are) divided by some number n that gives you an appropriate effect

Without testing … I think that might give you what you want.

Alternatively, if you don’t want floating to the surface… you can just raise the friction once you hit the water and make sure the forces you’re adding to move up and down in the water are strong enough

The script when working with a scene…I’ve gotten this script for this download. Check it out.