Unity-連接Arduino+碰撞事件
- 先在Arduino端設置四顆按鈕,並在按下後傳送1、2、3、4,分別控制物體的上下左右
- Serial.write(3);
- Serial.flush();
- delay(20);
- file->Build and Setting->Player Setting->API Compatibility Level->.NET 2.0
- Assets->create->c#
- using System.IO.Ports
- SerialPort sp = new SerialPort("COM7",9600);
- 在Start function 中 開啟sp->sp.Open()、sp.ReadTimeout = 1
- 在updata function 中 先確認sp是否正確->sp.IsOpen(true/false)-->try and catch
- 改變object的位置->transform.Translate(Vector3.left*speed,Space.World)
- 利用Vector3的left、reght、forward、back、up、down來改變三軸在空間中的位置
- 在想撞人的物體上加上剛體:component->Physics->Rigidbody
- 撞人和被撞至少有一個開啟Is Trigger才撞得進去:Sphere Collider->Is Trigger
- 當撞到且撞得進去時呼叫:void OnTriggerEnter(Collider other) (有勾Is Trigger)
- 當撞到但撞不進去時呼叫:void OnCollisionEnter(Collision collisionInfo) (都沒勾Is Trigger)
沒有留言:
張貼留言