MRTK thumbstick input - mrtk

I’ve been trying to figure how to access thumb-stick input from the controller in MRTK.
I’m new to coding and the documentation was just a bit too confusing for me to figure out.
I figured out a pointer click through the onPointerClick methods however I just can’t figure the other inputs.
I’m sure it’s simple, I just need to see an example. Any help is appreciated.
Thanks!

You can try a script like this:
public class ThumbstickMover : InputSystemGlobalListener, IMixedRealityInputHandler<Vector2>
{
public MixedRealityInputAction moveAction;
public float speed = 1.0f;
public void OnInputChanged(InputEventData<Vector2> eventData)
{
if (eventData.MixedRealityInputAction == moveAction)
{
Vector3 localDelta = speed * (Vector3)eventData.InputData;
transform.position = transform.position + transform.rotation * localDelta;
}
}
}
For that to work you'll need to set as moveAction an input action that is mapped to one of the thumbsticks. Let me know if you have trouble with that.

Related

How to detect different sounds based on what im walking on. 2D Game

Im really noob at coding, I know the basics, but I want an help to code a script who can detect on which terrain I'm walking on and make different sounds. On the internet there are some tutorials but they are for 3D Games, mine is a 2D Game so it's different. Can you please help me?
I would put the footstep sound in a "wrapper" method, and in that method include arguments/values that indicate the current terrain and manner-of-movement, if they matter.
For example:
playFootstep("grass", "jumping")
or:
playFootstep("gravel", "walking")
Then, in this method, select what sound file to play based on the parameters, and play it.
How do you currently play your footsteps?
Here's my footsteps code:
using UnityEngine;
public class Footsteps : MonoBehaviour
{
[SerializeField]
private AudioClip[] clips;
private AudioSource audioSource;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
}
private void Step()
{
AudioClip clip = GetRandomClip();
audioSource.PlayOneShot(clip);
}
private AudioClip GetRandomClip()
{
return clips[UnityEngine.Random.Range(0, clips.Length)];
}
}

Unity - Detecting keyboard input

I'm fairly new to Unity and have been looking up tons of tutorials/guides online. My issue is that for some reason when I use the below code it doesn't detect if the keyboard is clicked. Maybe I am doing keyboard detection wrong. Here is my code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
Vector3 player = GameObject.FindGameObjectWithTag("Player").transform.position;
void Update () {
if (Input.GetKeyDown(KeyCode.D)) {
player.x += 0.01F;
}
}
}
Your input code is correct but still couple of things that are not at right place. First you wrote an initializer (static method) outside any function. Remember when you do it in Unity3d C# then it will always give you a warning/error.
If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
So first move that sort of lines in either functions.
Second thing you are getting Vector3 and trying to use it as reference, that means you got a position reference in form of Vector3 and every change made in that variable will be effective, that is not the case, it won't.
But yes you can do it by getting Transform or GameObject, they will do it for you.
Third and last thing, you are trying to alter Vector3 component ("x" in your case ) directly, that'd also not acceptable for Unity. What you can do is either assign position with new Vector3 or create a separate Vector3 variable, alter that, then assign it to position.
So after all of these addresses your code should be look like this,
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
Transform player;
// Use this for initialization
void Start ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.D)) {
// Remove one of these two implementations of changing position
// Either
Vector3 newPosition = player.position;
newPosition.x += 0.01f;
player.position = newPosition;
//Or
player.position = new Vector3 (player.position.x + 0.01f, player.position.y, player.position.z);
}
}
}

Changing text field of button in for loop

