- Home /
Add force at position Question
I am currently working on a script for simple water buoyancy for a college project and I almost have everything working as I want it to, however after using Add force at position (which gives the exact effect I am after) To rotate around the X and Z axis they also seem to be affecting the Y axis rotation. I don't know if this is un-expected behavior or not as I have never used Add force at position before, if it isn't un-expected then how do I fix it, if it is un-expected, what is causing it?
The code below is what I have so far, it is applied to all Water objects and affects any object it comes into contact with that has a rigid-body after reading each objects variables from varass (posted below)
WaterScript
var colliders = new Array();
//Add/Remove any objects to the array that enter/leave the water
function OnTriggerEnter (other : Collider)
{
colliders.Add(other.gameObject);
}
function OnTriggerExit (other : Collider)
{
other.rigidbody.drag=0;
other.rigidbody.angularDrag=0.05;
colliders.remove(other.gameObject);
}
function FixedUpdate()
{
for(var hit in colliders)
{
if(hit.rigidbody)
{
//Set the Various Stats, calculated and read from varass, to each object
var waterLevel = transform.position.y;
var floatStats = hit.collider.gameObject.GetComponent("varass") as varass;
var floatForce = floatStats.bouyancy+((waterLevel-hit.transform.position.y)*5);
var returnSpeedX=((floatStats.IXPos-hit.rigidbody.rotation.x));
var returnSpeedZ=((floatStats.IZPos-hit.rigidbody.rotation.z));
hit.rigidbody.drag=1;
hit.rigidbody.angularDrag=1;
//Apply current and float force as well as 'righting' forces
hit.rigidbody.AddForce (floatStats.xCurrent, floatForce, floatStats.zCurrent);
hit.rigidbody.AddForceAtPosition(Vector3(returnSpeedX,0,0), Vector3(transform.position.x, transform.position.y, transform.position.z));
hit.rigidbody.AddForceAtPosition(Vector3(0,0,returnSpeedZ), Vector3(transform.position.x, transform.position.y, transform.position.z));
}
}
}
varass
var bouyancy=50;
var IXPos=0;
var IYPos=0;
var IZPos=0;
Thanks, Tom.
Are the objects using simple colliders (sphere, cube...)? If they're using anything oddly shaped, (mesh, compound) the centerOf$$anonymous$$ass
wouldn't be at (0,0,0), giving the wobble.
PS this
Vector3(transform.position.x, transform.position.y, transform.position.z)
is redundant, just do this
transform.position
Answer by Fattie · Mar 17, 2013 at 02:34 PM
(*) could your problem relate to using local not world positions (or vice versa)
(*) just FTR you are familiar with adding torque (rotational force) .. sometimes this is relevant
() the behaviour you describe is certainly not* unexpected. if you push it somewhere it's very likely to swing around on all axis, not just flat
(*) are you familiar with veery simply clamping the object, look at the rigidbody in Inspector, you can "lock" rotation on various axes, try it out
Note that you can do that in code .. you may need to clamp one axis in one situation, and a different axis at another time in a different situation
hope something here helps
Hey thanks for the answer, the rotation locking seems to be working as a temporary fix but I will be needing to change the Y rotation seperatley for some objects at a later date (for example a boat will turn to face the direction it is travelling in) will this be possible?
As for using local/world positions, how do I deter$$anonymous$$e which is being used, from it's behavior I think local rotation is being used, but I can't be certain.
Finally adding just torque didn't seem to feel as realistic as adding a force at a certain position and caused behavior I could't explain.
Thanks again for your answer.
To be honest I think your full problem is literally "WITH PHYSICS" ... what I mean is, with real world physics.
Like, how (as in "how the hell !!!") would you ACTUALLY push a boat to make it move STRICTLY, ONLY in one place (y axis presumably).
It would be almost impossible if not literally impossible (I have considerable experience with real world boats, and, stuffs like aircraft computer engineering, as well as video games! heh)
Regarding using torque .. you say it doesn't "feel realistic". Then perhaps you just need to change how you are using torque (ie, over which frames do you do it and how much)
All the world's video games work by adding torque you know? So it's probably in tehre somewhere.
iuf you say you want to change the rotation on other axis at different times, then. clamp those axis IN YOUR COD$$anonymous$$ I will add that to the answer, see edit
I'll look into these further suggestions, Thanks again for your help
nothing makes me happier than working on these issues
ultimately if you are doing a $$anonymous$$ASSIVE simulation of a boat in water, you will have to move to "multi-physics" engine like bullet (google)
you can achieve FANTASTIC boats in water though, using PhysX (ie, Unity3D)
waiting for more questions
To be honest I am incredibly drunk at the moment and don't entirely know what we're discussing, but truly little gives me more satisfaction than helping. Let's help!!
Very often for boat rigs you use SPRINGS, I encourage you to look in to them on unity.
Very often you have "objects inside other objects". You might have an "invisible" (so to speak) object that is your "platonic boat" You with me?
it actually stays ALWAYS UPRIGHT AND SQUAR$$anonymous$$ ok
inside that you have your "more physical boat". it is on springs. it can roll and pitch a bit. .. ON THE SRPINGS, you see ?
making "mixed" rigs like this is always a hassle as it's uncdocumented ("should the rigidbody be on the wrapper? or on the inside one?" I never remember and just try things until it works)
but anyway this is a very common approach to things like boat rigs. there's no reason you couldn't experiment a little with this. it can work INCREDIBLY, you just move the "wrapper" around (with a normal push), and then everything "bounces around" on the "actual boat" .. it's like magic
Furthermore:
you see how you're "pushing" the boat around ("force applied using ForceAtPosition")
You know what can be absolutely amazing. make a "thumper" object. think in real world terms it would be like a "missile" maybe 1m long and 100 or 200 kg. it s invisible. You have to be able to quickly position it say 50cm off the surface of your yacht, and correctly oriented. Then give it a velocity. next you have to very carefully get rid of it after it applies to the yacht. This can give an incredibly realistic effect, it's great.
boats are really tricky and you can need a lot of tricks to make 'em work