It was time to move on to the next source of force in the simulation, pulling the sphere body toward the midpoint between a player’s moving hands. Unfortunately, I broke one of my only rules for this dev log: I wrote code before planning out the algorithm. I figured I’d look at my original project script and roughly copy it, but quickly realized that my old code was janky. It worked, but I honestly attribute part of its success to luck with tweaking the many parameters I used. For example, take a look at this snippet from the old script:

float loc = Vector3.Distance(midPointObj.transform.position, emitterTargetObj.transform.position);
float dist = Vector3.Distance(leftEmitterObj.transform.position, rightEmitterObj.transform.position);
dist = dist / 2f;
 
float relativeDistance = Mathf.InverseLerp(0, dist, loc);
relativeDistance = moveTowardsCurve.Evaluate(relativeDistance);
 
float step = relativeDistance * Time.deltaTime * pushSpeed;
 
emitterTargetObj.GetComponent<Rigidbody>().position = Vector3.MoveTowards(
	emitterTargetObj.transform.position, midPointObj.transform.position, step
);

Why should the distance between the hands determine the force with which the body gets pushed to the midpoint? Your guess is as good as mine, but hey, it worked. Too bad I set the position directly though, or else I may have reused it…

It took a while to straighten things out. I originally tried mimicking the gravity force algorithm I used with the body-on-body attraction, but I had issues with the body getting stuck in orbit without ever re-approaching the midpoint. The inherent problem with gravity is that it weakens the further away two bodies get from one another. Therefore, once the body escaped orbit, there was no bringing it back.

I decided to instead rely on pushing the body to the midpoint using force directed toward the midpoint with drag that increased with proximity to the midpoint. The results are very satisfying.

I’ll proudly admit that this idea was not my own, but instead had been recommended to me in my old forum thread regarding object attraction. The drag solution works in this case since I don’t utilize drag for the body-on-body gravity attraction. If I end up needing to apply multiple drag forces to a body down the line, I’m confident I can figure that out using kinematics equations. Unlike the gravity, drag force directly counters motion.

I wrote my hand pushing force algorithm inside my player controller. While that seems ok, I think I’m better off having a global scene controller script manage the forces on each player. That’s part of my plan for tomorrow, as is testing my hand force against other gravity forces (there’s a reason I called this scene “Multiple Forces”). Also on the agenda: scaling the player according to the rules created in my 12/29/23 entry. Pretty soon, I’ll get to test this all on my Kinect!


Tags: gamedev unity physics scripting 3d kinect