protected override void OnUpdate(float dt) { elapsed += dt; bool isFirstUpdate = elapsed - dt == 0; if (elapsed < aimLogicDuration && !isFirstUpdate) { aimPosition = mapCollider.ClampPositionToGround(target.Position()) + info.Config.offset; float diff = aimPosition.x - aimPivot.x; if (Mathf.Abs(diff) > info.Config.startupMaxDistance) { aimPosition.x = aimPivot.x + info.Config.startupMaxDistance * Mathf.Sign(diff); } #if UNITY_EDITOR if (gizmosAimPosition != null) { gizmosAimPosition.SetPos(aimPosition); textAimPosition.SetPos(aimPosition); } #endif } if (info.Config.startupEnable) { ProcessStartupPhase(dt); } ProcessActivePhase(dt); }
private void ProcessAimPhase(float dt) { if (elapsed >= aimAt && !isAim) { isAim = true; fabrik.enabled = true; axisDirection = ikJoint.rotation * ikJoint.GetComponent <RotationLimit>().axis.normalized; intersectionPosition = CalculateIntersectionPosition(ikJoint.position, axisDirection); aimPivot = mapCollider.ClampPositionToGround(intersectionPosition); ikPosition = intersectionPosition; // fabrik.GetIKSolver().IKPosition = ikPosition; #if UNITY_EDITOR gizmosIkTarget = new SphereShape(0.1f, ikPosition, Color.red, aimDuration); textIkTarget = new TextShape(ikPosition, Color.red, "IK target"); gizmosAnimationIntersection = new SphereShape(0.1f, intersectionPosition, Color.green, aimDuration); textAnimationIntersection = new TextShape(intersectionPosition, Color.green, "Animation intersection"); gizmosClampedAnimationIntersection = new SphereShape(0.1f, intersectionPosition, Color.red, aimDuration); textClampedAnimationIntersection = new TextShape(intersectionPosition, Color.red, "Clamped anim intersection"); gizmosAimLine = new LineShape(ikJoint.position, Color.red, 1, Vector3.up); gizmosAimPosition = new SphereShape(0.1f, aimPosition, Color.red, aimDuration); textAimPosition = new TextShape(aimPosition, Color.red, "Target"); textPhase = new TextShape(caster.Position(), Color.red, "Aiming"); GizmosDrawer.Instance.AddRequest(new DrawRequest(gizmosIkTarget, aimDuration)); GizmosDrawer.Instance.AddRequest(new DrawRequest(textIkTarget, aimDuration)); GizmosDrawer.Instance.AddRequest(new DrawRequest(gizmosAnimationIntersection, aimDuration)); GizmosDrawer.Instance.AddRequest(new DrawRequest(textAnimationIntersection, aimDuration)); GizmosDrawer.Instance.AddRequest(new DrawRequest(gizmosClampedAnimationIntersection, aimDuration)); GizmosDrawer.Instance.AddRequest(new DrawRequest(textClampedAnimationIntersection, aimDuration)); GizmosDrawer.Instance.AddRequest(new DrawRequest(gizmosAimLine, aimDuration)); GizmosDrawer.Instance.AddRequest(new DrawRequest(gizmosAimPosition, aimDuration)); GizmosDrawer.Instance.AddRequest(new DrawRequest(textAimPosition, aimDuration)); GizmosDrawer.Instance.AddRequest(new DrawRequest(textPhase, totalDuration)); #endif } if (isAim && aimElapsed < aimInterpolationDuration) { aimElapsed += dt; float progress = aimElapsed / aimInterpolationDuration; axisDirection = (ikJoint.position - ikJointParent.position).normalized; // DLog.Log("OnUpdate:axisDirection: " + axisDirection.ToPreciseString()); intersectionPosition = CalculateIntersectionPosition(ikJointParent.position, axisDirection); aimPivot = mapCollider.ClampPositionToGround(intersectionPosition); Vector3 aimPos = mapCollider.ClampPositionToGround(target.Position()) + info.Config.offset; if (info.Config.clampAngle) { aimPos = intersectionPosition; } // Vector3 direction = (aimPos - ikJoint.position).normalized; // intersectionPosition = CalculateIntersectionPosition(ikJoint.position, direction); Vector3 clampedIntersection = intersectionPosition.CloneWithNewX(Mathf.Lerp(intersectionPosition.x, aimPos.x, progress)); Vector3 diff = clampedIntersection - ikJointParent.position; ikPosition = ikJointParent.position + diff.normalized * ((ikJoint.position - ikJointParent.position).magnitude + 1f); fabrik.GetIKSolver().IKPosition = ikPosition; #if UNITY_EDITOR gizmosIkTarget.SetPos(ikPosition); textIkTarget.SetPos(ikPosition); gizmosAnimationIntersection.SetPos(intersectionPosition); textAnimationIntersection.SetPos(intersectionPosition); gizmosClampedAnimationIntersection.SetPos(clampedIntersection); textClampedAnimationIntersection.SetPos(clampedIntersection); gizmosAimLine.SetPos(ikJointParent.position); gizmosAimLine.Length = diff.magnitude; gizmosAimLine.Direction = diff; #endif } }