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;
}
}
}
}