I'm making a level select screen and I need the text field to display different level numbers for each level. I don't really see what I'm doing wrong here, but I'll go over what I did and post the relevant code.
I have a button class (linked) and inside the symbol I have a dynamic text field. I have two classes of relevance, LevelSelectScreen and LevelSelectButtons (pretty self-explanatory what they are). I thought it would be really easy to change the text if I did it inside the LevelSelectButtons class, by simply doing levelText.text = "Wanted Text", where levelText is the given instance name for my button (just a text field on top of my graphic for the button). Unfortunately, this gives the oh so common and annoying error: TypeError: Error #1009: Cannot access a property or method of a null object reference.
I tried doing virtually the same thing in my LevelSelectScreen class during my loop, but I got the same error. Help on how to get this levelText to work is greatly appreciated! Here is the relevant code.
LevelSelectScreen
public class LevelSelectButtons extends SimpleButton {
public var levelNumber:int;
public var levelSelectScreen:LevelSelectScreen;
public function LevelSelectButtons(i) {
x = 200;
y = 100 + 50*i;
addEventListener(MouseEvent.CLICK,LevelSelectClicked,false,0,true)
levelNumber = i;
levelText.text = "Level" + i;
}
}
LevelSelectScreen
public class LevelSelectScreen extends MovieClip {
public var levelSelectButtons:LevelSelectButtons;
public var mainMenuButton:MainMenuButton;
public function LevelSelectScreen() {
for (var i:int = 1; i<=2; i++)
{
levelSelectButtons = new LevelSelectButtons(i);
addChild(levelSelectButtons);
}
}
}
You can't have a dynamic text field in a SimpleButton.
Annoying, I know.
Simple fix would be to have LevelSelectButton wrap a SimpleButton instead of extend it. Then your text field would be inside LevelSelectButton on top of a text-less SimpleButton. (Be sure to set mouseEnabled to false on the text field so it doesn't interfere with mouse events on the SimpleButton.
A more complex option would be to write your own custom button class.
It's not actually that difficult, but might be overkill for what you're trying to do here.
its because you haven't declared levelText variable and you are trying to access it, therefor Cannot access a property or method of a null object reference.

Generic used in conjunction with Sort(Comparison<T>)

Im wondering if its possible to reuse my overload of the Sort(Comparison) method to sort both labels and textboxes by tabIndex. Ive already tried and i couldnt get it to work. Any help would be appreciated.
foreach(Control control in gbUserInputs.Controls)
{
if (control is Label)
{
inputLabels.Add((Label)control);
}
if (control is TextBox)
{
inputTxtboxes.Add((TextBox)control);
}
}
Sort method call(this doesnt work).
inputLabels.Sort(sortMyInputs<Label>);
Overload of sort method.
private static int sortMyInputs<T>(T entry1, T entry2)
{
return entry1.TabIndex.CompareTo(entry2.TabIndex);
}
You shouldn't be making a generic method:
private static int CompareLabels(Label entry1, Label entry2)
{
return entry1.TabIndex.CompareTo(entry2.TabIndex);
}
The point of a generic delegate is to allow it to hold methods of different concrete types; not to allow it to hold methods that are themselves generic.
If you want to reuse your method, you can modify it to take Control (which both TextBox and Label inherit); you would still be able to pass it to List<Label>.Sort because of delegate covariance.
If you're using .Net < 4, which doesn't have delegate covariance, you can do it your way by adding a constraint to the method so that it knows what T can be:
private static int CompareLabels<T>(T entry1, T entry2) where T : Control
{
return entry1.TabIndex.CompareTo(entry2.TabIndex);
}
You can also simply replace all of your code with one line of LINQ:
inputLabels = gbUserInputs.Controls.OfType<Label>()
.OrderBy(c => c.TabIndex)
.ToList();

Using an introduced member?

I've been reading up on PostSharp, and I want to use introduction to inject a member. For a stupid example, say I write an aspect that introduces
public Guid Id = Guid.NewGuid();
Easy enough, if i disassemble, I see its there. Now the tricky part.. within the same class, I'd like to do something like;
public void PrintGuid()
{
Console.WriteLine(this.Id);
}
How would I do this? Alternately, if that doesn't work, can I do something like declare the variable in the class, and then use it in the aspect? So the class would have
public Guid Id;
And the aspect would do something like
this.Id = Guid.NewGuid();
You can see how to do it here
http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-14-e28093-Introducing-members-and-interfaces-Part-1.aspx
http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-15-e28093-Introducing-members-and-interfaces-Part-2.aspx
If you need to use it at design time then you probably shouldn't be introducing it at compile time. Either change it to be there at design time or you can abstract an interface and use Post.Cast<>() to take advantage of it
var MyClass = Post.Cast<IMyInterface>(...);
MyClass.MyIntroducedMember = 10;

Resources