Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
JavedMahmood's avatar
9 years ago

GearVR Touchpad Swipe Problem

Hi you all,

I have a huge problem with my code to communicate with OVRTouchpad.cs and my Script with player, OVRSwipeJVD.cs.
First when I run the game everything is fine and player interact with OVRTouchpad.cs quite nicely with smooth animation, But after losing or winning a battle the game backs to main menu and from there when I load the same or other levels- My player stops all kind of interaction, in fact I got this msg in console.

MissingReferenceException: The object of type 'Animator' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Animator.SetBool (System.String name, Boolean value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/Animation/AnimatorBindings.gen.cs:283)
OVRSwipeJVD.HandleTouchHandler (System.Object sender, System.EventArgs e) (at Assets/BOX_scene/OVRSwipeJVD.cs:34)
OVRTouchpad.HandleInputMouse (UnityEngine.Vector3& move) (at Assets/OVR/Moonlight/Scripts/OVRTouchpad.cs:221)
OVRTouchpad.Update () (at Assets/OVR/Moonlight/Scripts/OVRTouchpad.cs:134)
OVRTouchpadHelper.Update () (at Assets/OVR/Moonlight/Scripts/OVRTouchpad.cs:256)

Below I put my OVRSwipeJVD.cs code

using UnityEngine;
using System;

public class OVRSwipeJVD : MonoBehaviour {
public Animator anim;
public Animation animation;
public int restore_timer=0;

void Start () {
OVRTouchpad.Create();
OVRTouchpad.TouchHandler += HandleTouchHandler;
anim = GetComponent<Animator>();
}

void HandleTouchHandler(object sender, System.EventArgs e)
{
restore_timer++;

OVRTouchpad.TouchArgs touchArgs = (OVRTouchpad.TouchArgs)e;
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.SingleTap)
{
//Debug.Log("SINGLE CLICK\n");

}
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Down)
{
//Debug.Log("DOWN SWIPE\n");
anim.SetBool("Leftblow",true); 
restore_timer = 0;
}
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Up)
{
//Debug.Log("UP SWIPE\n");
anim.SetBool("Rightblow",true);
restore_timer = 0;
}
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Right)
{
//Debug.Log("RIGHT SWIPE\n");
anim.SetBool("JumpBack",true);
restore_timer = 0;
}
if (touchArgs.TouchType == OVRTouchpad.TouchEvent.Left)
{
anim.SetBool("SwingLeftBlow",true);
restore_timer = 0;
}

if(restore_timer==20)
{
anim.SetBool("BackToMain",true);
anim.SetBool("Rightblow",false);
anim.SetBool("Leftblow",false);
anim.SetBool("SwingLeftBlow",false);
anim.SetBool("MagicThrow",false);

// player getting hit
anim.SetInteger("playerhitmax",0);
anim.SetInteger("playerhit1",0);
anim.SetInteger("playerhit2",0);
anim.SetInteger("playerhit3",0);
anim.SetBool("playerhitmid",false);
restore_timer=0;
}
}
}

I am Using the built in OVRTouchpad.cs nothing changed in these.

Can Anyone help me with it?
Thanks in advance, Take care!

1 Reply

