Hi. In Unity 2D, I am attempting to create a combat that procedurally generates weapon usage animations. I am currently trying to procedurally generate a weapon swing animation by getting points in an arc, and making an animation that moves along them.
This is the code I use to get the arc points.
Vector3[] GenerateArcPoints(int count, Vector2 startPos, float radius, float arcLength)
{
Vector3[] points = new Vector3[count];
float startAngle = Mathf.Atan2(startPos.y, startPos.x) * Mathf.Rad2Deg;
float angleStep = arcLength / (count - 1);
for (int i = 0; i < count; i++)
{
float angle = startAngle + (i * angleStep);
float x = Mathf.Cos(angle * Mathf.Deg2Rad) * radius;
float y = Mathf.Sin(angle * Mathf.Deg2Rad) * radius;
points[i] = new Vector3(x, y);
}
return points;
}
I place the points from that function into this function (more specifically, the RelativePos inputs) to generate the animations.
public void makeAnimationClip(List<Vector3> L_RelativePos, List<Vector3> R_RelativePos, List<float> L_Length, List<float> R_Length, List<float> timeToTransfer, string animationToOverride, AnimationClip clipToOverride)
{
if(L_RelativePos.Count != R_RelativePos.Count || R_RelativePos.Count != L_Length.Count || L_Length.Count != R_Length.Count || R_Length.Count != timeToTransfer.Count)
{
Debug.LogError("There was an inequality somewhere within the animation position data, check the length of every single parsed array and make sure they are all equal. Logging all array length's for debugging");
Debug.Log("L_RelativePos.count = " + L_RelativePos.Count);
Debug.Log("R_RelativePos.count = " + R_RelativePos.Count);
Debug.Log("L_Length.count = " + L_Length.Count);
Debug.Log("R_Length.count = " + R_Length.Count);
Debug.Log("TimeToTransfer.count = " + timeToTransfer.Count);
return;
}
AnimatorOverrideController AOC = new AnimatorOverrideController(handAnimator.runtimeAnimatorController);
AnimationClip clip = new AnimationClip();
clip.legacy = false;
Keyframe[] leftPositionXKeys = new Keyframe[L_RelativePos.Count + 1];
Keyframe[] leftPositionYKeys = new Keyframe[L_RelativePos.Count + 1];
Keyframe[] rightPositionXKeys = new Keyframe[L_RelativePos.Count + 1];
Keyframe[] rightPositionYKeys = new Keyframe[L_RelativePos.Count + 1];
Keyframe[] rightScaleKeys = new Keyframe[L_RelativePos.Count + 1];
Keyframe[] leftScaleKeys = new Keyframe[L_RelativePos.Count + 1];
leftPositionXKeys[0] = new Keyframe(0f, leftArm.HandPoint.localPosition.x);
leftPositionYKeys[0] = new Keyframe(0f, leftArm.HandPoint.localPosition.y);
rightPositionXKeys[0] = new Keyframe(0f, rightArm.HandPoint.localPosition.x);
rightPositionYKeys[0] = new Keyframe(0f, rightArm.HandPoint.localPosition.y);
rightScaleKeys[0] = new Keyframe(0, rightArm.segmentLengthDefiner.localScale.x);
leftScaleKeys[0] = new Keyframe(0, leftArm.segmentLengthDefiner.localScale.x);
for (int i = 0; i < L_RelativePos.Count; i++)
{
Debug.Log(timeToTransfer[i]);
leftPositionXKeys[i + 1] = new Keyframe(timeToTransfer[i], L_RelativePos[i].x);
leftPositionYKeys[i + 1] = new Keyframe(timeToTransfer[i], L_RelativePos[i].y);
rightPositionXKeys[i + 1] = new Keyframe(timeToTransfer[i], R_RelativePos[i].x);
rightPositionYKeys[i + 1] = new Keyframe(timeToTransfer[i], R_RelativePos[i].y);
rightScaleKeys[i + 1] = new Keyframe(timeToTransfer[i], R_Length[i]);
leftScaleKeys[i + 1] = new Keyframe(timeToTransfer[i], L_Length[i]);
}
AnimationCurve leftPositionXCurve = new AnimationCurve(leftPositionXKeys);
clip.SetCurve("Left Arm/Hand", typeof(Transform), "localPosition.x", leftPositionXCurve);
AnimationCurve leftPositionYCurve = new AnimationCurve(leftPositionYKeys);
clip.SetCurve("Left Arm/Hand", typeof(Transform), "localPosition.y", leftPositionYCurve);
AnimationCurve rightPositionXCurve = new AnimationCurve(rightPositionXKeys);
clip.SetCurve("Right Arm/Hand", typeof(Transform), "localPosition.x", rightPositionXCurve);
AnimationCurve rightPositionYCurve = new AnimationCurve(rightPositionYKeys);
clip.SetCurve("Right Arm/Hand", typeof(Transform), "localPosition.y", rightPositionYCurve);
AnimationCurve rightScaleCurve = new AnimationCurve(rightScaleKeys);
clip.SetCurve("Right Arm/Arm Segment Length Definer", typeof(Transform), "localScale.x", rightScaleCurve);
AnimationCurve leftScaleCurve = new AnimationCurve(leftScaleKeys);
clip.SetCurve("Left Arm/Arm Segment Length Definer", typeof(Transform), "localScale.x", leftScaleCurve);
clipToOverride = clip;
AOC[animationToOverride] = clip;
handAnimator.runtimeAnimatorController = AOC;
handAnimator.Play(animationToOverride);
}
What I am expecting to obtain is an animation where both hands move in a circular motion. However, the animation moves in a line as opposed to a circle.
Have I done something wrong or missed something that results in this linear movement? If you could leave a reply, I would be most grateful.