Code example

A very simple Unity example

About the example

This example shows 3 different actions:

  • Transmitting the touchscreen input to move a ship
  • Transmitting the smartphone's orientation
  • Transmitting the smartphone's acceleration

It looks like this

example1

The code

The following is the whole C# code that makes this example work. It is about 114 lines. Most of it is Unity code, the actual Muvslide related code is highlighted and it's only 15 lines. If you are familiar with Unity scripts, only those 15 lines will be new for you.

In the actual Unity project this class is fully commented. Download it from the downloads section.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using UnityEngine;
using ForestIndieGames.Muvslide;

public class MuvslideConnectionManager : MonoBehaviour, IMuvslideObserver
{
    public Transform objectToMove, objectToRotate, objectToRotate2, objectToAccelerate;
    public float movementSpeed = 0.01f, accelerationSpeed = 0.5f;

    private Transform tempParent;
    private Vector3 originalAcceleratedParentPosition;

    private MuvslideConnection connection;
    private MuvslideInput muvslideInput;

    void OnApplicationQuit()
    {
        if (connection != null)
        {
            connection.Close();
            Debug.Log("Ended gracefully");
        }
    }

    void Start()
    {
        connection = new MuvslideConnection(this);
        muvslideInput = connection.GetInputManager();

        originalAcceleratedParentPosition = objectToAccelerate.parent.position;
    }

    public void MuvslideNotify(string message)
    {
        Debug.Log(message);
    }

    void Update()
    {
        ReturnFireballToPillar();

        if (muvslideInput.IsNewMotionAvailable())
        {
            float[] anglesArray = muvslideInput.GetOrientationDegrees();
            Vector3 newOrientation = new Vector3(anglesArray[0], anglesArray[1], anglesArray[2]);

            RotateObjects(newOrientation);
            AccelerateFireball(newOrientation);
        }

        if (muvslideInput.IsNewScrollAvailable())
            MoveShip();
    }

    private void RotateObjects(Vector3 newOrientation)
    {
        objectToRotate.eulerAngles = newOrientation;
        objectToRotate2.eulerAngles = newOrientation;
    }

    private void AccelerateFireball(Vector3 newOrientation)
    {
        objectToAccelerate.parent.eulerAngles = newOrientation;

        float[] accel = muvslideInput.GetAcceleration();
        objectToAccelerate.localPosition = new Vector3(
            -accel[0] * accelerationSpeed,
            -accel[1] * accelerationSpeed,
            -accel[2] * accelerationSpeed);

        tempParent = objectToAccelerate.parent;
        objectToAccelerate.parent = null;
        tempParent.position = objectToAccelerate.position;
        objectToAccelerate.parent = tempParent;
    }

    private void ReturnFireballToPillar()
    {
        if (objectToAccelerate.parent.position != originalAcceleratedParentPosition)
        {
            float distance = Vector3.Distance(objectToAccelerate.parent.position, originalAcceleratedParentPosition);
            objectToAccelerate.parent.position = Vector3.MoveTowards(
                objectToAccelerate.parent.position,
                originalAcceleratedParentPosition,
                distance * Time.deltaTime);
        }
    }

    private void MoveShip()
    {
        float distanceX = -muvslideInput.GetLastScrollDistanceX() * movementSpeed;
        float distanceZ = muvslideInput.GetLastScrollDistanceY() * movementSpeed;
        Vector3 newPosition = new Vector3(
            objectToMove.position.x + distanceX,
            objectToMove.position.y,
            objectToMove.position.z + distanceZ);

        float rotationSpeed = 0.2f;
        Quaternion targetRotation = Quaternion.LookRotation(newPosition - objectToMove.position);
        objectToMove.rotation = Quaternion.Slerp(objectToMove.rotation, targetRotation, rotationSpeed);

        objectToMove.position = newPosition;
    }

    public void ListenForMuvslideApp()
    {
        connection.WaitForConnectionFromBroadcast();
    }

    public void SearchForMuvslideApp()
    {
        connection.StartConnection();
    }

}