- Home /
I'm interpolating an object between positions and rotations. Now I need to make sure it happens in front of the camera.
So, I have a melee attack script, which interpolates the weapon between positions and rotations to create the melee attack motion.
I have the weapon as a child of "Hands" which is an object positioned sensibly for things being held.
"Hands" is parented to my player, so it stays in front of the player
I want the hands to stay in front of the torso, but I can't parent them to it, so I have an object parented to the torso, and I'm scripting the "Hands" position and rotation to match that.
I'm using animator hints to make the torso and head look up and down with the camera
But when I run my interpolation function, to "swing" the object based on predetermined positions, those positions are happening in local player space, as opposed to, well, local "torso" space
Now I basically want the behavior of, those local positions being applied as if they were local positions of the torso object, as opposed to the player (which it must be parented to.)
I was thinking things like "torsoObject.position + the melee interpolation step position" but while it was consistently affecting it, it wasn't doing what I needed it to, just for example of what I'm trying to do.
Here is a video to show the problem: https://youtu.be/E0lXQR518kk
Note that I am successfully keeping the weapon within view of the camera, until I attack. Then, it is using positions which were defined as local positions, as a child of the player object. I need those positions to change as if they were defined as local to the torso object (the one it copies the transform of until we attack).
Answer by Eno-Khaon · Apr 09, 2018 at 12:51 AM
As long as you know your "target" positions relative to the torso (which you mention having a series of them to interpolate between, you should be able to use Transform.TransformPoint(Vector3) to translate from the torso-based position to the world-space position suited to it.
I am sure this is my answer, but I really need help wrapping my head around it.
Here is the hierarchy:
Player
^}Hands Object (Has a script to copy "Hands Position" transform, only while not attacking)
^}Skeleton
^^}Torso
^^^}}Hands Position
The positions I have an array of, were made local to the player, without considering I would want to perform these "swings" relative to the torso.
Since they are localpositions, I set the localPosition of the hands object to move it, lerping from position to position, resulting in the position being relative to the player, since of course it is a child of the player.
I need it to be as if they are relative to the "Hands Position". I can't simply parent it because of issues with scale and logic.
So, where do I call this Transform.TransformPoint? Do I run that before interpolating the hands, giving it the torso objects vector? Or what is the order of operations here?
I'm doing "hands.localPosition= Vector3.Lerp(posA, posB, t)";
where posA and posB are both local positions, relative to the player object, that I need to be relative to the torso object.
Please give an example of how to use this!
I made it work! This is specific to my use but sharing how I did it.
int i = 0;
if(calc){
foreach (Things.$$anonymous$$eleeAttack atk in dirs) {
int b = 0;
foreach (Things.moveStep step in atk.steps){
//Take the vector the hands would be at locally to the player, turn that vector into a world point.
atk.steps [b].pos = randyHands.TransformPoint (step.pos);
//Take that world point, convert it into where the hands should be relative to the torso object.
atk.steps [b].pos = movesWithTorso.InverseTransformPoint (atk.steps [b].pos);
b++;
}
i++;
}
}
Then I just copied the list during run-time and pasted the component values into the script those vectors are stored in. Worked like a charm.