Cov txheej txheem:

Yooj Yim Kev Sib Koom Yooj Yim: 5 Kauj Ruam
Yooj Yim Kev Sib Koom Yooj Yim: 5 Kauj Ruam

Video: Yooj Yim Kev Sib Koom Yooj Yim: 5 Kauj Ruam

Video: Yooj Yim Kev Sib Koom Yooj Yim: 5 Kauj Ruam
Video: kheev Lam Yooj Yim - Nkauj Ntxhee Xyooj (Official Audio 2023) 2024, Kaum ib hlis
Anonim
Yooj Yim Kev Sib Koom Tes
Yooj Yim Kev Sib Koom Tes

Nqe lus piav qhia

Txoj haujlwm no yog tus tswj hwm yooj yim heev uas tuaj yeem muab tswv yim rau Kev Sib Koom thaum nias cov nyees khawm Kuv tsim nws los tswj cov kev ua si uas kuv tau ua uas cov neeg ua si tuaj yeem mus sab laug thiab sab xis. Tau kawg koj tuaj yeem hloov kho nws rau kev tswj hwm ntau dua. Tsis tas yuav siv lwm lub tsev qiv ntawv ntxiv.

Kauj ruam 1: Cov ntaub ntawv

Cov khoom xav tau

  • -Arduino 2x resistor
  • -[~ 220 Ohm
  • -Cov xov hlau
  • -2x Nyees khawm
  • -Breadboard

Kauj ruam 2: Breadboarding

Breadboarding
Breadboarding

Kuv pib los ntawm kev txuas qhov chaw nres nkoj arduino GND mus rau qhov tsis zoo (-) tus pin nyob rau sab laug ntawm lub khob cij, thiab txuas 5V rau qhov zoo (+) ntawm sab xis ntawm lub khob cij.

Tom qab ntawd kuv muab cov nyees khawm tso rau hauv qhov chaw kuv xav tau mto. Thiab txuas lawv siv nrog cov xov hlau thiab cov tiv thaiv.

Kauj Ruam 3: Coding Arduino

Ua ntej tshaj plaws koj yuav tsum tau sau qee cov cai rau koj Arduino kom khiav. Kuv muab cov lus pom hauv cov cai uas piav qhia nws ua dab tsi.

// Tshaj tawm cov pins uas khawm txuas nrog.

const int buttonPin1 = 3; const int buttonPin2 = 4;

void teeb tsa ()

{// Pib qhov Serial.begin (9600); // Kho tus pin raws li tso zis. pinMode (buttonPin1, INPUT); pinMode (buttonPin2, INPUT); }

void lub voj ()

{// Nyeem lub xeev ntawm lub pob yog (digitalRead (buttonPin1) == HIGH) {// luam kab no yog lub xeev HIGH. Serial.println ("Sab laug"); ncua (20); } yog (digitalRead (buttonPin2) == HIGH) {// luam kab no yog tias lub xeev HIGH. Serial.println ("Txoj Cai"); ncua (20); }}

Kauj Ruam 4: Kev sib haum xeeb

Yog tias koj tsis muaj game npaj, txoj cai no yuav ua haujlwm ntawm txhua qhov gameobject hauv kev sib koom.

yog tias qhov no yog qhov koj yuav xav tau gameobject kom txav mus.

Rau qhov yooj yim peb yuav tsim lub voos xwmfab los ntawm kev mus rau GameObject-> 3D Object-> Cube

thaum lub voos xwmfab nyob hauv koj qhov xwm txheej, xaiv nws thiab nias lub pob Ntxiv Cov Khoom, thiab tsim tsab ntawv tshiab.

Tsis tas li koj yuav tsum tau hloov qib Api sib xws nyob rau hauv kev txiav txim rau System. IO. Ports tsev qiv ntawv ua haujlwm.

Txhawm rau ua qhov no mus rau Edit-> Project Settings-> Player

nqes mus rau hauv cov ntawv qhia zaub mov no kom txog thaum koj pom Api Compatibility Level thiab xaiv. NET 2.0 hloov ntawm. Net 2.0 subset

Tam sim no koj tau npaj pib coding.

siv System. Collections; siv System. Collections. Generic; siv UnityEngine; siv System. IO. Ports;

pej xeem hauv chav PlayerMovement: MonoBehaviour

{pej xeem ntab ceev; ntab txav;

SerialPort sp = tshiab SerialPort ("COM3", 9600);

void Pib ()

{// Khiav cov cai qhib qhov chaw nres nkoj txuas OpenSerialPort (); }

tsis muaj dab tsi OpenSerialPort ()

{// Qhib qhov chaw nres nkoj txuas sp. Open (); sp. ReadTimeout = 1; }

tsis muaj dabtsis txav (txoj hlua txoj kev)

{// Txheeb xyuas qhov kev taw qhia qhov arduino tau dhau mus yog (Kev Taw Qhia == "Sab laug") {txav mus = -1; } yog (Kev taw qhia == "Txoj Cai") {txav = 1; } // xam tus nqi uas gameobject yuav raug txav ntab txhais lus = txav * ceev; // Siv qhov txav mus rau gameobject hloov pauv. Txhais lus (txhais lus, 0, 0); }

void hloov tshiab ()

{yog (sp. IsOpen) {sim {// thaum lub serialport qhib qhib ua haujlwm txav lub luag haujlwm thiab hla kab uas Arduino luam ntawv txav chaw (sp. ReadLine ())); } ntes (System. Exception) {

}

}

} }

Kauj Ruam 5: Cov Lus Cim Kawg

Txheeb xyuas yog tias koj cov khoom siv ua haujlwm

Ib qho teeb meem uas kuv tau ntsib thaum tsim qhov no yog txhua qhov xov hlau thiab cov cai raug thiab yuav tsum tsis muaj teeb meem hlo li, tab sis nws tsis ua haujlwm. tib yam tuaj yeem tshwm sim nrog qee yam ntawm koj lwm yam khoom siv.

Pom zoo: