You’re using the trigger events wrong. This script must be attached either to the water or to the player. If your script is attached to the water (trigger), you must check if the other object is the player before controlling it. If the script is attached to the player, on the other hand, you must check if the other object is the water. Supposing the script is attached to the water, it should be something like this:
using UnityEngine;
using System.Collections;
public class gravity : MonoBehaviour {
public GameObject _pc;
public GameObject _water;
bool inWater = false;
void OnTriggerEnter(Collider other) {
if (other.gameObject == _pc){ // if other object is _pc...
Debug.Log ("pc entered the water");
inWater = true; // set inWater flag
_pc.rigidbody.useGravity = false; // turn gravity off
}
}
void OnTriggerExit(Collider other) {
if (other.gameObject == _pc) // if _pc exited the trigger...
Debug.Log ("pc exited the water");
inWater = false; // set inWater to false
_pc.rigidbody.useGravity = true; // turn gravity on
}
void FixedUpdate() {
if (inWater){ // if inside water...
_pc.rigidbody.AddForce(0, 50, 0); // add force up
}
}
}
But notice that you’re actually creating a “negative weight” when the player enters the water: this force up will push the player out of the water, what will turn gravity on, making it fall to the water, which in turn will activate the force up again, and so on - in other words, the player will shake up and down frenetically while over the water.
EDITED: As I said in my comment, you can try to solve the problem above applying a force proportional to the submersed height. You must also use a slightly different method to detect when the player enters and exits the water, since a mesh collider will report trigger events when the player gets completely submersed. The whole thing could be like this (water script):
using UnityEngine;
using System.Collections;
public class gravity : MonoBehaviour {
public GameObject _pc;
bool inWater = false;
float height;
float waterY;
void Start(){
waterY = transform.position.y;
}
void OnTriggerEnter(Collider other) {
float y = other.transform.position.y;
// if other is _pc and it's above the water...
if (other.gameObject == _pc && y > waterY){
inWater = true; // set inWater flag
height = 2 * (y - waterY); // estimate the player height
}
}
void OnTriggerExit(Collider other) {
float y = other.transform.position.y;
if (other.gameObject == _pc && y > waterY) // if _pc exited the trigger...
inWater = false; // set inWater to false
}
void FixedUpdate() {
if (inWater){ // if inside water...
// calculate the submersed portion (1 means 100%)
float submersion = 0.5f + (waterY - _pc.transform.position.y)/height;
submersion = Mathf.Clamp(submersion, 0, 1); // clamp to 0..1 range
// force up will be limited to ~ 80% weight when totally submersed
_pc.rigidbody.AddForce(0, _pc.rigidbody.mass * 8.0 * submersion, 0);
}
}
}
There are some mysterious tricks here. First: when the player touches the water plane, its height is estimated as twice the vertical distance between its position (the pivot) and the water plane. Second: in FixedUpdate, a submersion factor is calculated, which ranges from 0 (player has only wet his feet) to 1 (completely submersed) and multiplied by 8 to give an up force that ranges from 0 to 80% of his weight (8/9.8, the default gravity).