why can't i get back on land with this script

#pragma strict

var waterLevel : float;

var groundLevel : float;

private var isUnderwater : boolean;

private var canSwim : boolean = false;

private var underGround : boolean = false;

private var chMotor : CharacterMotor;

function Start () {

chMotor = gameObject.GetComponent(CharacterMotor);

}

function Update ()

{
//swimming setup
if((transform.position.y < waterLevel) != isUnderwater) {

isUnderwater = transform.position.y < waterLevel;

if(isUnderwater) SetUnderwater ();

if(!isUnderwater) SetNormal ();

}

if(transform.position.y < groundLevel)

{

canSwim = true;

underGround = true;

}

else

{

underGround = false;

}
//keys that make character swim
if(isUnderwater && Input.GetKey(KeyCode.E))

{

GetComponent.<ConstantForce>().relativeForce = Vector3(0,-200, 0);

}

else

{

GetComponent.<ConstantForce>().relativeForce = Vector3(0, 0, 0);

}

if(isUnderwater && Input.GetKey(KeyCode.Q))

{

GetComponent.<ConstantForce>().relativeForce = Vector3(0, 200, 0);

}

}

function SetNormal () {

chMotor.movement.gravity = 20;

chMotor.movement.maxFallSpeed = 20;

chMotor.movement.maxForwardSpeed = 6;

chMotor.movement.maxSidewaysSpeed = 6;

canSwim = false;

}

function SetUnderwater () {

chMotor.movement.maxFallSpeed = 5;

chMotor.movement.maxForwardSpeed = 4;

chMotor.movement.maxSidewaysSpeed = 4;

}

this script runs and lets the character swim. But once I try to get back on shore it will not let me it will keep me in the water any help would be nice. Thank you.

How exactly is it keeping you in the water? Is it not letting you jump out or walk out? Also put your code in code tags so it’s readable, like this:

#pragma strict
var waterLevel : float;
var groundLevel : float;
private var isUnderwater : boolean;
private var canSwim : boolean = false;
private var underGround : boolean = false;
private var chMotor : CharacterMotor;
function Start () {
chMotor = gameObject.GetComponent(CharacterMotor);
}
function Update ()
{
if((transform.position.y < waterLevel) != isUnderwater) {
isUnderwater = transform.position.y < waterLevel;
if(isUnderwater) SetUnderwater ();
if(!isUnderwater) SetNormal ();
}
if(transform.position.y < groundLevel)
{
canSwim = true;
underGround = true;
}
else
{
underGround = false;
}
if(isUnderwater && Input.GetKey(KeyCode.E))
{
GetComponent.<ConstantForce>().relativeForce = Vector3(0,-200, 0);
}
else
{
GetComponent.<ConstantForce>().relativeForce = Vector3(0, 0, 0);
}
if(isUnderwater && Input.GetKey(KeyCode.Q))
{
GetComponent.<ConstantForce>().relativeForce = Vector3(0, 200, 0);
}
}
function SetNormal () {
chMotor.movement.gravity = 20;
chMotor.movement.maxFallSpeed = 20;
chMotor.movement.maxForwardSpeed = 6;
chMotor.movement.maxSidewaysSpeed = 6;
canSwim = false;
}
function SetUnderwater () {
chMotor.movement.maxFallSpeed = 5;
chMotor.movement.maxForwardSpeed = 4;
chMotor.movement.maxSidewaysSpeed = 4;
}
1 Like

It will keep me a couple inches from the shore. I can not walk out I cant swim out or anything because it pushes me back once I get a couple of inches next to the shore.

Is your character colliding with the shore itself in such a way that it can’t climb it? It might not be this script that’s stopping you from getting out in that case, make sure your character controller is set up properly

The character makes no contact with sure what so ever. What do you mean my character controller might not be set up correctly?

Are you using a custom character controller? As I understand it, the one premade by Unity has an option to stop players from climbing slopes that are steeper than a certain grade (say, 60%). If this is too low or high, it can obviously cause problems (like not being able to walk up small slopes). Also if it’s not making contact with anything, what is it climbing out onto? Isn’t it making contact with the ground it’s getting out onto?

I tried changing the slope walk up and he will walk on the shore but he will now fly way above it.

would gravity being on have anything to do with that?

Gravity being on should STOP that xD Is he flying in a straight line along the slope, or just straight up?

he is fly up like he’s walking up a steps. I tried flying back over the water and he fell after a certain distance over the water.

What component is that? Post the code to that component.