After a few days of juggling with AddForce and Lerp I am slowly growing desperate…
What I try to accomplish is this:
I have two Gameobjects, let’s call them A and B. I want A to smoothly get attracted AND repelled by B.
I want to be able to move B (simply in the game editor for testing) and want A to actually be able to collide with B (if attraction is on) or being rejected the inverse way.
First I tried with Lerp, that gave me some strange behavior sometimes, when A and B collided. I then tried Hermite from the Wiki Mathfx library and this was just perfect, but I couldn’t do a rejection.
So I tried AddForce. But whatever I do, A never collides with B, it always circles around B in an elliptical way (which seems logical but not what I want.)
In the end I need a solution that would work with many Gameobjects, all attracting and rejecting each other more or less.
I already did what I want with BlitzMax and a 3D library but without any physics engine, all by pure vector math, but it wasn’t machine and time independent. So I thought I could do it nicer with physics in Unity. Is physics the right way or should I better keep working with vectors and coordinates?
Hoping for help…
I don’t quite get what you want to do,
but if you have already done it with just vector manipulation,
why not do that again?
Anything in FixedUpdate() is done at a fixed update rate,
and any movement done in Update() can be made framerate-independent
by multiplying it with Time.deltaTime which is the length of that update in seconds
(I think… or milliseconds or something
check the docs).
Maybe that is what you’re looking for?
What I want to do is a gravitational system including rejection and collision, so not a like a real planetary simulation where the planets won’t collide.
I know I could do it again like I did, but I thought it would be better using the built-in physics engine.
I thought of a possible solution. The problem now is that the velocity leading A away from B is higher than the force from A to B. So, maybe if I add a counterforce in the inverse direction A is moving - the bigger the angle between AB and the moving direction from A compared to B - the higher the force this would help. Like a car that drives to fast to be able to take a curve without crash, it has to break.
Gonna try this…
ok, I thought I had a nice Lerp solution working with Hermite from the Mathfx library, but it doesn’t always work. All of the sudden it breaks and it behaves totally wrong and I can’t figure out why???
Am I missing something? Please help me, before I go insande :shock:
Is this some kind of floating point precision thingy??
import Mathfx;
var force : float = 1000;
var caps : GameObject;
var zero : GameObject;
var closeDistance = 1.0;
var lerp = 1;
function Start() {
}
function FixedUpdate () {
if (caps.transform) {
LerpIt();
}
}
function LerpIt() {
var distance = Vector3.Distance(caps.transform.position, zero.transform.position);
var direction = (zero.transform.position - caps.transform.position );
var directionTrue = Mathf.Sign(force) * (direction.normalized * force * force) / (distance * distance);
print (Vector3.Distance(caps.transform.position, directionTrue));
Debug.DrawRay(caps.transform.position, directionTrue, Color.green);
if (!Mathfx.Approx(caps.transform.position, zero.transform.position, closeDistance)) {
caps.transform.position.x = Mathfx.Hermite(caps.transform.position.x,directionTrue.x, lerp * Time.deltaTime);
caps.transform.position.y = Mathfx.Hermite(caps.transform.position.y, directionTrue.y, lerp * Time.deltaTime);
caps.transform.position.z = Mathfx.Hermite(caps.transform.position.z, directionTrue.z, lerp * Time.deltaTime);
}
yield;
}
I am very much a novice when it comes to scripting, and I usually code in C#, but I could take a look at your project here in an hour or so, if you are interested.
Just zip it and post it.
Also, if you could explain, step by step, what you want to happen in the scene, that would be awesome, because I am still not completely sure about what you are trying to do.
Thanks! Actually you only have to create to GameObjects, one at A(0,0,0) and another at B(-100,0,75) like I did and attach the script at Object B and dragging A in realtime and see what happens. Somehow I think that it’s all about floating point inprecision. My BlitzMax prototype was all with doubles.
Found the problem.
You accelerate the sphere with a constantly growing force, which accelerates it so much, that it passes beyond the target without getting within the stop-range, between 2 physics steps.
So it is never close enough to the target, to actually make the if statement true.
Ehm, yea… Hope I make sense :S
You have to check if the distance is smaller than the closeDistance compared to the direction, and not just distance in itself. On top of that you also have to position the sphere where you want it to be when the statement is true, because it has already passed a few hundred units past the target.
Hmmm, nope 
What I don’t understand why it works for a while and then (mostly when A got near B once) stops working suddenly.
I actually set the distance value to a very high value, so the routine is always executed.
I guess I maybe set up the scene wrong then :S
Nothing difficult to set up, really, use the coordinates I mentioned above and of you go. Fiddle with the force to have a decent velocity and on you go… for a while 
Dunno, will see what the unity gurus come up with? This is all quite depressing 
This is my new version to try to find out if it’s really floating point or not. Dunno, going to cry somewhere in a dark corner…
import Mathfx;
var force : float = 1000;
var caps : GameObject;
var zero : GameObject;
var closeDistance = 1.0;
var lerp =1;
var scale = 100;
function Start() {
}
function FixedUpdate () {
if (caps.transform) {
LerpIt();
}
}
function LerpIt() {
var capsN = caps.transform.position * scale;
var zeroN = zero.transform.position * scale;
var distance = Vector3.Distance(capsN, zeroN);
var direction = (zeroN - capsN );
var directionTrue = Mathf.Sign(force) * ((direction.normalized * force * force) / ((distance * distance)/(scale * scale)));
print (Vector3.Distance(capsN / scale, directionTrue));
Debug.DrawRay(capsN / scale, directionTrue, Color.green);
//if (!Mathfx.Approx(capsN, zeroN, closeDistance)) {
caps.transform.position.x = Mathfx.Hermite(capsN.x,directionTrue.x, lerp * Time.deltaTime) / scale;
caps.transform.position.y = Mathfx.Hermite(capsN.y, directionTrue.y, lerp * Time.deltaTime) / scale;
caps.transform.position.z = Mathfx.Hermite(capsN.z, directionTrue.z, lerp * Time.deltaTime) / scale;
//}
yield;
}
Just to keep it complete I’ll add my gravity script. I don’t know what’s wrong with CapsDiff Funcion. It should draw the direction where the attracted Object is heading too… but it doesn’t work… too…
//import Mathfx;
var force : float;
var caps : GameObject;
var zero : GameObject;
var closeDistance = 100.0;
var magneticStrength =10;
var reflect = 1;
var capsOld : Vector3;
function Start() {
}
function FixedUpdate () {
if (caps.transform) {
ApplyMagnetForce();
}
}
function ApplyMagnetForce() {
var direction = (caps.transform.position - zero.transform.position) * reflect;
var distance = Vector3.Distance(caps.transform.position, zero.transform.position);
var newMagnet = magneticStrength / (distance * distance);
caps.rigidbody.AddForce(direction.normalized * newMagnet);
// Draws direction from A to B
Debug.DrawRay(caps.transform.position, direction.normalized * 10, Color.green);
CapsDiff();
yield;
}
function CapsDiff() {
// Draws Velocity direction
capsOld = caps.transform.position;
yield WaitForSeconds (0.1);
Debug.DrawRay(caps.transform.position, capsOld.normalized * 10 , Color.red);
print(Vector3.Distance(capsOld, caps.transform.position));
}
Well I did as you said, adding 2 spheres to a scene, adding the script, and placing each sphere as each gameobject in the script, and the sphere moving just accelerates towards the target, passes it, and then keeps passin back and forth at a very high speed, but I got it to stop at the sphere when I increased the stop range to 10.
Good morning MartinB 
Are you referring to the last script, the MagneticForce Script? What stop range do you mean? Have you tried moving the target around? It sounds as if my script works for you…?? What OS do you use? I’m on WinXP SVP3.
Bump apology,
Hi there Preludian,
var direction = (caps.transform.position - zero.transform.position) * 1;
var distance = Vector3.Distance(caps.transform.position, zero.transform.position);
var newMagnet = magneticStrength / (distance * distance);
caps.rigidbody.AddForce(direction.normalized * newMagnet);
Had been looking for the effects of this code for a long time now, thanks!