Floating on the water

I have the following code to make the player float when he enter to the water:

var frequencyMin : float = 1.0; 
var frequencyMax : float = 3.0; 
var magnitude : float = 0.0025; 
var Player:Transform;
private var randomInterval : float; 

function Start () { 
   randomInterval = Random.Range(frequencyMin, frequencyMax); 
} 
function Floating(){
   Player.position.y +=(Mathf.Cos(Time.time * randomInterval) * magnitude); 
   Player.eulerAngles.x += (Mathf.Cos(Time.time * randomInterval) * 2);
   yield WaitForSeconds(1);
}
function Update () { 
if(Movementwii.floater==true){
Player.position.y=transform.position.y;
Movementwii.waves=true;
}
if(Movementwii.waves==true)
Floating();
   
}

The problem is that for some reason the player seems to bounce hard, and its suposed to give the illusion of floating. I guess that the problem has to be related to this line: “Player.position.y=transform.position.y;”
But I need that the player has a portion of the body under the plane of water, so thats why I place that line.
Please Help

I guess one problem is that the player is subject to your wave algorithm regardless of where the wave is in the cycle. One option would be to interpolate the position towards the target position. This will smooth things out but I have not tested your script in a scene.

You may also wish to set a start time when the player hits the water and diff against that time, rather than taking the current time as the input to cos(). If you take that time when the player hits the water, use sin() instead as it will start with zero offset. Maybe even use -sin(time since start), since it will then begin its cycle with a downwards dip.

I modified the code, but now its seems not to work at all, the other one was buggy so I try to modify it. This is the code I have.

var frequencyMin : float = 1.0; 
var frequencyMax : float = 3.0; 
var magnitude : float = 0.0025; 
var Player:Transform;
var waterlevel;
private var randomInterval : float; 
var surface=false;
function Start () { 
   randomInterval = Random.Range(frequencyMin, frequencyMax); 
  waterlevel=transform.position.y;

} 
function Floating(){
   Player.position.y +=(Mathf.Cos(Time.time * randomInterval) * magnitude); 
   Player.eulerAngles.x += (Mathf.Cos(Time.time * randomInterval) * 2);
}
function Surface(){
Player.position.y=waterlevel;
surface=true;
}
function Update () { 
if(Movementwii.floater==true&surface==false)
Surface();
if(surface==true)
Floating();

}

And I change some lines in the players movement script like this part:

if (grounded||floater)
			verticalSpeed = 0.0;
		else
			verticalSpeed -= gravity * Time.deltaTime;

I think this should avoid the player sink, but this is what is actually happening.
Please help

I thought of another problem which I don’t think is being addressed by these scripts. When you change the Y position of the player, they may suddenly be above water level and floating will be turned off.

You will have to set up some kind of swimming state. One way (maybe not the best one) to do this would be to raycast from water height at the player’s XZ position downwards along the Y axis to see how far the bottom of the lake/seabed is. If the water is deep enough, make the player float.

Otherwise I think you will have problems with the floating state changing all the time when the height of the player is modified. When you can successfully manage the floating state, setting the player height with a wave motion should be fairly straightforward. Hope that helps (I’m sorry I don’t have time to set up a test scene, but if I can point you in the right direction I hope you can fix it) :slight_smile:

If the floating object is a rigidbody, you might find the script and explanation in this thread useful.

That’s quite a nice solution and, I expect, more realistic. I definitely recommend this approach ^ ^ ^ :slight_smile:

The problem is that my player is using a charactercontroller, so the rigidbody seems not to work when this component is around. I need my player always treading in water, so I guess the raycasting might be the best choice in this case. Or does the rigidbody works when its already a component of charactercontroller

It is possible to switch between rigidbody and character controller behaviour, but if you need the controller behaviour while floating then don’t use the rigidbody approach.