Created
October 1, 2018 17:17
-
-
Save baobao/3216b62d52ccbe858ca62cea4c488031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Shark : MonoBehaviour | |
{ | |
[SerializeField] | |
private Vector3 _dir; | |
public float angle = 5f; | |
public float speed = 0.3f; | |
/// <summary> | |
/// サメオブジェクト | |
/// </summary> | |
[SerializeField] | |
private Player _player; | |
public string PortName = "/dev/cu.usbmodem1412"; | |
private const int BaudRate = 115200; | |
// https://github.com/baobao/SerialPortWrapper/blob/master/SerialPortWrapper.csを使用しています | |
private SerialPortWrapper _serialPortWrapper; | |
void OnEnable() | |
{ | |
if (_serialPortWrapper != null) | |
{ | |
_serialPortWrapper.KillThread(); | |
} | |
_serialPortWrapper = new SerialPortWrapper(PortName, BaudRate); | |
_serialPortWrapper.onMessageCallback = OnMessage; | |
} | |
void OnDisable() | |
{ | |
if (_serialPortWrapper != null) | |
{ | |
_serialPortWrapper.KillThread(); | |
_serialPortWrapper = null; | |
} | |
} | |
void Update() | |
{ | |
_player.transform.Translate(Vector3.forward * speed); | |
var q = _player.transform.localRotation; | |
_player.transform.localRotation = q * Quaternion.Euler(_dir * angle); | |
} | |
void OnMessage(string msg) | |
{ | |
if (string.IsNullOrEmpty(msg) == false) | |
{ | |
var array = msg.Split('_'); | |
if (array.Length == 3) | |
{ | |
// 1023で割って正規化する | |
_dir = new Vector3( | |
0, | |
float.Parse(array[0]), | |
0 | |
) / 1023f; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment