From pressing the mouse button to Air Tap with Hololens

Hey guys
so I have this Code to change an objects Color when I click on it with my mouse.
It goes like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeColorOnTap : MonoBehaviour
{
public Material[ ] materials;
public Renderer rend;

private int index = 1;

void Start ()
{
rend = GetComponent();
rend.enabled = true;
}

void OnMouseDown()
{
if (materials.Length == 0)
{
return;
}
if (Input.GetMouseButtonDown(0))
{
index += 1;

if (index == materials.Length + 1)
{
index = 1;
}
print(index);

rend.sharedMaterial = materials[index - 1];
}
}
}

I don’t want to change it that much I just want the “OnMouseDown” and “Input.GetMouseButtonDown” to change but I don’t know the exact thing to put in there.

I really need your help.
Best is if you change it to where I only need to copy and paste it in my script.

Thanks guys.

I’m not going to write your code for you. I suggest you look at GestureRecognizer and the TappedEvent it contains.

I just want the line. I already looked at it but I don’t get much out of this.
I just wanna know what I put in instead of the said lines.

Again, I’m not writing your code for you.
Microsoft have even made a tutorial. I find it hard to believe that a quick Google search did not unearth this.
Gestures in Unity

Again… I CAN NOT get my code to work with this… That is why I am posting here to get someone to actually help me and NOT send me to that website again and again.
I don’t want you or anyone to write my fcking code for me. I want an explanation based on a working version of my code on why it is that this (then provided code) is working and what the fck I did wrong in not understanding how to transfer the information put out on the website you posted into my code.
but thanks for nothing…

Also… a “quick google search” brought me to the website you also posted… Call me too dumb or whatever but I can not do this. I want to understand how but on my own I can’t!

I modified your script to trigger the material change on an Air Tap.
This will only work on a HoloLens. Also, it is untested.

As the tutorial describe in detail; you need to create a GestureRecognizer, tell it which event types you are interested in, assign to the relevant delegate on the object and remove it when you are finished.

Also, use code tags when posting code.

using UnityEngine;
using UnityEngine.VR.WSA.Input;

public class ChangeColorOnTap : MonoBehaviour
{
    private GestureRecognizer recognizer;

    public Material[] materials;
    public Renderer rend;

    private int index = 1;

    private void Start()
    {
        recognizer = new GestureRecognizer();
        recognizer.SetRecognizableGestures(GestureSettings.Tap);
        recognizer.TappedEvent += MyTapEventHandler;
        recognizer.StartCapturingGestures();

        rend = GetComponent<Renderer>();
        rend.enabled = true;
    }

    private void OnDestroy()
    {
        recognizer.TappedEvent -= MyTapEventHandler;
    }

    private void MyTapEventHandler(InteractionSourceKind source, int tapCount, Ray headRay)
    {
        if (materials.Length == 0)
        {
            return;
        }

        index += 1;

        if (index == materials.Length + 1)
        {
            index = 1;
        }
        print(index);

        rend.sharedMaterial = materials[index - 1];
    }
}

THANK YOU!!!
Doesn’t quite work yet but I am getting there.

  1. use code tags
  2. post in the correct section
  3. do not ask people to write code or do your work for you. Use unity connect to hire someone if you to, this site is for learning not free labor.

closed.