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 VR: Switching between snap and continuous turn with a dropdown

Discussion in 'Input System' started by SP4ZE, Dec 2, 2022.

  1. SP4ZE

    SP4ZE

    Joined:
    Jul 31, 2022
    Posts:
    6
    Heyy, im trying to code a UI dropdown which will allow player to switch his turning controls from snap to continuous and vice versa.

    I want to achieve this by simply turning off these components in the locomotion but Im not sure how to reference them, I tried, but SetActive didn't seem to work. Would kindly appreciate any info

    Code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.XR.Interaction.Toolkit;

    public class UI_Turning : MonoBehaviour
    {
    public TMPro.TMP_Dropdown dropdown;
    // TRYING TO REFERENCE LOCOMOTION COMPONENTS
    public GameObject locomotion;
    public ActionBasedSnapTurnProvider snapTurn;
    public ActionBasedContinuousTurnProvider continuousTurn;

    void Start()
    {
    snapTurn = locomotion.GetComponent<ActionBasedSnapTurnProvider>();
    continuousTurn = locomotion.GetComponent<ActionBasedContinuousTurnProvider>();
    }

    public void TurningOptions()
    {
    if (dropdown.value == 0)
    {
    // TRYING TO DISABLE / ENABLE COMPONENTS
    Debug.Log("Continuous turning options");
    locomotion.GetComponent<ActionBasedSnapTurnProvider>().enabled = false;
    locomotion.GetComponent<ActionBasedContinuousTurnProvider>().enabled = true;
    }

    if (dropdown.value == 1)
    {
    Debug.Log("Snap turning options");
    locomotion.GetComponent<ActionBasedSnapTurnProvider>().enabled = true;
    locomotion.GetComponent<ActionBasedContinuousTurnProvider>().enabled = false;
    }
    }
    }
     
  2. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    616
    I want to achieve this by simply turning off these components in the locomotion but Im not sure how to reference them, I tried, but SetActive didn't seem to work. Would kindly appreciate any info

    First for readability you really should post code marked as code for example

    Code (CSharp):
    1. void Start()
    2. {
    3. snapTurn = locomotion.GetComponent<ActionBasedSnapTurnProvider>();
    4. continuousTurn = locomotion.GetComponent<ActionBasedContinuousTurnProvider>();
    5. }
    Pardon my saying so but you're all over the map with this. You have variables defined, it looks like you are setting them in Start and then ignoring them from then on. The problem is even if it worked you wouldn't really know why.

    My suggestion is to start slow and build up. For instance you don't need a dropdown to test this out you can simply assign a variable and if the rest of the code works you would see it working. Then you can add in the dropdown code. You should also verify your assumptions so add Debug.Log statements to make sure you found the component and can check it's current enabled setting.

    Meanwhile you should "ideally" create two methods SnapTurnOn and SnapTurnOff for instance. I don't know what locomotion is in your example but I have a feeling it isn't a game object.

    And finally there is a small issue in the Unity code that can cause problems when changing between turn providers so you've picked one that might require slightly more work to get right.
     
    SP4ZE likes this.
  3. SP4ZE

    SP4ZE

    Joined:
    Jul 31, 2022
    Posts:
    6
    Thank you so much! Im sorry for wrong post formatting, im new to this forum, and Unity.

    Locomotion is a preset GameObject in VR that manages movement and rotation of player, you attach different preset component scripts such as SnapTurnProvider these come with Unity's XR Plugin, so you don't code them yourself. All i'm trying to do is disable / enable these components from an external script, hence switching between different rotation controls.

    But i'm guessing im asking the wrong question.
    As essentially what im trying to do is simply disable / enable a script component inside another object from an external script

    I really appreciate your suggestion to test it without the dropdown, but only the code first, I will do that :)
    Thank's a lot again
     
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    616
    Locomotion is a preset GameObject in VR that manages movement and rotation of player, you attach different preset component scripts such as SnapTurnProvider these come with Unity's XR Plugin, so you don't code them yourself.

    I'm not certain that is the case exactly. Do you mean the Locomotion System script? I have it as well obviously but it is the Locomotion providers that you add that control things like Snap Turn and Continuous Turn.

    As essentially what im trying to do is simply disable / enable a script component inside another object from an external script

    Not a problem I went through the same trials. You need a reference to each of the turn providers which you have defined. I don't know that you can get to them through the locomotion object. You can get to them from your XR Rig (or XR Origin). But if it worked then you would see in a couple of log lines that your references are not null.

    In your TurningOptions method you are not using the references that you got already but are attempting to get them again. The point of getting them in Start is to eliminate the need to do this each time. And setting the enabled property should be enough but "there is an issue" with these particular providers depending upon which actions you have set for operating the turns. Hard to explain but there is a thread in the bugs that goes over it. So if you get the code working but it crashes every so often that's why. There is a workaround, find the thread and see if it makes sense.

    I always suggest simplifying new code to only what is essential along with using log statements to make sure your values are what you think they are. When it works you can add additional layers of code.