// Update is called once per frame void Update() { if (penguin.trackingId != null && moveGoal != null) { //Resetting rotation this.transform.rotation = Quaternion.Euler(-8f, 0, 0); //Getting average x of feet and moving penguin to that float averageX = (bodyPartManager.GetPart("footLeft", (ulong)penguin.trackingId).position.x + bodyPartManager.GetPart("footRight", (ulong)penguin.trackingId).position.x) / 2; Vector3 newMoveGoalPos = new Vector3(averageX, bodyPartManager.GetPart("spineBase", (ulong)penguin.trackingId).position.y, bodyPartManager.GetPart("spineBase", (ulong)penguin.trackingId).position.z); if (Vector3.Distance(moveGoal.transform.position, newMoveGoalPos) > 0.5f) { moveGoal.transform.position = Vector3.Lerp(moveGoal.transform.position, newMoveGoalPos, Time.deltaTime * 2); } agent.destination = moveGoal.transform.position; timeEngaged += Time.deltaTime; } else if (penguin.trackingId != null && moveGoal == null) { moveGoal = new GameObject("Move Goal : " + penguin.trackingId); this.transform.position = new Vector3(bodyPartManager.GetPart("spineBase", (ulong)penguin.trackingId).position.x, -5, -45.6f); moveGoal.transform.position = this.transform.position; return; } else if (penguin.trackingId == null && moveGoal != null) { if (agent.transform.position.x >= 0) { moveGoal.transform.position = new Vector3(50f, 0f, -70f); } else { moveGoal.transform.position = new Vector3(-50f, 0f, -70f); } heightManager.child.transform.localPosition = new Vector3(heightManager.child.transform.localPosition.x, 2.76f, heightManager.child.transform.localPosition.z); heightManager.parent.transform.localPosition = new Vector3(heightManager.parent.transform.localPosition.x, 2f, heightManager.parent.transform.localPosition.z); agent.destination = moveGoal.transform.position; this.transform.LookAt(new Vector3(agent.destination.x, agent.destination.y, agent.destination.z), Vector3.up); Destroy(moveGoal); moveGoal = null; if (timeEngaged > 10) { WriteToFile(timeEngaged); } } }
private void OnAnimatorIK() { if (penguin.trackingId != null) { // Arm weights anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, ikWeight); anim.SetIKPositionWeight(AvatarIKGoal.RightHand, ikWeight); // Rotate body based on shoulder location // Mutliplier to scale rotation based on distance from center on x axis float rotationDistanceMultiplier = (Mathf.Abs(bodyPartManager.GetPart("spineBase", (ulong)penguin.trackingId).position.x * 1f) + 1); float absShoulderX = -Mathf.Abs(bodyPartManager.GetPart("shoulderLeft", (ulong)penguin.trackingId).position.x) - Mathf.Abs(bodyPartManager.GetPart("shoulderRight", (ulong)penguin.trackingId).position.x); float absShoulderZ = Mathf.Abs(bodyPartManager.GetPart("shoulderLeft", (ulong)penguin.trackingId).position.z - bodyPartManager.GetPart("shoulderRight", (ulong)penguin.trackingId).position.z) * rotationDistanceMultiplier; float spineAngleRadians = Mathf.Atan(absShoulderX / absShoulderZ); float spineAngle; if (bodyPartManager.GetPart("shoulderLeft", (ulong)penguin.trackingId).position.z > bodyPartManager.GetPart("shoulderRight", (ulong)penguin.trackingId).position.z) { spineAngle = (90 + ((spineAngleRadians) * (180.0f / Mathf.PI))); } else { spineAngle = (270 - ((spineAngleRadians) * (180.0f / Mathf.PI))); } Quaternion hipsRot = anim.GetBoneTransform(HumanBodyBones.Hips).localRotation; newSpineAngle = Mathf.Lerp(newSpineAngle, spineAngle, Time.deltaTime * 10); //anim.SetBoneLocalRotation(HumanBodyBones.Hips, Quaternion.AngleAxis(newSpineAngle, Vector3.up)); anim.SetBoneLocalRotation(HumanBodyBones.Hips, Quaternion.AngleAxis(spineAngle, Vector3.up)); Vector3 spineRotationDir = bodyPartManager.GetPart("head", (ulong)penguin.trackingId).position - bodyPartManager.GetPart("spineBase", (ulong)penguin.trackingId).position; Quaternion spineRot = anim.GetBoneTransform(HumanBodyBones.Spine).localRotation; anim.SetBoneLocalRotation(HumanBodyBones.Spine, Quaternion.FromToRotation(new Vector3(0, 1, 0), (spineRotationDir))); // Leg weights anim.SetIKPositionWeight(AvatarIKGoal.LeftFoot, ikWeight / 8); anim.SetIKPositionWeight(AvatarIKGoal.RightFoot, ikWeight / 8); // TODO scale arm joints relative to average of feet. // Arm goals leftArmPos = Vector3.Lerp(leftArmPos, bodyPartManager.GetPart("handRight", (ulong)penguin.trackingId).position, Time.deltaTime * 10); rightArmPos = Vector3.Lerp(rightArmPos, bodyPartManager.GetPart("handLeft", (ulong)penguin.trackingId).position, Time.deltaTime * 10); anim.SetIKPosition(AvatarIKGoal.LeftHand, leftArmPos); anim.SetIKPosition(AvatarIKGoal.RightHand, rightArmPos); leftLegPos = Vector3.Lerp(leftLegPos, bodyPartManager.GetPart("footRight", (ulong)penguin.trackingId).position, Time.deltaTime * 10); rightLegPos = Vector3.Lerp(rightLegPos, bodyPartManager.GetPart("footLeft", (ulong)penguin.trackingId).position, Time.deltaTime * 10); // Leg goals anim.SetIKPosition(AvatarIKGoal.LeftFoot, leftLegPos); anim.SetIKPosition(AvatarIKGoal.RightFoot, rightLegPos); } }