Kulesi sihloko, siqhubeka nokuthuthukisa isilawuli somlingisi we-platformer ye-2D ku-Unity, sihlola ngokucophelela isinyathelo ngasinye sokumisa nokuthuthukisa izilawuli.
Esihlokweni esandulele, " Indlela Yokudala Isilawuli Sezinhlamvu Ze-2D Ebunyeni: Ingxenye 1 ", sixoxisane ngokuningiliziwe ukuthi singadala kanjani isisekelo somlingisi, okuhlanganisa ukuziphatha kwakhe ngokomzimba nokunyakaza okuyisisekelo. Manje, sekuyisikhathi sokudlulela kuzici ezithuthuke kakhulu, ezifana nokubamba okokufaka nokulandela ikhamera eguqukayo.
Kulesi sihloko, sizocubungula ekusetheni isistimu entsha yokufaka ye-Unity, sidale izenzo ezisebenzayo zokulawula umlingisi, ukunika amandla ukweqa, nokuqinisekisa izimpendulo ezifanele emiyalweni yabadlali.
Uma ufuna ukwenza zonke izinguquko ezichazwe kulesi sihloko ngokwakho, ungadawuniloda inqolobane yegatsha elithi “ Umzimba Wezinhlamvu ”, eliqukethe isisekelo salesi sihloko. Kungenjalo, ungadawuniloda igatsha elithi “ Isilawuli Sezinhlamvu ” ngomphumela wokugcina.
Ngaphambi kokuthi siqale ukubhala ikhodi ukuze silawule uhlamvu lwethu, sidinga ukumisa isistimu yokufaka kuphrojekthi. Kumdlali wethu wenkundla, sikhethe Isistimu Yokokufaka entsha ye-Unity, eyethulwe eminyakeni embalwa edlule, ehlala ibalulekile ngenxa yezinzuzo zayo ngaphezu kwesistimu evamile.
Uhlelo Lokufaka lunikeza indlela evumelana nezimo futhi evumelana nezimo ekuphatheni okokufaka, okuvumela onjiniyela ukuthi bamise kalula izilawuli zamadivayisi ahlukahlukene futhi basekele izimo zokufakwayo eziyinkimbinkimbi ngaphandle kokusebenza okwengeziwe.
Okokuqala, faka iphakheji Yesistimu Yokufaka. Vula Isiphathi Sephakheji kumenyu enkulu ngokukhetha Iwindi → Isiphathi Sephakheji. Esigabeni Sokubhaliswa Kwebumbano, thola iphakheji "Isistimu Yokufaka" bese uchofoza okuthi "Faka".
Okulandelayo, iya kuzilungiselelo zephrojekthi ngemenyu ethi Hlela → Izilungiselelo Zephrojekthi. Khetha ithebhu ethi Isidlali, thola isigaba esithi Isiphathi Sokufaka Okusebenzayo, bese usimisa ukuze sithi "Iphakheji Yesistimu Yokufaka (Entsha).
Ngemva kokuqeda lezi zinyathelo, i-Unity izokwazisa ukuthi uqale kabusha. Uma sekuqaliswe kabusha, yonke into izobe isilungele ukulungiselela ukapteni wethu.
Kufolda ethi Izilungiselelo , dala Izenzo Zokokufaka usebenzisa imenyu enkulu: Amafa → Dala → Izenzo Zokufaka . Qamba ifayela elithi "Izilawuli."
I-Unity's Input System iyithuluzi elinamandla nelishintshashintshayo lokulawula okokufaka elivumela onjiniyela ukuthi balungiselele izilawuli zezinhlamvu nezinto zegeyimu. Isekela amadivaysi okokufaka ahlukahlukene. Izenzo Zokokufaka ozidalayo zinikeza ukuphathwa kokufakwayo okumaphakathi, ukwenza kube lula ukusetha nokwenza isixhumi esibonakalayo sibe lula.
Chofoza kabili ifayela le -Controls ukuze ulivule ukuze lihlelwe, bese wengeza I -Action Map yokulawula izinhlamvu ebizwa ngokuthi "Character."
I-Action Map in Unity iqoqo lezenzo ezingaxhunyaniswa nezilawuli ezihlukahlukene nokhiye ukwenza imisebenzi ethile kugeyimu. Kuyindlela ephumelelayo yokuhlela izilawuli, okuvumela onjiniyela bakwazi ukwaba nokulungisa okokufaka ngaphandle kokubhala kabusha ikhodi. Ukuze uthole imininingwane eyengeziwe, bheka imibhalo esemthethweni Yesistimu Yokufaka .
Isenzo sokuqala sizobizwa ngokuthi "Hambisa." Lesi senzo sizochaza isiqondiso sokunyakaza komlingisi. Setha Uhlobo Lwesenzo ku-"Value" kanye Nohlobo Lokulawula ku-"Vector2" ukuze unike amandla ukunyakaza ezindleleni ezine.
Yabela isibopho kulesi senzo ngokukhetha Ukuhlanganisa Phezulu/Phansi/Kwesokudla/Kwesokunxele futhi unikeze okhiye abajwayelekile be-WASD eziqondisweni zabo.
Ungakhohlwa ukulondoloza izilungiselelo zakho ngokuchofoza Londoloza Ifa . Lokhu kusetha kuqinisekisa ukuthi ungakwazi ukwabela kabusha izibopho zesenzo esithi "Hambisa", isibonelo, kumcibisholo okhiye noma induku yokudlala yegeyimu.
Okulandelayo, engeza isenzo esisha — "Yeqa." Gcina Uhlobo Lwesenzo njengokuthi "Inkinobho", kodwa engeza Ukusebenzisana okusha — "Cindezela", bese usetha I-Trigger Behavior ukuthi "Cindezela Futhi Ukhulule", njengoba sidinga ukuthwebula kokubili cindezela inkinobho nokukhulula.
Lokhu kuqeda uhlelo lokulawula izinhlamvu. Isinyathelo esilandelayo ukubhala ingxenye yokusingatha lezi zenzo.
Sekuyisikhathi sokuxhumanisa Izenzo Zokokufaka esizidalele ukulawula izinhlamvu engxenyeni ye CharacterBody
, okwenza umlingisi akwazi ukuhambahamba esigcawini ngokuvumelana nemiyalo yethu yokulawula.
Ukwenza lokhu, sizodala iskripthi esinesibopho sokulawula ukunyakaza futhi siyiqambe ngokuthi CharacterController
ukuze sicaciseleke. Kulesi script, sizoqale sichaze izinkambu eziyisisekelo. Sizokwengeza ireferensi engxenyeni ye CharacterBody
, _characterBody
, ezolawulwa ngokuqondile umbhalo.
Sizophinda sisethe amapharamitha esivinini sokunyakaza komlingiswa ( _speed
) nokuphakama kweqa ( _jumpHeight
). Ukwengeza, sizochaza inhloso yenkambu _stopJumpFactor
.
Kungenzeka ukuthi uqaphele ukuthi kumapulatifomu amaningi e-2D, ukuphakama kwe-jump kungalawulwa. Uma inkinobho yokweqa isabanjwa isikhathi eside, kulapho umlingiswa egxuma kakhulu. Empeleni, isivinini sokuqala esiya phezulu sisetshenziswa ekuqaleni kokugxuma, futhi leli jubane liyancishiswa lapho inkinobho ikhishwa. I _stopJumpFactor
inquma ukuthi ijubane elikhuphukayo liyancipha kangakanani lapho ukhulula inkinobho yokweqa.
Nasi isibonelo sekhodi esizoyibhala:
// CharacterController.cs public class CharacterController : MonoBehaviour { [SerializeField] private CharacterBody _characterBody; [Min(0)] [SerializeField] private float _speed = 5; [Min(0)] [SerializeField] private float _jumpHeight = 2.5f; [Min(1)] [SerializeField] private float _stopJumpFactor = 2.5f; }
Okulandelayo, sizosebenzisa ikhono lokuhambisa uhlamvu kwesokunxele nakwesokudla. Uma ebambe inkinobho yokunyakaza, umlingisi kufanele agcine isivinini sokunyakaza esishiwo kungakhathaliseki ukuthi yiziphi izithiyo. Ukuze sifinyelele lokhu, sizofaka okuhlukile kusikripthi ukuze sigcine isivinini sokunyakaza samanje endaweni (noma ngokuvundlile lapho uhlamvu luhamba emoyeni):
// CharacterController.cs private float _locomotionVelocity;
Engxenyeni CharacterBody
, sizokwethula indlela yokusetha lesi sivinini:
// CharacterBody.cs public void SetLocomotionVelocity(float locomotionVelocity) { Velocity = new Vector2(locomotionVelocity, _velocity.y); }
Njengoba umdlalo wethu ungafaki izindawo ezitshekile, le ndlela ilula kakhulu. Ezimweni eziyinkimbinkimbi kakhulu, kuzodingeka siphendule ngesimo somzimba kanye nomthambeka ongaphezulu. Okwamanje, simane silondoloze ingxenye eqondile yesivinini kuyilapho silungisa kuphela ukuxhumanisa okuvundlile kuka x
.
Okulandelayo, sizosetha leli nani endleleni Update
kuwo wonke uhlaka:
// CharacterController.cs private void Update() { _characterBody.SetLocomotionVelocity(_locomotionVelocity); }
Sizochaza indlela yokuphatha amasiginali asuka ku- Move
Input Action:
// CharacterController.cs public void OnMove(InputAction.CallbackContext context) { var value = context.ReadValue<Vector2>(); _locomotionVelocity = value.x * _speed; }
Njengoba isenzo Move
sichazwa ngokuthi i- Vector2
, umongo uzohlinzeka ivelu ye-vector kuye ngokuthi yiziphi izinkinobho ezicindezelwe noma ezikhishwe. Isibonelo, ukucindezela ukhiye D
kuzoholela endleleni OnMove
yokuthola i-vector (1, 0). Ukucindezela kokubili D
no- W
ngesikhathi esisodwa kuzoholela ku-(1, 1). Ukukhulula bonke okhiye kuzoqalisa OnMove
ngevelu (0, 0).
Kukhiye ongu A
, i-vector izoba (-1, 0). Endleleni ye OnMove
, sithatha ingxenye evundlile yevekhtha eyamukelwe bese siyiphindaphinda ngesivinini esishiwo sokunyakaza, _speed
.
Okokuqala, sidinga ukufundisa ingxenye ye CharacterBody
ukuphatha ukweqa. Ukwenza lokhu, sizongeza indlela enesibopho sokugxuma:
// CharacterBody.cs public void Jump(float jumpSpeed) { Velocity = new Vector2(_velocity.x, jumpSpeed); State = CharacterState.Airborne; }
Esimweni sethu, le ndlela iqondile: ibeka isivinini esiqondile futhi ngokushesha ishintsha isimo somlingisi sibe Airborne
.
Okulandelayo, sidinga ukunquma isivinini umlingiswa okufanele agxume ngaso. Sesivele sikuchazile ukuphakama kwe-jump futhi siyazi ukuthi amandla adonsela phansi ahlala esebenza emzimbeni. Ngokusekelwe kulokhu, isivinini sokuqala sokugxuma singabalwa kusetshenziswa ifomula:
Lapho u-h ewubude bokweqa, futhi u-g ukusheshisa kwamandla adonsela phansi. Futhi sizolandisa ngesiphindaphindi samandla adonsela phansi esikhona engxenyeni ye CharacterBody
. Sizongeza inkambu entsha ukuze sichaze isivinini sokuqala sokweqa futhi sibale ngale ndlela elandelayo:
// CharacterController.cs private float _jumpSpeed; private void Awake() { _jumpSpeed = Mathf.Sqrt(2 * Physics2D.gravity.magnitude * _characterBody.GravityFactor * _jumpHeight); }
Sizodinga enye inkambu ukuze silandelele ukuthi ingabe umlingisi uyeqa okwamanje, ukuze sikwazi ukukhawulela isivinini sokugxuma ngesikhathi esifanele.
Ukwengeza, uma umdlali ephethe inkinobho yokweqa kuze kube yilapho efika, kufanele silethe kabusha leli fulegi ngokwethu. Lokhu kuzokwenziwa ngendlela Update
:
// CharacterController.cs private bool _isJumping; private void Update() { if (_characterBody.State == CharacterState.Grounded) { _isJumping = false; } //... }
Manje, ake sibhale indlela yokusingatha isenzo Jump
:
// CharacterController.cs public void OnJump(InputAction.CallbackContext context) { if (context.started) { Jump(); } else if (context.canceled) { StopJumping(); } }
Njengoba isenzo Jump
siyinkinobho, singakwazi ukunquma kusukela komongo ukuthi ukucindezela inkinobho kuqalile ( context.started
) noma kuphelile ( context.canceled
). Ngokusekelwe kulokhu, siqala noma simise ukweqa.
Nansi indlela yokwenza i-jump:
// CharacterController.cs private void Jump() { if (_characterBody.State == CharacterState.Grounded) { _isJumping = true; _characterBody.Jump(_jumpSpeed); } }
Ngaphambi kokugxuma, sibheka ukuthi umlingiswa uphansi yini. Uma kunjalo, sisetha ifulegi _isJumping
bese senza umzimba ugxume nge _jumpSpeed
.
Manje, ake sisebenzise indlela yokuyeka ukugxuma:
// CharacterController.cs private void StopJumping() { var velocity = _characterBody.Velocity; if (_isJumping && velocity.y > 0) { _isJumping = false; _characterBody.Velocity = new Vector2( velocity.x, velocity.y / _stopJumpFactor); } }
Simisa ukweqa kuphela uma ifulegi elithi _isJumping
lisebenza. Esinye isimo esibalulekile ukuthi umlingiswa kumele anyukele phezulu. Lokhu kuvimbela ukukhawulela isivinini sokuwa uma inkinobho ye-jump ikhishwa ngenkathi iya phansi. Uma zonke izimo zihlangatshezwana nazo, sisetha kabusha ifulegi _isJumping
futhi sehlise isivinini esime mpo ngento ethi _stopJumpFactor
.
Manje njengoba zonke izingxenye sezimi ngomumo, engeza izingxenye PlayerInput
kanye ne CharacterController
entweni kaCaptain endaweni yesigameko. Qiniseka ukuthi ukhetha ingxenye ye CharacterController
esiyidalile, hhayi ingxenye evamile ye-Unity edizayinelwe ukulawula izinhlamvu ze-3D.
Ku CharacterController
, yabela ingxenye ekhona CharacterBody
ukusuka kuhlamvu. Ku PlayerInput
, setha Izilawuli ezidalwe ngaphambilini kunkambu Actions
.
Okulandelayo, lungiselela ingxenye ye-PlayerInput ukuze ushayele izindlela ezifanele ku-CharacterController. Nweba izigaba zezehlakalo nezinhlamvu kusihleli, bese uxhumanisa izindlela ezihambisanayo nezenzo zokuthi Hambisa futhi Yeqa.
Manje, yonke into isimi ngomumo ukuze iqalise umdlalo futhi ihlole ukuthi zonke izingxenye ezimisiwe zisebenza kanjani ndawonye.
Manje, sidinga ukwenza ikhamera ilandele umlingisi nomaphi lapho eya khona. Ubunye bunikeza ithuluzi elinamandla lokuphatha ikhamera — Umshini wesinema .
I-Cinemachine iyisixazululo soguquko sokulawula ikhamera ku-Unity enikeza onjiniyela amakhono anhlobonhlobo okudala amasistimu wamakhamera aguqukayo, ashunwe kahle avumelana nezidingo zokudlala umdlalo. Leli thuluzi lenza kube lula ukusebenzisa amasu ekhamera ayinkimbinkimbi, njengokulandela umlingiswa, ukulungisa ukugxila okuzenzakalelayo, nokunye okuningi, okwengeza ubungqabavu nokunotha kuzo zonke izigcawu.
Okokuqala, thola into Yekhamera Eyinhloko esigcawini, bese wengeza ingxenye CinemachineBrain
kuyo.
Okulandelayo, dala into entsha esigcawini okuthiwa CaptainCamera . Lena kuzoba yikhamera elandela ukapteni, njengomthwebuli ochwepheshile. Engeza ingxenye CinemachineVirtualCamera
kuyo. Setha inkambu ethi Landela ukapteni, khetha I-Framing Transposer yenkambu Yomzimba , bese usetha ipharamitha ye -Lens Ortho size ibe ngu-4.
Ukwengeza, sizodinga enye ingxenye ukuze sichaze i-offset yekhamera ehlobene nomlingisi — CinemachineCameraOffset
. Setha inani elingu-Y libe ngu-1.5 kanye nenani lika-Z libe ngu--15.
Manje, ake sihlole ukuthi ikhamera ilandela kanjani umlingiswa wethu.
Ngicabanga ukuthi kuhambe kahle kakhulu. Ngabona ukuthi ikhamera iyangingiza kancane ngezikhathi ezithile. Ukulungisa lokhu, ngimisa inkambu ye-Blend Update Method yento ye-Main Camera ku-FixedUpdate.
Ake sihlole oomakhenikha ababuyekeziwe. Zama ukugijima nokugxuma ngokuqhubekayo. Abadlali bamageyimu abanolwazi bangase baqaphele ukuthi ukweqa akubhalisi njalo. Emidlalweni eminingi, lokhu akuyona inkinga.
Kuvele ukuthi kunzima ukubikezela isikhathi sokufika esiqondile ukuze ucindezele inkinobho yokweqa futhi. Sidinga ukwenza igeyimu ixolele kakhulu ngokuvumela abadlali ukuthi bacindezele gxuma kancane ngaphambi kokuhlala futhi senze umlingisi agxume ngokushesha lapho efika. Lokhu kuziphatha kuhambisana nalokho abadlali abakujwayele.
Ukuze wenze lokhu, sizokwethula okuhlukile okusha, _jumpActionTime
, okubonisa isikhathi lapho ukweqa kusengaqaliswa khona uma ithuba livela.
// CharacterController.cs [Min(0)] [SerializeField] private float _jumpActionTime = 0.1f;
Ngengeze inkambu _jumpActionEndTime
, ephawula ukuphela kwewindi lesenzo sokweqa. Ngamanye amazwi, kuze kufinyelelwe ku _jumpActionEndTime
, umlingisi uzogxuma uma ithuba livela. Masiphinde sibuyekeze isibambi sesenzo Jump
.
// CharacterController.cs private float _jumpActionEndTime; public void OnJump(InputAction.CallbackContext context) { if (context.started) { if (_characterBody.State == CharacterState.Grounded) { Jump(); } else { _jumpActionEndTime = Time.unscaledTime + _jumpActionTime; } } else if (context.canceled) { StopJumping(); } }
Lapho inkinobho yokweqa icindezelwa, uma umlingisi ephansi, bayagxuma ngokushesha. Uma kungenjalo, sigcina iwindi lesikhathi lapho ukweqa kusengenziwa khona.
Ake sisuse isheke lesimo Grounded
endleleni ye Jump
ngokwayo.
// CharacterController.cs private void Jump() { _isJumping = true; _characterBody.Jump(_jumpSpeed); }
Sizophinda sijwayelane nendlela yokweqa. Uma inkinobho ikhishwe ngaphambi kokuhlala, akukho ukweqa okufanele kwenzeke, ngakho-ke sisetha kabusha _jumpActionEndTime
.
// CharacterController.cs private void StopJumping() { _jumpActionEndTime = 0; //... }
Kufanele sihlole nini ukuthi umlingiswa usefikile futhi siqalise ukugxuma? Isimo CharacterBody
sicutshungulwa kokuthi FixedUpdate
, kuyilapho ukucubungula isenzo kwenzeka kamuva. Kungakhathaliseki ukuthi i Update
noma FixedUpdate
, ukubambezeleka kohlaka olulodwa phakathi kokufika nokugxuma kungenzeka, okubonakalayo.
Sizongeza umcimbi StateChanged
ku CharacterBody
ukuze siphendule ngokushesha ekufikeni. I-agumenti yokuqala izoba yisimo sangaphambilini, kanti eyesibili izoba yisimo samanje.
// CharacterBody.cs public event Action<CharacterState, CharacterState> StateChanged;
Sizolungisa abaphathi bezwe ukuze siqalise umcimbi wokushintsha isimo bese sibhala kabusha FixedUpdate
.
// CharacterBody.cs [field: SerializeField] private CharacterState _state; public CharacterState State { get => _state; private set { if (_state != value) { var previousState = _state; _state = value; StateChanged?.Invoke(previousState, value); } } }
Ngiphinde ngalungisa ukuthi surfaceHit
iphathwa kanjani kokuthi FixedUpdate
.
// CharacterBody.cs private void FixedUpdate() { //... if (_velocity.y <= 0 && slideResults.surfaceHit) { var surfaceHit = slideResults.surfaceHit; Velocity = ClipVector(_velocity, surfaceHit.normal); if (surfaceHit.normal.y >= _minGroundVertical) { State = CharacterState.Grounded; return; } } State = CharacterState.Airborne; }
Ku CharacterController
, sizobhalisela umcimbi StateChanged
futhi sengeze isibambi.
// CharacterController.cs private void OnEnable() { _characterBody.StateChanged += OnStateChanged; } private void OnDisable() { _characterBody.StateChanged -= OnStateChanged; } private void OnStateChanged(CharacterState previousState, CharacterState state) { if (state == CharacterState.Grounded) { OnGrounded(); } }
Sizosusa ukuhlola kwe Grounded
state kokuthi Update
bese sikuhambisa ku OnGrounded
.
// CharacterController.cs private void Update() { _characterBody.SetLocomotionVelocity(_locomotionVelocity); } private void OnGrounded() { _isJumping = false; }
Manje, engeza ikhodi ukuze uhlole ukuthi ingabe ukweqa kufanele kucushwe.
// CharacterController.cs private void OnGrounded() { _isJumping = false; if (_jumpActionEndTime > Time.unscaledTime) { _jumpActionEndTime = 0; Jump(); } }
Uma _jumpActionEndTime
inkulu kunesikhathi samanje, kusho ukuthi inkinobho yokweqa isanda kucindezelwa, ngakho sisetha kabusha _jumpActionEndTime
bese seqa.
Manje, zama ngokuqhubekayo ukweqa nomlingiswa. Uzoqaphela inkinobho yokweqa izwakala isabela kakhulu, futhi ukulawula umlingisi kuba bushelelezi. Kodwa-ke, ngibonile ukuthi ezimweni ezithile, njengekhona eliboniswe emfanekisweni ongezansi, i Grounded
state ihlangabezana nokubambezeleka okuncane, okuphazamisa uchungechunge lwe-jump.
Ukuze ngibhekane nalokhu, ngimisa inkambu ye Surface Anchor
engxenyeni ye CharacterBody
ibe ngu-0.05 esikhundleni sika-0.01. Leli nani limelela ubuncane bebanga ukuya endaweni ukuze umzimba ungene kusimo Grounded
.
Kungenzeka ukuthi uqaphele ukuthi ukuzama ukugxuma ngenkathi ugijima endaweni eqondile akusebenzi ngaso sonke isikhathi. Kungase kuzwakale sengathi inkinobho yokweqa kwesinye isikhathi ayiphenduli.
Lokhu ngokunye kobuqili bokwakha Isilawuli Sezinhlamvu zamapulatifomu e-2D. Abadlali badinga ikhono lokugxuma ngisho noma sekwephuze kancane ngokucindezela inkinobho yokweqa. Nakuba lo mqondo ungase ubonakale uyinqaba, yindlela iningi lamapulatifomu asebenza ngayo. Umphumela uba umlingiswa obonakala ephusha emoyeni, njengoba kuboniswe kupopayi ngezansi.
Masisebenzise lo mshini. Sizokwethula inkambu entsha yokugcina iwindi lesikhathi (ngamasekhondi) lapho umlingisi asengakwazi ukweqa ngemva kokulahlekelwa isimo Grounded
.
// CharacterController.cs [Min(0)] [SerializeField] private float _rememberGroundTime = 0.1f;
Sizophinda sengeze enye inkambu ukuze sigcine isitembu sesikhathi okuthi ngemva kwalokho i- Grounded
state "ikhohlwe."
// CharacterController.cs private float _lostGroundTime;
Lesi simo sizolandelelwa kusetshenziswa umcimbi CharacterBody
. Sizolungisa isibambi se OnStateChanged
kule njongo.
// CharacterController.cs private void OnStateChanged(CharacterState previousState, CharacterState state) { if (state == CharacterState.Grounded) { OnGrounded(); } else if (previousState == CharacterState.Grounded) { _lostGroundTime = Time.unscaledTime + _rememberGroundTime; } }
Kubalulekile ukuhlukanisa ukuthi ingabe umlingisi ulahlekelwe yisimo Grounded
ngenxa yokweqa ngenhloso noma ngenxa yesinye isizathu. Sesivele sinefulegi elithi _isJumping
, elikhutshazwayo njalo uma StopJumping
ibizwa ukuvimbela izenzo ezingafuneki.
Nginqume ukungalethuli elinye ifulegi njengoba ukukhanselwa kokweqa okungasasebenzi akuthinti ukudlala umdlalo. Zizwe ukhululekile ukuzama. Ifulegi _isJumping
manje selizosulwa kuphela uma umlingisi ehlala ngemuva kokugxuma. Masibuyekeze ikhodi ngokufanele.
// CharacterController.cs private void StopJumping() { _jumpActionEndTime = 0; var velocity = _characterBody.Velocity; if (_isJumping && velocity.y > 0) { _characterBody.Velocity = new Vector2( velocity.x, velocity.y / _stopJumpFactor); } }
Ekugcineni, sizobuyekeza indlela OnJump
.
// CharacterController.cs public void OnJump(InputAction.CallbackContext context) { if (context.started) { if (_characterBody.State == CharacterState.Grounded || (!_isJumping && _lostGroundTime > Time.unscaledTime)) { Jump(); } else { _jumpActionEndTime = Time.unscaledTime + _jumpActionTime; } } else if (context.canceled) { StopJumping(); } }
Manje, ukweqa endaweni eqondile akusaphazamisi isigqi segeyimu futhi kuzwakala kungokwemvelo kakhulu, naphezu kobuwula bako obusobala. Umlingiswa angakwazi ukusunduza emoyeni ngokoqobo, aye kude kunalokho okubonakala kunengqondo. Kodwa yilokhu kanye okudingekayo ku-platformer yethu.
Ukuthinta okokugcina kwenza umlingisi abhekane nesiqondiso somnyakazo. Sizokwenza lokhu ngendlela elula kakhulu - ngokushintsha isikali somlingisi eduze kwe-x-eksisi. Ukusetha inani elibi kuzokwenza ukapteni wethu abhekane nolunye uhlangothi.
Okokuqala, masigcine isikali sangempela uma sihluka kwesi-1.
// CharacterController.cs public class CharacterController : MonoBehaviour { //... private Vector3 _originalScale; private void Awake() { //... _originalScale = transform.localScale; } }
Manje, uma siya kwesokunxele noma kwesokudla, sizosebenzisa isikali esiphozithivu noma esinegethivu.
// CharacterController.cs public class CharacterController : MonoBehaviour { public void OnMove(InputAction.CallbackContext context) { //... // Change character's direction. if (value.x != 0) { var scale = _originalScale; scale.x = value.x > 0 ? _originalScale.x : -_originalScale.x; transform.localScale = scale; } } }
Ake sihlole umphumela.
Lesi sihloko sivele sinemininingwane eminingi, kodwa sikwazile ukumboza zonke izici ezibalulekile zokulawula izinhlamvu ku-platformer ye-2D. Njengesikhumbuzi, ungabheka umphumela wokugcina egatsheni elithi “ Isilawuli Sezinhlamvu ” wendawo yokugcina.
Uma ukujabulele noma uthole lokhu kanye nendatshana edlule ilusizo, ngingabonga ukuthandwa nezinkanyezi ku-GitHub. Ungangabazi ukufinyelela lapho uhlangabezana nezinkinga noma uthola amaphutha. Ngiyabonga ukulalela kwenu!