Hey there. I am working on an FPS with a swimming option. I have it so that I can jump on the surface of the water, then hit a key to dive. After you go underwater everything turns blue, gravity is low, and I can swim freely in all 3 axis. Easy. I also have it when I exit the water level based on the world height, everything is set to normal. My issue lies with the fact that whenever I try and "surface" while still in the water the fog effects for my water flicker on and off and it won't let me go above the surface. I basically want it so that if I swim close to the top, it will send me into surface mode smoothly. Here's my water script.
var waterLevel : float;
var isUnderwater: boolean;
var normalColor: Color;
var underwaterColor: Color;
private var chMotor: Player_Controller;
private var chLook: Mouse_Look;
private var surface: boolean;
private var controller: CharacterController;
private var moveDirection = Vector3.zero;
function Start ()
{
chMotor = GetComponent(Player_Controller);
chLook = GetComponent (Mouse_Look);
controller = GetComponent(CharacterController);
}
function OnTriggerEnter (other: Collider){
if (other.gameObject.name == "Surface" && !isUnderwater){
chMotor.gravity = 0;
surface = true;
}
//I can't get it to work when if(other.gameObject.name == "Surface" && isUnderwater) is the trigger function.
}
function Update ()
{
if (surface == true && Input.GetKeyUp (KeyCode.E)){
isUnderwater = true;
surface = false;
}
if ((transform.position.y < waterLevel) != isUnderwater)
{
isUnderwater = transform.position.y < waterLevel;
if (isUnderwater && !surface) SetUnderwater();
if (!isUnderwater) SetNormal();
}
}
function SetNormal()
{
RenderSettings.fogColor = normalColor;
RenderSettings.fogMode = FogMode.Linear;
RenderSettings.fogStartDistance = 0f;
RenderSettings.fogEndDistance = 25000f;
chMotor.gravity = -10000;
chMotor.speed = 100;
chMotor.speedStore = 100;
chMotor.runspeed = 250;
chLook.Axis = chLook.Axis.MouseX;
}
function SetUnderwater()
{
RenderSettings.fogColor = underwaterColor;
RenderSettings.fogMode = FogMode.Linear;
RenderSettings.fogStartDistance = 149.26f;
RenderSettings.fogEndDistance = 1000f;
chMotor.gravity = -1000;
chMotor.speed = 65;
chMotor.speedStore = 65;
chMotor.runspeed = 100;
chLook.Axis = chLook.Axis.MouseXandY;
}
↧