First you have to pick it up.
And you can with my character… in most situations. There’s a bug. Take a look at the picture below.
I have it scripted so when you click on a book nearby, the character moves into position and picks up the book at it’s exact location. The character lifts or lowers his arm at the shoulder, and either bends or extends his elbow, depending on the location of the book. If the book is low he will also lean forward.
The problem is with the shoulder angle controls. Here is the code:
var bookDir = book.position - shoulder.position;
var handDir = handDummy.position - shoulder.position;
var up = shoulder.up;
var targetBookAngle = Vector3.Angle(bookDir, up);
var handAngle = Vector3.Angle(handDir, up);
var shoulderAngle = handAngle - targetBookAngle;
if(shoulderAngle < -1.0) { // Lower Arm
if (armLift / animSpeed < 2) {
armLift += 1;
atHeight = false;
}
} else
if(shoulderAngle > 1.0) { // Lift Arm
if (armLift / animSpeed > 0.00) {
armLift -= 1 * Time.deltaTime * 60;
atHeight = false;
}
}else atHeight = true; // On Target
}
The idea works: I find the angle from the shoulder to the book (Angle A, “targetBookAngle”), then from the shooulder to the hand (Angle B, “handAngle”), to figure out their difference (Angle C, “shoulderAngle”). If the result (shoulderAngle) is positive, he lowers his arm; if the result is negative, he lifts it.
Usually this works, but the angle isn’t being read right somehow. If books are too high, he lowers his arm and never gets there. Reading Angle A, “targetBookAngle”, books that are low have a low angle, medium height gives a medium angle, but high books give a low angle also.
Moving from degrees to eulers to quaternarions is very slippery and I’m going wrong somewhere.
Any help is greatly appreciated!