Items with no label
3335 Discussions

SR300 player gesture controller

MMoha17
Novice
3,613 Views

Hi,

I have made a Unity game which uses box colliders and triggers actions received from the SR300, to control the player (thanks to MartyG), however, I would like to improve the smoothness of this control, such that I do not want to use box colliders but just simple swipes to the left, right, forward and backwards to move the player. May I know how I can do that?

I have attached my current controller type below.

Thank you

0 Kudos
26 Replies
idata
Employee
1,240 Views

Hi Supermomo90,

 

 

Thank you for contacting us.

 

 

To be honest, any application using Unity Game Engine is out of our support, so we don't have experience with it. Nevertheless, to help you with your project I have found the documentation of Unity, so I hope you find it helpful. ( https://docs.unity3d.com/Manual/Physics3DReference.html)

 

 

I'm not sure if it is possible to add swipes, but you should look at the documentation to find the way to do this.

 

 

Have a nice day.

 

 

Regards,

 

Leonardo R.
0 Kudos
MMoha17
Novice
1,240 Views

Hi Leonardo R.,

Thank you for the document link.

0 Kudos
MartyG
Honored Contributor III
1,240 Views

Apologies for my delay in responding to this message, as I was giving it some thought. RealSense in Unity can recognise a swipe but only a side swipe, I believe. Also, it is one of the gestures that RealSense has more trouble recognising than others, causing frustration for the user. So I personally would not use swipes.

0 Kudos
MartyG
Honored Contributor III
1,240 Views

I just realised I already built this swipe system in Unity. Make a copy of the rocker selector mechanism you already built for your menu. Add trigger blocks on the left and right side too as well as the up and down triggers already there. Set the selector object's TrackingAction constraints so it can move left and right as well as up and down. Put scripts in the trigger blocks that make something happen when the selector enters the trigger collider.

MMoha17
Novice
1,240 Views

Hi MartyG,

Thank you for the response. I have made 2 selector objects which control the forward/backwards movement and left/right movements. I have made left hand tracking on 1 selector and a right hand tracking on another selector and have restricted their axes. The controls are working. However, when I use the left hand tracking, the selector which uses right hand tracking seems to move too. May I know if there is a way to prevent the interference? Must I calibrate the SR300?

Thank you.

0 Kudos
MartyG
Honored Contributor III
1,240 Views

Please check the 'Index' number that you have given your TrackingAction in the other selector. One should have an index of '0' and the other should have an index of '1' so that they can move independently when both hands are being used. Otherwise, if both selectors are set to '0' then moving one hand will move both of them.

0 Kudos
MMoha17
Novice
1,240 Views

Hi MartyG,

Thank you. I have a question. From the GUI built using box Colliders and selectors, is it possible to make the selector go back to origin when the gesture is lost?

Thank you.

0 Kudos
MartyG
Honored Contributor III
1,240 Views

Yes, it is possible. I built such a reset system myself. Here is the script that I wrote that is placed inside the object that is to have its position reset. It moves the object smoothly back to the origin point instead of making it snap back instantly (a type of motion called a Lerp).

In this line:

transform.position = new Vector3 ((Mathf.Lerp (HeightX, 0f, t)), (Mathf.Lerp (HeightY, 0f, t)), (Mathf.Lerp (HeightZ, 0f, t)));

change '0' for the X, Y and Z axes to whatever coordinate you want the origin point to be. So for example, if you wanted it to return to 50,100, 50 then the equation would look like this:

transform.position = new Vector3 ((Mathf.Lerp (HeightX, 50f, t)), (Mathf.Lerp (HeightY, 100f, t)), (Mathf.Lerp (HeightZ, 50f, t)));

What it is basically doing is saying to look at what the current position of the object is (HeightX, HeightY and HeightZ) and then progressively change the object's position to the target coordinate over the course of 't' seconds of time.

At the end of the script, there is an If condition that states that once the script has been activated (made True) then it can only disable once the X coordinate is less than a certain value. This ensures that the script will not disable until the object has returned to its origin point. You will need to edit the value in this If instruction for your own project's X origin. For instance, in my original script my X origin was '0', so I tell the script to stop if its X coordinate becomes < 0.01 (it is easier to trigger an event when near a target instead of absolutely at that target if the coordinates are continually changing during the reset process).

To activate the script, set up a SendMessageAction component to trigger 'LateUpdate' when the gesture is lost.

Here's the script.

*****************

private float t;

private float HeightX;

private float HeightY;

private float HeightZ;

public void LateUpdate ()

{

this.enabled = true;

HeightX = transform.position.x;

HeightY = transform.position.y;

HeightZ = transform.position.z;

t += 0.01f;

transform.position = new Vector3 ((Mathf.Lerp (HeightX, 0f, t)), (Mathf.Lerp (HeightY, 0f, t)), (Mathf.Lerp (HeightZ, 0f, t)));

if (HeightX < 0.01f) {

this.enabled = false;

}

}

}

