Hi. I am trying to do something similar in Ico where the male character holds the hand of the female character occasionally through the game.
Point A = Shoulder of Character 1
Point B = Shoulder of Character 2
Point C = Where the hands of Character 1 and 2 will join
Trigwise, I know Point A and Point B, I know the distance between them, I know that the angles of line BC and AC will be opposites of each other, but I do not know how to compute the world space coordinates of Point C.
Can somebody help me script how to find Point C?
Thank you in advance.
EDIT: There are constraints to limit the range of points.
- This will be used in conjunction with IKTargets and IKHints on both characters. So the point of intersection, Point C, will always be below points A and B. I note that the Y axis coordinate will go down as the gap between the characters narrow and will go up as the distance widens while they are still “holding hands” but will still always be below Points A and B.
- The angles on the shoulder of Character 1 and that of Character 2 will be opposites of each other (one character is slightly in front of the other). Another way to say this is that if I draw a line straight up from Point C so that it intersects Line AB, it will dissect that line into two equal lengths.
- The rotation of the elbows will be disregarded, i.e., not needed in computing Point C, although Unity’s IK system will calculate these back into the animation to give a natural look. This will look good enough for my purpose.
Thanks
I figured it out after walking away from the problem for a week.
The answer is a simple application of the pythagorean theorem
// get the length from midpoint to shoulder of player
float tempB = Vector3.Distance (new Vector3 (transform.position.x, 1.5f, transform.position.z), middlePoint);
// get the length of the arm - used as hypotenuse
float tempC = 0.75f;
// compute for the length from midpoint going down to hand using pythagorean theorem
float tempA = Mathf.Sqrt( (tempC * tempC) - (tempB * tempB));
// now we can get the point in 3D space by adjusting the midpoint
handHoldingPoint = new Vector3 (middlePoint.x, middlePoint.y - tempA, middlePoint.z);
Hope that this will be helpful for anyone who may have had the same problem. Here is a screenshot of the effect.
Given A and B, there exists an infinite amount of possible positions C if C should be be constrained to a fixed distance d such that |AC| = |BC|= d.
Why?
Because the hands can join at any point in a circle between A and B that satisfies the constraint. If we stand next to each other and hold each others hands, arms straight, we can spin them around in circles and every point is valid. If you are taller than me, then there may be ranges where I can’t reach or you can’t reach, without jumping or crouching. So you have to figure out which of the points C you want to use.
One way to join hands you may have not expected. Up:
Or just joining hands, pointing downward, forward or behind.
One simple solution to this is to cutscene/animate the character into position and just play an animation where they hold hands.