Getting Started with magic leap-I want to receive touchpad input
Introduction
I will follow the tutorial of C # beginner and unity beginner magic leap, and make a note of what I investigated at that time. Therefore, please note that the content of the basic grammar and the content of the script structure itself are mixed.
What this is what you want to do
The magic leap controller has a touchpad that can receive its input. Let’s take this input.
Preparation with unity editor
Preparation with the unity editor can be done without problems if you follow 2.3 Touchpad Gesture Unity. did.
Write a C # script
I referred to 2.4 Touchpad Gestures –Unity. I will read this content.
TouchpadGestures.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.MagicLeap;
public class TouchpadGestures : MonoBehaviour {
#region Public Variables
public Text typeText, stateText, directionText;
public Camera Camera;
#endregion
#region Private Variables
private MLInput.Controller _controller;
#endregion
#region Unity Methods
void Start() {
MLInput.Start();
_controller = MLInput.GetController(MLInput.Hand.Left);
}
void OnDestroy() {
MLInput.Stop();
}
void Update() {
updateTransform();
updateGestureText();
}
#endregion
#region Private Methods
void updateGestureText() {
string gestureType = _controller.CurrentTouchpadGesture.Type.ToString();
string gestureState = _controller.TouchpadGestureState.ToString();
string gestureDirection = _controller.CurrentTouchpadGesture.Direction.ToString();
typeText.text = "Type: " + gestureType;
stateText.text = "State: " + gestureState;
directionText.text = "Direction: " + gestureDirection;
}
void updateTransform() {
float speed = Time.deltaTime * 5.0f;
Vector3 pos = Camera.transform.position + Camera.transform.forward;
gameObject.transform.position = Vector3.SlerpUnclamped(gameObject.transform.position, pos, speed);
Quaternion rot = Quaternion.LookRotation(gameObject.transform.position - Camera.transform.position);
gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, rot, speed);
}
#endregion
}
I would like to make notes from the most rudimentary ones as possible.
using ○○
I think this is similar to the library header in C. By declaring a specific one, you can declare the class that belongs to it.
#region Public Variables~#endregion
This is the code to improve the readability of the notation. You can now collapse the enclosed area on the IDE.
public Text typeText, stateText, directionText;
It is a class that stores a character string for displaying the state input to the touchpad, which is declared by public from the outside of the script. You can use it by declaring ʻusing UnityEngine.UI`. This allows you to register a text game object.
public Camera Camera;
This class acquires information about the camera. It takes some effort to get a normal game object from a script, but this makes the camera easier to operate.
private MLInput.Controller _controller;
This is a class for acquiring controller information. Details will be written as soon as they are investigated.
void start()
The Start method is special and is only executed once at the beginning when this script is executed.
MLInput.Start();
This will start inputting magic leap.
_controller = MLInput.GetController(MLInput.Hand.Left);
This allows you to select the controller on the left to receive input.
void OnDestroy()
This is also a special method like the Start method. It is executed when the game object in which this script is registered is destroyed.
MLInput.Stop();
This completes the magic leap input.
void Update()
This is also a special method, which is executed every time the unity screen is updated.
updateTransform();
I am trying to execute the class declared later in Update. I will also write about the contents here.
float speed = Time.deltaTime * 5.0f;
Time.dealtime
gets the time taken for one play. The time is set by multiplying this by a numerical value.
Vector3 pos = Camera.transform.position + Camera.transform.forward;
A three-dimensional vector is prepared, and “position information advanced by 1 in the z direction with the camera position as the origin” is added to the camera position information. To put it plainly, I got the coordinates that the camera advanced by 1 before.
gameObject.transform.position = Vector3.SlerpUnclamped(gameObject.transform.position, pos, speed);
It seems to be a function that acquires the position information of a game object and complements the sphere between two points. Interpolation is a smooth connection between two points. This time, we are making a way from the position of gameObject to the position of camera stored in pos. Especially this time, since it is spherical interpolation, a path that follows the spherical surface is created instead of a straight line. Understanding Unity Vector, About Linear Interpolation of Vectors I think you can understand more if you read around.
Quaternion rot = Quaternion.LookRotation(gameObject.transform.position - Camera.transform.position);
Quaternion is a class that handles the rotation of game objects. For more details, see “Quaternion Organize! -Handling the Rotation and Posture of 3D Objects Vividly-”. I will. In particular, Quaternion.LookRotation can create a Quaternion to point in a certain direction. Take the direction you want to face the argument. This time, we are pointing the direction of the gameObject as seen from the camera.
There are other ways to turn the game object in a certain direction, but it will suddenly turn in that direction. This way you can slowly turn in that direction.
gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, rot, speed);
This method changes the Quaternion value of gameObject. Take (value before change, value after change, constant) as an argument. This function has the function of bringing the quaternion value before the change to the quaternion value after the change by a constant amount. As for the constant, a value from 0 to 1 can be input, and the closer it is to 1, the closer to the changed Quaternion value, and the closer to 0, the closer to the Quaternion value.
updateGestureText();
As with the above, I try to make ten classes declared later in Update. I will also write about the contents here.
string gestureType = _controller.CurrentTouchpadGesture.Type.ToString();
string is a variable that handles strings. This is provided to display the entered gesture. Tostring
is a function that converts numbers such as int and float to strings. The state is obtained from _controller
that received the input of the controller on the left side and assigned.
I found a conference that seems to be an earlier version of the namespace, so I found “Enum MagicLeapInputControllerTouchpadGestureType ”was used as a reference.
The following two lines have the same content, so they are omitted.
typeText.text = "Type: " + gestureType;
Assign a character string to the text prepared for display on unity. When substituting multiple character strings, you can connect them with +
.
Summary
The above is what I investigated this time. It cannot be proved that the correct information is correct because it is the content examined by an amateur, but I will continue to correct it as soon as I find a part to be corrected. Also, if there are any mistakes in the content, I would appreciate it if you could comment.
Referenced
- Three reasons why you shouldn’t use #region and one exception
- Understand in 3 minutes! How to change the content of Unity text
- [Introduction to Unity] Learn the basics of cameras! Such behavior is surprisingly easy !?
- [Unity] Get button input from controller with Magic Leap
- Unity Script Conference Vector3
- [Unity] Vector and Rotation Memorandum
- Understanding Unity Vector
- About linear interpolation of vectors
- Organize Quaternions! -Handling the rotation and posture of 3D objects vividly-
- Explanation of Quaternion.Slerp ()
- [Unity] Function summary of string string
- Enum MagicLeapInputControllerTouchpadGestureType