Quiero que Start Flip sea una función async y que me regrese el resultado del giro using…
- User
Quiero que Start Flip sea una función async y que me regrese el resultado del giro
using UnityEngine; using System.Collections;
public class CoinFlip : MonoBehaviour { [SerializeField] private Camera mainCamera; [SerializeField] private float distanceFromCamera = 2f; [SerializeField] private float distanceFromCameraY = -2f; [SerializeField] private float tossHeight = 2f; [SerializeField] private float flipSpeed = 720f; [SerializeField] private float duration = 1.5f; [SerializeField] private float bounceHeight = 0.2f; // Altura máxima del rebote [SerializeField] private float bounceDuration = 0.3f; // Duración del rebote [SerializeField] private float lastFlipTime = 0.6f; // Duración del rebote
private bool isFlipping = false; private Quaternion initialRotation; private Vector3 headsRotation; private Vector3 tailsRotation; void Start() { initialRotation = transform.rotation; headsRotation = initialRotation.eulerAngles; tailsRotation = new Vector3(180, 0, 0); if (mainCamera == null) mainCamera = Camera.main; } public void StartFlip() { if (!isFlipping) { isFlipping = true; StartCoroutine(FlipCoinRoutine()); } } private IEnumerator FlipCoinRoutine() { bool isHeads = Random.Range(0, 2) == 0; PositionCoinInFrontOfCamera(); float elapsed = 0f; float verticalSpeed = 4 * tossHeight / duration; float halfDuration = duration / 2f; Vector3 targetRotation = isHeads ? headsRotation : tailsRotation; Vector3 currentRotation = Vector3.zero; Debug.Log(isHeads); Debug.Log(targetRotation); while (elapsed < duration) { elapsed += Time.unscaledDeltaTime; float progress = elapsed / duration; // Movimiento vertical float yOffset = verticalSpeed * elapsed * (1 - progress); transform.position = mainCamera.transform.position + mainCamera.transform.forward * distanceFromCamera + mainCamera.transform.up * distanceFromCameraY + Vector3.up * yOffset; // Reducción de velocidad de giro y acercamiento a la rotación final float currentFlipSpeed = Mathf.Lerp(flipSpeed, 0, progress); if (progress > lastFlipTime) { // Ajustar targetRotation para ser múltiplo de 360 grados más cerca del estado actual float currentXRotation = currentRotation.x; float targetXRotation = targetRotation.x; float rotationsCompleted = Mathf.Floor(currentXRotation / 360f); float targetAdjustedXRotation = (rotationsCompleted * 360f) + targetXRotation; if (targetAdjustedXRotation < currentXRotation) { targetAdjustedXRotation += 360f; } Debug.Log("Target adjusted X rotation: " + targetAdjustedXRotation); Debug.Log("Current rotation: " + currentRotation); Debug.Log("Progress: " + (progress - lastFlipTime) / (1f - lastFlipTime)); // Interpolar directamente entre ángulos de Euler Vector3 currentEulerAngles = currentRotation; Vector3 newEulerAngles = Vector3.Lerp(currentEulerAngles, new Vector3(targetAdjustedXRotation, 0, 0), (progress - lastFlipTime) / (1f - lastFlipTime)); currentRotation = newEulerAngles; // Asignar la rotación interpolada transform.rotation = Quaternion.Euler(newEulerAngles); } else { transform.Rotate(Vector3.right, currentFlipSpeed * Time.deltaTime, Space.Self); currentRotation += new Vector3(currentFlipSpeed * Time.deltaTime, 0, 0); } yield return null; } // Iniciar el rebote final yield return StartCoroutine(BounceRoutine()); isFlipping = false; - Assistant
- User
UnityException: RandomRangeInt can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
- Assistant
- User
Como econtrar un objeto de manera rapida en Unity
- Assistant