- Home /
C# Water Rotation
Hello, I am developing a game that replicates water physics (2D Side-View), Everything works fine apart from the fact that once an object(I will call it a brick from now on) starts spinning it will never stop, Here is the part of the code that deals with the floating of the gameObject (It is attached to the water):
public float waterLevel = 0f;
public float floatHeight = 2;
public float bounceDamp = 0.05f;
public Vector3 buoyancyCentreOffset;
private float forceFactor;
private Vector3 actionPoint;
private Vector3 uplift;
void OnTriggerStay2D(Collider2D Hit)
{
actionPoint = Hit.transform.position + Hit.transform.TransformDirection(buoyancyCentreOffset);
forceFactor = 1f - ((actionPoint.y - waterLevel) / floatHeight);
if(forceFactor > 0f){
uplift = -Physics.gravity * (forceFactor - Hit.rigidbody2D.velocity.y * bounceDamp);
Hit.rigidbody2D.AddForceAtPosition(uplift, actionPoint);
}
}
I was wondering if anyone knew some code that would make the brick rotate towards it original rotation, bearing in mind that it has to look like it would do in real life if the object were to spin in real water.
Thanks for any help in advance.
Not really answering your question(hence the comment) but would it be easier to use rigidbody2D.gravityScale = 0.0f to keep the object "floating"? Your also comparing to 0.0f which might not be a good idea. Have you tried comparing with forceFactor > epsilon?
The current water based floating system works fine for my purpose as it allows me to control a vast array of variables that I can use to simulate different liquids.
It is a good point however, Got any ideas on how to make the rotation happen in water ?
Do you have any damping values set on the rigidbody? if not, it will continue spinning as if it was in space. You may need to adjust damping values on the rigidbody to make it work better?
Your answer
Follow this Question
Related Questions
How to rotate a GameObject with another GameObject while simulating gravity? 2 Answers
Flip over an object (smooth transition) 3 Answers
How To Fix My RayCastHit2D in Unity 4.3.4? 0 Answers
2D floating object 2 Answers
How to make a dynamic bow shot and the arrow to curve and turn correctly when shot? 0 Answers