Search Unity

Question Bad rotation in animation(AR application)

Discussion in 'Animation' started by Rountman, Dec 27, 2021.

  1. Rountman

    Rountman

    Joined:
    Jan 24, 2021
    Posts:
    4
    Hi
    I have a problem with the animation not working properly as I would think. Although the animation is performed, the rotations of the object are very small.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5.  
    6. public class MovingScript : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     ARRaycastManager m_RaycastManager;
    10.     [SerializeField]
    11.     GameObject spawnedPrefab;
    12.     List<ARRaycastHit> m_Hits = new List<ARRaycastHit>();
    13.     Vector3 previousPosition;
    14.     Vector3 finalPosition;
    15.     Vector3 previousRotation;
    16.     Vector3 finalRotation;
    17.     float Angle;
    18.     float ratio;
    19.     float duration;
    20.     float multiplier;
    21.  
    22.     void Start()
    23.     {
    24.         previousPosition = spawnedPrefab.transform.position;
    25.         previousRotation = spawnedPrefab.transform.rotation.eulerAngles;
    26.         duration = 0.9f;    //animation duration control
    27.         multiplier = 1 / duration;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         if (Input.touchCount > 0 && spawnedPrefab.transform.position == finalPosition)
    34.         {
    35.             Touch touch = Input.GetTouch(0);
    36.  
    37.             if (touch.phase == TouchPhase.Began)
    38.             {
    39.                 m_RaycastManager.Raycast(touch.position, m_Hits);
    40.  
    41.                 if (m_Hits[0].trackable is ARPlane plane)
    42.                 {
    43.                     previousPosition = finalPosition;
    44.                     previousRotation = finalRotation;
    45.                     ratio = 0;
    46.                    
    47.                     finalPosition = m_Hits[0].pose.position;
    48.                     Angle = Vector3.SignedAngle(previousPosition, finalPosition, Vector3.up);
    49.                     finalRotation = new Vector3(0, Angle + previousRotation.y, 0);
    50.                 }
    51.             }
    52.         }
    53.  
    54.         if (spawnedPrefab.transform.rotation != finalRotation)
    55.         {
    56.             ratio += Time.deltaTime * multiplier;
    57.             spawnedPrefab.transform.rotation = Quaternion.Euler(Vector3.Lerp(previousRotation, finalRotation, ratio));
    58.         }
    59.     }
    60. }
    61.