void OnDrawGizmos() { if (target && head) { Gizmos.color = Color.red; Gizmos.DrawWireSphere(target.position, gizmoSize); Gizmos.DrawLine(AvatarSceneScript.CalculateCentroid(head.transform), target.position); } }
void LateUpdate() { float blend; if (!target) { transform.position = new Vector3(0, 7.5f, 0); transform.rotation = Quaternion.Euler(new Vector3(10, 180, 0)); return; } // calculate centroid Vector3 targetCentroid = AvatarSceneScript.CalculateCentroid(target); Vector3 targetHead = AvatarSceneScript.CalculateCentroid(target.Find("Avatar")); // calculate angle float wantedAngle = angle; //float currentAngle = Quaternion.FromToRotation(target.forward, transform.position - target.position).eulerAngles.y - 180f; //blend = 1f - Mathf.Pow(1f - angleSharpness, Time.deltaTime * angleDamping); //float lerpedAngle = Mathf.LerpAngle(currentAngle, wantedAngle, blend); // TODO: Lerped angle change, con questo codice currentAngle è calcolato // correttamente ma la vibrazione in caso di corsa porta la telecamera // a non avanzare correttamente e bloccarsi ad angolazioni sbagliate. // Possibile soluzione: Lerp pesato in base alla distanza del valore wanted e current! // calculate position Vector3 currentPosition = transform.position; Vector3 wantedPosition = target.position - (Quaternion.Euler(0, target.rotation.y + wantedAngle, 0) * target.forward * distance); blend = 1f - Mathf.Pow(1f - positionSharpness, Time.deltaTime * positionDamping); Vector3 lerpedPosition = Vector3.Lerp(currentPosition, wantedPosition, blend); // calculate height float currentHeight = transform.position.y; float wantedHeight = targetCentroid.y + height; blend = 1f - Mathf.Pow(1f - heightSharpness, Time.deltaTime * heightDamping); float lerpedHeight = Mathf.Lerp(currentHeight, wantedHeight, blend); // move camera to new position Vector3 newPosition; newPosition = lerpedPosition; newPosition.y = lerpedHeight; transform.position = newPosition; // calculate rotation Quaternion currentRotation = transform.rotation; Quaternion wantedRotation; switch (rotationTarget) { default: case RotationTarget.HEAD: wantedRotation = Quaternion.LookRotation(targetHead - transform.position); break; case RotationTarget.UPPER_BODY: wantedRotation = Quaternion.LookRotation((targetHead + targetCentroid) / 2 - transform.position); break; case RotationTarget.HALF_BODY: wantedRotation = Quaternion.LookRotation(targetCentroid - transform.position); break; } float horizontalBlend = 1f - Mathf.Pow(1f - rotationHorizontalSharpness, Time.deltaTime * rotationHorizontalDamping); float verticalBlend = 1f - Mathf.Pow(1f - rotationVerticalSharpness, Time.deltaTime * rotationVerticalDamping); Quaternion lerpedRotation = Quaternion.Euler(new Vector3(Mathf.LerpAngle(currentRotation.eulerAngles.x, wantedRotation.eulerAngles.x, verticalBlend), Mathf.LerpAngle(currentRotation.eulerAngles.y, wantedRotation.eulerAngles.y, horizontalBlend), Mathf.LerpAngle(currentRotation.eulerAngles.z, wantedRotation.eulerAngles.z, verticalBlend))); // rotate camera to lerped rotation Quaternion newRotation; newRotation = lerpedRotation; transform.rotation = newRotation; }
void Update() { SkinnedMeshRenderer blendshapeEyeMesh = null; if (head != null) { blendshapeEyeMesh = head.GetComponent <SkinnedMeshRenderer>(); } if (blendshapeEyeMesh) { if (target) { Transform headTransform = head.transform; headTransform.position = AvatarSceneScript.CalculateCentroid(head.transform); Vector3 newTgt = headTransform.InverseTransformPoint(target.transform.position); float x = newTgt.x * targetMultiplierX; float y = newTgt.y * targetMultiplierY; blendshapeEyeMesh.SetBlendShapeWeight(positiveXAxis, x > 0 ? x * 100f : 0); // +X blendshapeEyeMesh.SetBlendShapeWeight(negativeXAxis, x < 0 ? -x * 100f : 0); // -X blendshapeEyeMesh.SetBlendShapeWeight(positiveYAxis, y > 0 ? y * 100f : 0); // +Y blendshapeEyeMesh.SetBlendShapeWeight(negativeYAxis, y < 0 ? -y * 100f : 0); // -Y } else { blendshapeEyeMesh.SetBlendShapeWeight(positiveXAxis, target2dX > 0 ? target2dX * 100f : 0); // +X blendshapeEyeMesh.SetBlendShapeWeight(negativeXAxis, target2dX < 0 ? -target2dX * 100f : 0); // -X blendshapeEyeMesh.SetBlendShapeWeight(positiveYAxis, target2dY > 0 ? target2dY * 100f : 0); // +Y blendshapeEyeMesh.SetBlendShapeWeight(negativeYAxis, target2dY < 0 ? -target2dY * 100f : 0); // -Y } if (randomEyes) { if (!randomizerTarget) { randomizerTarget = new GameObject("RNDTRG"); randomizerTarget.transform.position = blendshapeEyeMesh.transform.position; target = randomizerTarget.transform; } else { target.position += Random.onUnitSphere * Time.fixedDeltaTime / randomCompressor; } } if (blinkable) { float blinkVal = blendshapeEyeMesh.GetBlendShapeWeight(blinkL); if (Time.time - blinkInterval > minz && !isBlinking && !isBlinkedFull && !isBlinkinged) { minz = Time.time - Time.deltaTime; isBlinking = true; } if (isBlinking) { if (!isBlinkinged) { if (blinkVal < 99 && !isBlinkedFull) { blinkVal = Mathf.Lerp(blinkVal, 100, blinkSpeed); if (blinkVal >= 99) { isBlinkedFull = true; } } if (isBlinkedFull) { blinkVal = Mathf.Lerp(blinkVal, 0.0f, blinkSpeed); if (blinkVal <= 2.0f) { isBlinkinged = true; } } } blendshapeEyeMesh.SetBlendShapeWeight(blinkL, blinkVal); blendshapeEyeMesh.SetBlendShapeWeight(blinkR, blinkVal); if (isBlinkinged) { blendshapeEyeMesh.SetBlendShapeWeight(blinkL, 0); blendshapeEyeMesh.SetBlendShapeWeight(blinkR, 0); isBlinkinged = false; isBlinking = false; isBlinkedFull = false; } } } } }