Blenderでボーンを入れた人のオブジェクトをunityで読み込み、unity-chanのモーションをつけてanimatorで動かしています。
人感センサー(SB412A)で数値を読み取ったら、unity で動かしている人のオブジェクトを動くようにしたいのですが、どうしたらいいかがわからず混乱しています。
C++
1//Arduino側 2void setup() { 3 Serial.begin(115200); 4} 5 6void loop() { 7 int v = analogRead(0); 8 Serial.println(v); 9 delay(100); 10} 11
C#
1 2 3//unity側 4using System.Collections; using System.Collections.Generic; using UnityEngine; 5Animator _animator; 6 7public string portName = “/dev/cu.usbmodem1421"; 8 9public int baudRate = 115200; 10 11private serialPort serialPort_; 12private Thread thread; 13private bool isRunnning_ = false; 14 15private string message_; 16private bool isNewMessageReceived_ = false; 17 18 19void Start () { 20 21 _animator = GetComponent<Animator>(); 22 } 23 24 25public void startAnimation(){ 26 27 if(_animator){ 28 29 _animator.Play("walk"); 30 31 }else{ 32 33 34 public void changeAnimation(){ 35animator.Play("jump00B"); 36 37 } 38 39 40 } 41
通常時(センサーに反応していない時)は歩いていて,センサーに反応したらジャンプするという
コードのつもりです、、、
本当に慣れてないのでコードがめちゃくちゃだと思います、、、
すみませんがどなたかお力を貸していただけないでしょうか・・・。
y_waiwai様 、MasahikoHirata様
本当にご返答ありがとうございます!
MasahikoHirata様のコードをお借りして、実行してみたのですが
なかなか上手くいきません、、。
もう少し調べてみようと思います、、
↓ 現在のコードです。
追記
C++
1//Arduino 2#define in A0 3#define H 1 4#define L 0 5int mode; 6 7void setup() { 8 Serial.begin(115200); 9 pinMode(in, INPUT); 10 mode = L; 11} 12 13void loop() { 14 int status; 15 status = digitalRead(in); 16 if (status == H) { 17 if (mode == L) { 18 mode = H; 19 Serial.print("L"); 20 } else { 21 if (mode == H) { 22 mode = L; 23 Serial.print("H"); 24 } 25 26 } 27 } 28 29}
C#
1//unity側 、ボタンを押すとアニメーションが変わる 2using UnityEngine; 3using System.Collections; 4using System.Collections.Generic; 5 6 7public class animation : MonoBehaviour 8{ 9 10 private Animator animator; 11 12 private const string key_isRun = "isRun"; 13 private const string key_isJump = "isJump"; 14 15 // Use this for initialization 16 17 void Start() 18 { 19 this.animator = GetComponent<Animator>(); 20 } 21 22 // Update is called once per frame 23 void Update() 24 { 25 if (Input.GetKey(KeyCode.H)) 26 { 27 this.animator.SetBool(key_isRun, true); 28 29 30 } 31 else 32 { 33 this.animator.SetBool(key_isRun, false); 34 35 } 36 37 if (Input.GetKey(KeyCode.L)) 38 { 39 40 this.animator.SetBool(key_isJump, true); 41 42 } 43 else 44 { 45 this.animator.SetBool(key_isJump, false); 46 47 48 } 49 } 50}
C#
1//unity側でシリアル通信をするためのコード 2using UnityEngine; 3using System.Collections; 4using System.IO.Ports; 5using System.Threading; 6 7public class SerialHandler : MonoBehaviour 8{ 9 public delegate void SerialDataReceivedEventHandler(string message); 10 public event SerialDataReceivedEventHandler OnDataReceived; 11 12 public string portName = "/dev/cu.usbmodem14141"; 13 public int baudRate = 115200; 14 15 private SerialPort serialPort_; 16 private Thread thread_; 17 private bool isRunning_ = false; 18 19 private string message_; 20 private bool isNewMessageReceived_ = false; 21 22 void Awake() 23 { 24 Open(); 25 } 26 27 void Update() 28 { 29 if (isNewMessageReceived_) 30 { 31 OnDataReceived(message_); 32 } 33 isNewMessageReceived_ = false; 34 } 35 36 void OnDestroy() 37 { 38 Close(); 39 } 40 41 private void Open() 42 { 43 serialPort_ = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One); 44 45 serialPort_.Open(); 46 47 isRunning_ = true; 48 49 thread_ = new Thread(Read); 50 thread_.Start(); 51 } 52 53 private void Close() 54 { 55 isNewMessageReceived_ = false; 56 isRunning_ = false; 57 58 if (thread_ != null && thread_.IsAlive) 59 { 60 thread_.Join(); 61 } 62 63 if (serialPort_ != null && serialPort_.IsOpen) 64 { 65 serialPort_.Close(); 66 serialPort_.Dispose(); 67 } 68 } 69 70 private void Read() 71 { 72 while (isRunning_ && serialPort_ != null && serialPort_.IsOpen) 73 { 74 try 75 { 76 message_ = serialPort_.ReadLine(); 77 isNewMessageReceived_ = true; 78 } 79 catch (System.Exception e) 80 { 81 Debug.LogWarning(e.Message); 82 } 83 } 84 } 85 86 public void Write(string message) 87 { 88 try 89 { 90 serialPort_.Write(message); 91 } 92 catch (System.Exception e) 93 { 94 Debug.LogWarning(e.Message); 95 } 96 } 97} 98 99
unity側でアニメーションを再生するコードとarduinoからの信号を受け取るコードを
別に分けて書きました。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/28 09:48