Replies have been turned off for this discussion


  • Hi,

    I am currently looking into this and will advise you on what information I find that is applicable to your situation.


    Thanks and I did it! Found it on internet, Just modified a little for my own purpose. If anyone face the similar problem- below is the answer.
    Please do modify for your own purpose. I put my //comments in the code to make it more useful. Thanks 
    imperativity for your kind note.

    using UnityEngine;
    using System;

    namespace VRStandardAssets.Utils
    {
    // This class encapsulates all the input required for most VR games.
    // It has events that can be subscribed to by classes that need specific input.
    // This class must exist in every scene and so can be attached to the main
    // camera for ease.
    public class OVRTouchSwipeJVD : MonoBehaviour
    {
    public Animator anim;
    public Animation animation;
    //Swipe directions
    public enum SwipeDirection
    {
    NONE,
    UP,
    DOWN,
    LEFT,
    RIGHT
    };
    void Start()
    {
    //#if OVR && !UNITY_EDITOR
    //OVRTouchpad.Create();
    //OVRTouchpad.TouchHandler += HandleTouchHandler;
    //#endif
    anim = GetComponent<Animator>();
    }

    public event Action<SwipeDirection> OnSwipe; // Called every frame passing in the swipe, including if there is no swipe.
    public event Action OnClick; // Called when Fire1 is released and it's not a double click.
    public event Action OnDown; // Called when Fire1 is pressed.
    public event Action OnUp; // Called when Fire1 is released.
    public event Action OnDoubleClick; // Called when a double click is detected.
    public event Action OnCancel; // Called when Cancel is pressed.


    [SerializeField] private float m_DoubleClickTime = 0.3f; //The max time allowed between double clicks
    [SerializeField] private float m_SwipeWidth = 0.3f; //The width of a swipe


    private Vector2 m_MouseDownPosition; // The screen position of the mouse when Fire1 is pressed.
    private Vector2 m_MouseUpPosition; // The screen position of the mouse when Fire1 is released.
    private float m_LastMouseUpTime; // The time when Fire1 was last released.
    private float m_LastHorizontalValue; // The previous value of the horizontal axis used to detect keyboard swipes.
    private float m_LastVerticalValue; // The previous value of the vertical axis used to detect keyboard swipes.


    public float DoubleClickTime{ get { return m_DoubleClickTime; } }


    private void Update()
    {
    CheckInput();
    }


    private void CheckInput()
    {
    // Set the default swipe to be none.
    SwipeDirection swipe = SwipeDirection.NONE;

    if (Input.GetButtonDown("Fire1"))
    {
    // When Fire1 is pressed record the position of the mouse.
    m_MouseDownPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

    // If anything has subscribed to OnDown call it.
    if (OnDown != null)
    OnDown();
    }

    // This if statement is to gather information about the mouse when the button is up.
    if (Input.GetButtonUp ("Fire1"))
    {
    // When Fire1 is released record the position of the mouse.
    m_MouseUpPosition = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);

    // Detect the direction between the mouse positions when Fire1 is pressed and released.
    swipe = DetectSwipe ();
    }

    // If there was no swipe this frame from the mouse, check for a keyboard swipe.
    if (swipe == SwipeDirection.NONE)
    swipe = DetectKeyboardEmulatedSwipe();

    // If there are any subscribers to OnSwipe call it passing in the detected swipe.
    if (OnSwipe != null)
    OnSwipe(swipe);

    // This if statement is to trigger events based on the information gathered before.
    if(Input.GetButtonUp ("Fire1"))
    {
    // If anything has subscribed to OnUp call it.
    if (OnUp != null)
    OnUp();

    // If the time between the last release of Fire1 and now is less
    // than the allowed double click time then it's a double click.
    if (Time.time - m_LastMouseUpTime < m_DoubleClickTime)
    {
    // If anything has subscribed to OnDoubleClick call it.
    if (OnDoubleClick != null)
    OnDoubleClick();
    }
    else
    {
    // If it's not a double click, it's a single click.
    // If anything has subscribed to OnClick call it.
    if (OnClick != null)
    OnClick();
    }

    // Record the time when Fire1 is released.
    m_LastMouseUpTime = Time.time;
    }

    // If the Cancel button is pressed and there are subscribers to OnCancel call it.
    if (Input.GetButtonDown("Cancel"))
    {
    if (OnCancel != null)
    OnCancel();
    }
    }


    private SwipeDirection DetectSwipe ()
    {
    // Get the direction from the mouse position when Fire1 is pressed to when it is released.
    Vector2 swipeData = (m_MouseUpPosition - m_MouseDownPosition).normalized;

    // If the direction of the swipe has a small width it is vertical.
    bool swipeIsVertical = Mathf.Abs (swipeData.x) < m_SwipeWidth;

    // If the direction of the swipe has a small height it is horizontal.
    bool swipeIsHorizontal = Mathf.Abs(swipeData.y) < m_SwipeWidth;

    // If the swipe has a positive y component and is vertical the swipe is up.
    if (swipeData.y > 0f && swipeIsVertical)
    anim.SetBool("Leftblow",true);
    //return SwipeDirection.UP;

    // If the swipe has a negative y component and is vertical the swipe is down.
    if (swipeData.y < 0f && swipeIsVertical)
    anim.SetBool("Rightblow",true);
    // return SwipeDirection.DOWN;

    // If the swipe has a positive x component and is horizontal the swipe is right.
    if (swipeData.x > 0f && swipeIsHorizontal)
    anim.SetBool("JumpBack",true);
    // return SwipeDirection.RIGHT;

    // If the swipe has a negative x component and is vertical the swipe is left.
    if (swipeData.x < 0f && swipeIsHorizontal)
    anim.SetBool("SwingLeftBlow",true);
    //return SwipeDirection.LEFT;

    // If the swipe meets none of these requirements there is no swipe.
    return SwipeDirection.NONE;
    }


    private SwipeDirection DetectKeyboardEmulatedSwipe ()
    {
    // Store the values for Horizontal and Vertical axes.
    float horizontal = Input.GetAxis ("Horizontal");
    float vertical = Input.GetAxis ("Vertical");

    // Store whether there was horizontal or vertical input before.
    bool noHorizontalInputPreviously = Mathf.Abs (m_LastHorizontalValue) < float.Epsilon;
    bool noVerticalInputPreviously = Mathf.Abs(m_LastVerticalValue) < float.Epsilon;

    // The last horizontal values are now the current ones.
    m_LastHorizontalValue = horizontal;
    m_LastVerticalValue = vertical;

    // If there is positive vertical input now and previously there wasn't the swipe is up.
    if (vertical > 0f && noVerticalInputPreviously)
    return SwipeDirection.UP;

    // If there is negative vertical input now and previously there wasn't the swipe is down.
    if (vertical < 0f && noVerticalInputPreviously)
    return SwipeDirection.DOWN;

    // If there is positive horizontal input now and previously there wasn't the swipe is right.
    if (horizontal > 0f && noHorizontalInputPreviously)
    return SwipeDirection.RIGHT;

    // If there is negative horizontal input now and previously there wasn't the swipe is left.
    if (horizontal < 0f && noHorizontalInputPreviously)
    return SwipeDirection.LEFT;

    // If the swipe meets none of these requirements there is no swipe.
    return SwipeDirection.NONE;
    }


    private void OnDestroy()
    {
    // Ensure that all events are unsubscribed when this is destroyed.
    OnSwipe = null;
    OnClick = null;
    OnDoubleClick = null;
    OnDown = null;
    OnUp = null;
    }
    }
    }