- Home /
How do you get an object to level out when floating?
Hello again, I have a floating script that I got off of here that works like a charm, just wondering, does anyone know of a way to make it so that an object floating will aline so that its largest surface is beneath? I know the engine can't detect that but if it was a variable I could change I wouldn't mind. Here's the floating code I have, and kudos to whomever made it:
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);
}
}
}
It's C# and normally I prefer javascript but I'm not good enough to convert it :/ Thanks.
On subject of code formatting, simply select your text and hit the "Code Sample" button. Then you can more easily format your code blocks. I agree, though, it could be a lot easier to use.
I did try but it only added it to the first line, all better now though :3
Your answer
Follow this Question
Related Questions
Floating object on the water and stand on it. 2 Answers
Making an object float in water 2 Answers
How do i make floating ship ? 0 Answers
Simulating Water not working.. 1 Answer
Realistic boat movement 2 Answers