Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Animation dosent work in VR-Game

Discussion in 'Animation' started by unity_IIqD3f_j07YsHA, May 24, 2023.

  1. unity_IIqD3f_j07YsHA

    unity_IIqD3f_j07YsHA

    Joined:
    May 20, 2023
    Posts:
    1
    Hello,
    i am building a small VR-Game and followed this video (
    ) Part 1 to 3.
    Everything works fine but my avatar is only making super small steps and i dont know why. I printed the values of "DirectionY" on the console and it is changing from -1 to 1 dependig how i am moving "CenterEyeAnchor".

    In the Animator-Windows i can see the preview of the animation and everything seems fine. Do you see a problem in the script oder do you think i made a mistake with the animator?

    I am using the OVRCameraRig.
    And i dont use the robot from the video as an avatar. Instead i used a own model in ".fbx"-format

    Thank you for help!


    Script for animation:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class VRAnimatorController : MonoBehaviour
    4. {
    5.     public float speedTreshold = 0.1f;
    6.     [Range(0,1)]
    7.     public float smoothing = 1;
    8.     private Animator animator;
    9.     private Vector3 previousPos;
    10.     private VRRig vrRig;
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         animator = GetComponent<Animator>();
    17.         vrRig = GetComponent<VRRig>();
    18.         previousPos = vrRig.head.vrTar.position;  //vrTarget ersetzt durch vrTar im VRRig-Script
    19.  
    20.      
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         //Compute the speed
    27.         Vector3 headsetSpeed = (vrRig.head.vrTar.position - previousPos) / Time.deltaTime;
    28.         headsetSpeed.y = 0;
    29.         //Local Speed
    30.         Vector3 headsetLocalSpeed = transform.InverseTransformDirection(headsetSpeed);
    31.         previousPos = vrRig.head.vrTar.position;
    32.  
    33.         //Set Animator Values
    34.         float previousDirectionX = animator.GetFloat("DirectionX");
    35.         float previousDirectionY = animator.GetFloat("DirectionY");
    36.  
    37.         print(previousDirectionY);
    38.  
    39.         animator.SetBool("isMoving", headsetLocalSpeed.magnitude > speedTreshold);
    40.         animator.SetFloat("DirectionX", Mathf.Lerp(previousDirectionX, Mathf.Clamp(headsetLocalSpeed.x, -1, 1), smoothing));
    41.         animator.SetFloat("DirectionY", Mathf.Lerp(previousDirectionY, Mathf.Clamp(headsetLocalSpeed.z, -1, 1), smoothing));
    42.      
    43.     }
    44. }

    Script VRRIg:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [System.Serializable]
    4. public class VRMap
    5. {
    6.     public Transform vrTar;
    7.     public Transform rigTar;
    8.     public Vector3 trackPosOff;
    9.     public Vector3 trackRotOff;
    10.  
    11.     public void Map()
    12.     {
    13.         rigTar.position = vrTar.TransformPoint(trackPosOff);
    14.         rigTar.rotation = vrTar.rotation * Quaternion.Euler(trackRotOff);
    15.     }
    16. }
    17.  
    18. public class VRRig : MonoBehaviour
    19. {
    20.     public float turnSmoothness;
    21.     public VRMap head;
    22.     public VRMap left;
    23.     public VRMap right;
    24.  
    25.     public Transform mainC;
    26.     Vector3 headBodOff;
    27.     public Transform VrH;
    28.  
    29.     private void Start()
    30.     {
    31.         headBodOff = transform.position - VrH.position;
    32.     }
    33.  
    34.     private void LateUpdate()
    35.     {
    36.         transform.position = VrH.position + headBodOff;
    37.         transform.forward = Vector3.Lerp(transform.forward,
    38.             Vector3.ProjectOnPlane(mainC.forward, Vector3.up).normalized, Time.deltaTime * turnSmoothness);
    39.  
    40.         head.Map();
    41.         left.Map();
    42.         right.Map();
    43.     }
    44. }