Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question (VR) How to set player position and orientation on scene load

Discussion in 'VR' started by chrisharrisbosch, Apr 18, 2023.

  1. chrisharrisbosch

    chrisharrisbosch

    Joined:
    Mar 10, 2023
    Posts:
    1
    Hi,



    I am building a virtual tour in VR with Unity and am trying to set the spawn point and orientation when the scene loads. I have scene changing and teleportation working fine withing the scenes but cannot work out how to trigger a teleport and reorient on scene load, or find any game object or coordinates that is the scene spawn point.



    I have followed this tutorial:
    and attempted to copy the code from my scene changing functions in order to trigger the reorient code from the tutorial.



    This is my PlayerController script which is attached to the XR origin (pictured)



    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class PlayerController : MonoBehaviour
    10.  
    11. {
    12.  
    13.     public static PlayerController Instance;
    14.  
    15.  
    16.  
    17.     private void Awake()
    18.  
    19.     {
    20.  
    21.         Instance = this;
    22.  
    23.     }
    24.  
    25.  
    26.  
    27.     [SerializeField] Transform resetTransform;
    28.  
    29.     [SerializeField] GameObject player;
    30.  
    31.     [SerializeField] Camera playerHead;
    32.  
    33.  
    34.  
    35.     [ContextMenu("Reset Position")]
    36.  
    37.  
    38.  
    39.     public void ResetPosition()
    40.  
    41.     {
    42.  
    43.         var rotationAngleY = playerHead.transform.rotation.eulerAngles.y - resetTransform.transform.rotation.eulerAngles.y;
    44.  
    45.         player.transform.Rotate(0, -rotationAngleY,0);
    46.  
    47.  
    48.  
    49.         var distanceDiff = resetTransform.position - playerHead.transform.position;
    50.  
    51.         player.transform.position += distanceDiff;
    52.  
    53.     }
    54.  
    55. }


    And this is my spawn script



    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4.  
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. public class spawn : MonoBehaviour
    10.  
    11. {
    12.  
    13.     void Start(){
    14.  
    15.         ResetPosition();
    16.  
    17.     }
    18.  
    19.  
    20.  
    21.     private void ResetPosition()
    22.  
    23.     {
    24.  
    25.         PlayerController.Instance.ResetPosition();
    26.  
    27.     }
    28.  
    29. }



    When the scene with this script loads, the view is from a point on the map and the head rotation system breaks (view does not move when headset moves)

    Any help or advice on next steps would be greatly appreciated

    Thanks :)
     
    esanuriana08 likes this.