Buoyancy (424005)

I know very little about C# and was wondering if someone could convert the little script to jScript/UnityScript for me.:slight_smile:

using UnityEngine;
using System.Collections;

public class Floater : MonoBehaviour {
	public float waterLevel, floatHeight;
	public Vector3 buoyancyCentreOffset;
	public float bounceDamp;
//===================================	
    void FixedUpdate () {
    Vector3 actionPoint = transform.position + transform.TransformDirection(buoyancyCentreOffset);
    float forceFactor = 1f - ((actionPoint.y - waterLevel) / floatHeight);
    if (forceFactor > 0f) {
    Vector3 uplift = -Physics.gravity * (forceFactor - rigidbody.velocity.y * bounceDamp);
    rigidbody.AddForceAtPosition(uplift, actionPoint);
    }
  }
}

I know nothing about C# also, but I think your script roughly translates to this:

var waterLevel : float;
var floatHeight : float:
var buoyancyCentreOffset : Vector3;
var bounceDamp : float;

function FixedUpdate () {
	var actionPoint : Vector3 = transform.position + transform.TransformDirection(buoyancyCentreOffset);
	var forceFactor : float = 1f - ((actionPoint.y - waterLevel) / floatHeight);
	if (forceFactor > 0.0) {
		var uplift : Vector3 = - Physics.gravity * (forceFactor - rigidbody.velocity.y * bounceDamp);
		rigidbody.AddforceAtPosition(uplift, actionPoint);
	}
}

I’m pretty sure this won’t compile without errors as I noticed there are several undeclared variables in there. I’m also not sure if you need to declare the variable types in the FixedUpdate () function. This is a super quick hack and I’ll be interested to see how close to being right I got. :wink:

Incidentally, I believe Andeee posted a buoyancy script a long while back. Is this that script? I thought he posted one in Javascript?

Or is this the script from the Ocean project? I sort of recognize it now…

A leftover of C# :slight_smile:

Good catch, go ahead and change the “1f” to “1.0”. I’m sure there are other things I may have missed as I really don’t know C# at all but thought this might be a good time to learn a bit. :wink:

Just for future reference, there has actually been a JS version of that script on the original page for some time now.

:)TY Andeeee:)