0 Kudos
MMoha17
Novice
1,240 Views

Hi MartyG,

I am supposed to add component in my selector object via the Inspector and then add in the SendMessageAction script. After that, I created a C# file and named it Lerp and added that script to the Selector object too. I also have another script which contains all the trigger actions and I have named it FastSpin. In other words, my Selector object has 3 scripts attached to it as shown in the image attached.

May I know where I can add in the 'LateUpdate' trigger component to activate the script?

Thank you.

0 Kudos
MartyG
Honored Contributor III
1,240 Views

You would put the word LateUpdate into the 'Function Name' box instead of the word OnTrigger.

0 Kudos
MMoha17
Novice
1,240 Views
0 Kudos
MMoha17
Novice
1,240 Views

Hi MartyG,

I tried to put Hand Index to 1 for one of the selector and 0 for the other selector but the selector with Hand Index = 0 only moves, the selector with hand index = 1 does not move.

Thank you.

0 Kudos
MartyG
Honored Contributor III
1,240 Views

If you are only using one hand, that is how it is supposed to work. To make the index '1' selector move, you need to put your other hand in front of the camera so that both hands are being used at the same time. One hand moves the index '0' selector and the other hand moves the index '1' selector.

0 Kudos
MMoha17
Novice
1,240 Views

Hi MartyG,

Thanks for the reply. I am having some issues with this control. Basically I have created a GUI with left,right,up and down control and would like to use 2 selectors, 1 selector for the up and down and another selector for the left and right. However, I would like to control 1 selector with the right hand and the other with the left hand. But the gesture dont seem to be smooth.

I would also like to ask if I were to use 1 selector, is there any way I can make the selector to always start from 0,0,0 when the hand is detected? Similar to the Lerp function, but I use Lerp for the end of the gesture. Is there any way to use a similar function to always start the selector from 0,0,0 when the hand is detected?

Thank you.

0 Kudos
MartyG
Honored Contributor III
1,240 Views

I understand your problem. My experience is that two-handed tracking in Unity with TrackingAction is not smooth. If the second hand does not remain in front of the first hand then the camera can lose sight of it after a few seconds and stop tracking it until it recognizes the presence of the second hand again. I've had this issue for years in my own Unity project. The camera is more likely to maintain tracking if you do not put your left and right hand too close together. It works better when the hands are spread apart.

You can set the position to 0,0,0 with the Vector3.Zero instruction,

https://docs.unity3d.com/ScriptReference/Vector3-zero.html Unity - Scripting API: Vector3.zero

0 Kudos
MMoha17
Novice
1,240 Views

Hi MartyG,

Should I create another script for this or can I put it in my Lerp script? I tried to create a new script and add to the object but the selector does not go back to 0,0,0.

Thank you.

0 Kudos
MartyG
Honored Contributor III
1,240 Views

If you wanted the selector to return to 0, 0, 0 at the start of the script then I think I would put it in the Start() section of your Lerp script. That way, the reset to 0, 0, 0 will only be triggered once when the script activates and will not keep triggering multiple times whilst the script is trying to do the Lerp operation.

Edit: I just realized that putting it in Start() may not work if you are using this script with a SendMessageAction gesture trigger, because it would jump straight to the LateUpdate function, or whatever Update() method you have your code in, and never see the Start() function.

Ok, I have a different idea. Put your 0, 0, 0 reset code inside an OnEnable() function. Then when the script is triggered by the SendMessageAction, the script is 'enabled' and the reset should hopefully happen.

0 Kudos
MMoha17
Novice
1,240 Views

Hi MartyG,

The code works now. The selector goes to 0,0,0. However, I just realised that the coordinates are supposed to be (208,-1,0). May I know how I can do it? I am sorry about that.

Edit:

Hi MartyG,

After adding the code below in my Lerp file, I created a SendMessageAction in my inspector and put the Function Name as Lerp (which is my file name. Or am I supposed to put it as OnEnable?). But the the Virutal World Box dimensions have moved down to (0,0,0) instead of the selector (shown in green) only. And I am sorry, I just realised that the coordinates are (208,-1,0) and not (0,0,0). I am sorry.

void OnEnable()

{

transform.position = Vector3.zero;

}

Thank you.

0 Kudos
MartyG
Honored Contributor III
1,240 Views

It's no trouble at all. If you want to use coordinates other than 0, 0, 0 then you can rewrite Vector3.zero as:

transform.position = new Vector3(208, -1, 0);

Remember to insert the word new before Vector3 to make it work.

0 Kudos
MMoha17
Novice
1,206 Views

Hi MartyG,

I just edited my reply just now. I am sorry about that. I have attached my Lerp script below.

Thank you.

0 Kudos
Reply