paint-brush
Ndenge ya kosala Contrôleur ya caractère 2D na bomoko: Eteni ya 2pene@deniskondratev
134 botángi

Ndenge ya kosala Contrôleur ya caractère 2D na bomoko: Eteni ya 2

pene Denis Kondratev17m2024/12/08
Read on Terminal Reader

Molai mingi; Mpo na kotánga

Lisolo oyo ezali kolakisa ndenge ya kotombola contrôleur ya caractère Unity 2D, oyo ezali ko couvrir configuration ya système ya sika ya entrée, mécanique ya saut améliorée, mpe suivi ya caméra sans soudure.
featured image - Ndenge ya kosala Contrôleur ya caractère 2D na bomoko: Eteni ya 2
Denis Kondratev HackerNoon profile picture

Na article oyo, tozali kokoba ko développer contrôleur ya caractère pona plateforme 2D na Unity, ko examiner malamu étape moko na moko ya ko configurer pe ko optimiser ba contrôles.


Na lisolo oyo eleki, “ Ndenge ya kosala contrôleur ya personnage 2D na bomoko: Eteni ya 1 ”, tolobelaki na bozindo ndenge ya kosala moboko ya personnage, bakisa mpe bizaleli na ye ya nzoto mpe mouvement ya moboko. Sikoyo, ekomi tango ya kokende na ba aspects ya avancement mingi, lokola manipulation ya entrée na suivi dynamique ya caméra.


Na article oyo, toko profonder na ko setup système ya sika ya entrée ya Unity, ko créer ba actions actives pona ko contrôler personnage, ko permettre ko sauter, pe ko assurer ba réponses ya malamu na ba commandes ya joueur.

Soki olingi kosalela mbongwana nyonso oyo elimbolami na lisolo oyo yo moko, okoki kozwa etape ya ebombelo ya “ Nzoto ya bizaleli ”, oyo ezali na moboko ya lisolo oyo. Soki te, okoki kozwa filiale “ Contrôleur des caractères ” na résultat ya suka.

Kobongisa Système ya Entrée

Yambo to banda kokoma code pona ko contrôler personnage na biso, esengeli to configurer système ya entrée na projet. Mpo na platformer na biso, toponi Système ya sika ya Input ya Unity, oyo ekɔtisamaki eleki mwa bambula, oyo etikali na ntina mpo na matomba na yango koleka système ya bonkoko.


Système d’entrée epesaka approche modulaire mpe flexible mingi na manipulation ya entrée, epesaka ba développeurs nzela ya ko se mettre en place ba contrôles ya ba appareils ndenge na ndenge na pete mpe ko soutenir ba scénarios ya entrée complexe mingi sans ba frais généraux ya mise en œuvre ya kobakisa.


Ya liboso, installer ensemble ya Système d’entrée. Fungola Gestionnaire ya ba paquets na menu principal na koponáká Fenêtre → Gestionnaire ya ba paquets. Na eteni ya Registre ya bomoko, luka ensemble "Système d'entrée" mpe finá "Install".

Na nsima, kende na ba paramètres ya projet na nzela ya menu Edit → Paramètres ya projet. Pona onglet Joueur, luka eteni ya Manipulation ya entrée active, mpe tia yango na "Paquet ya système d'entrée (Nouveau).

Sima ya kosilisa ba étapes oyo, Unity ekosenga yo obandi lisusu. Soki tobandi lisusu, nionso ekozala prêt ya ko configurer ba contrôles pona capitaine na biso.

Kosala Misala ya Bokotisi

Na dossier Paramètres , sala Misala ya Bokotisi na nzela ya menu monene: Biloko → Kosala → Misala ya bokotisi . Bopesa nkombo ya fisyé "Contrôles."

Système d’entrée ya Unity ezali outil ya gestion ya entrée ya makasi pe flexible oyo epesaka ba développeurs nzela ya ko configurer ba contrôles pona ba personnages na ba éléments ya jeu. Ezali ko soutenir ba appareils ya entrée ndenge na ndenge. Ba Actions ya Entrée oyo osali epesaka gestion centralisée ya entrée, ko simplifier configuration pe kosala interface plus intuitive.


Finá mbala mibale na fisyé Contrôles mpo na kofungola yango mpo na kobongisa, mpe bakisa Carte d'action mpo na contrôle ya bilembo oyo ezali na kombo ya "Character."

Carte d’action na Bomoko ezali lisanga ya misala oyo ekoki kozala na boyokani na ba contrôleurs mpe ba clés ndenge na ndenge mpo na kosala misala ya sikisiki na lisano. Ezali lolenge ya malamu ya kobongisa ba contrôles, kopesa nzela na ba développeurs ya ko allouer mpe ko ajuster ba entrées sans kokoma lisusu code. Mpo na koyeba makambo mosusu, tala mikanda ya officiel ya Système d’entrée .


Action ya liboso ekobengama "Move." Action oyo eko définir direction ya mouvement ya personnage. Botia Type ya Action na "Value" mpe Type ya Contrôle na "Vector2" mpo na ko permettre mouvement na ba direction minei.

Assigner ba liaisons na action oyo na kopona Add Up/Down/Right/Left Composite mpe ko assigner ba touches WASD oyo eyebani na ba direction na bango respectives.

Kobosana te kobomba ba paramètres na yo na kofinaka Bomba Biloko . Configuration oyo e assure que okoki ko reassigner ba liaisons pona action "Move", par exemple, na ba touches ya flèches to même na joystick ya gamepad.


Na nsima, bakisa likambo ya sika — "Kopumbwa." Bomba Type ya Action lokola "Bouton", kasi bakisa Interaction ya sika — "Press", mpe tia Trigger Behavior na "Press And Release", lokola tosengeli kokanga ezala bouton presse mpe liberation.

Yango esilisaka schéma ya contrôle ya ba personnages. Etape oyo elandi ezali ya kokoma composante moko mpo na ko gérer ba actions wana.

Kokende na moto oyo azali kosala na lobɔkɔ ya mwasi mpe na lobɔkɔ ya mobali

Ezali tango ya ko relier ba Actions d’entrée oyo tosalaki pona contrôle ya personnage na composante CharacterBody , ko permettre personnage a tambola activement na scène selon ba commandes na biso ya contrôle.


Pona kosala yango, tokosala script oyo ezali responsable ya contrôle ya mouvement pe tokopesa yango kombo CharacterController pona clarté. Na script oyo, toko définir liboso mua ba domaines ya base. Tokobakisa référence na composante CharacterBody , _characterBody , oyo ekozala directement contrôlé na script.


Tokotia pe ba paramètres pona vitesse ya mouvement ya personnage ( _speed ) pe hauteur ya saut ( _jumpHeight ). En plus, toko définir tina ya champ _stopJumpFactor .


Okoki komona ete na ba plateformes mingi ya 2D, hauteur ya saut ekoki kozala contrôlé. Soki bouton ya kopumbwa esimbami ntango molai, moto yango akopumbwa likoló. Na ntina mpenza, mbangu ya liboso ya komata esalelamaka na ebandeli ya kopumbwa, mpe mbangu yango ekitisami ntango bouton ebimisami. _stopJumpFactor elakisaka na boniboni mbangu ya komata ekiti na kobimisa bouton ya kopumbwa.


Tala ndakisa ya code oyo tokokoma:


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


Na sima, toko mettre en œuvre makoki ya ko déplacer personnage gauche na droite. Ntango ozali kosimba bouton ya mouvement, personnage asengeli kobatela vitesse ya mouvement oyo elakisami ata soki ezali na mikakatano nini. Mpo na kokokisa yango, tokobakisa variable moko na script mpo na kobomba vitesse ya mouvement ya lelo na surface (to kaka horizontalement tango personnage ezali na mopepe):


 // CharacterController.cs private float _locomotionVelocity;


Na composante CharacterBody , toko kotisa méthode ya ko setting vitesse oyo:


 // CharacterBody.cs public void SetLocomotionVelocity(float locomotionVelocity) { Velocity = new Vector2(locomotionVelocity, _velocity.y); }


Lokola lisano na biso ezalaka na bisika oyo ezali na nse te, lolenge yango ezali mpenza pɛtɛɛ. Na makambo ya mindɔndɔmindɔndɔ mingi, ekosɛnga tópesa ntina ya ezalela ya nzoto mpe ndenge oyo likoló ezali kokita. Mpo na sikoyo, tozali kaka kobatela composante verticale ya vitesse tout en modifier kaka coordonnée x horizontal.


Na sima, tokotia valeur oyo na méthode Update na cadre nionso:


 // CharacterController.cs private void Update() { _characterBody.SetLocomotionVelocity(_locomotionVelocity); }


Toko définir méthode ya ko gérer ba signaux oyo ewutaka na Move Input Action:


 // CharacterController.cs public void OnMove(InputAction.CallbackContext context) { var value = context.ReadValue<Vector2>(); _locomotionVelocity = value.x * _speed; }


Lokola action Move e définir lokola Vector2 , contexte ekopesa valeur ya vecteur selon ba touches nini efinami to ebimisami. Ndakisa, kofina touche D ekosala ete méthode OnMove ezwa vecteur (1, 0). Kofina D mpe W nyonso mibale na mbala moko ekosala ete ozwa (1, 1). Kobimisa ba touches nionso eko déclencher OnMove na valeur (0, 0).


Mpo na fungola A , vecteur ekozala (-1, 0). Na méthode OnMove , tozuaka composante horizontale ya vecteur oyo ezuami pe to multiplier yango na vitesse ya mouvement oyo elakisami, _speed .

Koteya ezaleli ya kopumbwa

Ya liboso, tosengeli koteya composante CharacterBody mpo na kosimba kopumbwa. Mpo na kosala yango, tokobakisa lolenge moko oyo ezali na mokumba ya kopumbwa:


 // CharacterBody.cs public void Jump(float jumpSpeed) { Velocity = new Vector2(_velocity.x, jumpSpeed); State = CharacterState.Airborne; }


Na cas na biso, méthode oyo ezali droite : etie vitesse verticale mpe e changeaka mbala moko état ya personnage na Airborne .


Na nsima, tosengeli koyeba mbangu oyo moto oyo azali kosala yango asengeli kopumbwa. Tosili kolimbola bosanda ya kopumbwa mpe toyebi ete nguya ya kobenda biloko esalaka ntango nyonso na nzoto. Na kotalela yango, vitesse ya saut ya liboso ekoki ko calculer na nzela ya formule :



Epayi h ezali bosanda ya kopumbwa, mpe g ezali accélération gravitationnelle. Toko comptabiliser pe multiplicateur ya gravité oyo ezali na composant CharacterBody . Tokobakisa esika ya sika mpo na kolimbola mbangu ya kopumbwa ya ebandeli mpe kosala calcul na yango boye:


 // CharacterController.cs private float _jumpSpeed; private void Awake() { _jumpSpeed = Mathf.Sqrt(2 * Physics2D.gravity.magnitude * _characterBody.GravityFactor * _jumpHeight); }


Tokozala na mposa ya esika mosusu mpo na kolandela soki moto azali kopumbwa na ntango oyo, mpo ete tokoka kopekisa mbangu ya kopumbwa na ngonga oyo ebongi.


En plus, soki joueur asimbi bouton ya saut tiii na atterrissage, esengeli biso moko to réinitialiser drapeau oyo. Yango ekosalema na lolenge Update :


 // CharacterController.cs private bool _isJumping; private void Update() { if (_characterBody.State == CharacterState.Grounded) { _isJumping = false; } //... }


Sikoyo, tokoma méthode ya ko gérer action Jump :


 // CharacterController.cs public void OnJump(InputAction.CallbackContext context) { if (context.started) { Jump(); } else if (context.canceled) { StopJumping(); } }


Lokola action Jump ezali bouton, tokoki koyeba na contexte soki kofina bouton ebandi ( context.started ) to esili ( context.canceled ). Na kotalela likambo oyo, tobandisaka to topekisaka kopumbwa.


Tala méthode ya ko exécuter saut:


 // CharacterController.cs private void Jump() { if (_characterBody.State == CharacterState.Grounded) { _isJumping = true; _characterBody.Jump(_jumpSpeed); } }


Liboso ya kopumbwa, totalaka soki moto yango azali na mabele. Soki ezali bongo, totie drapeau _isJumping mpe tosalaka ete nzoto epumbwa na _jumpSpeed .

Sikoyo, to mettre en œuvre comportement ya stop-jumping:


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


Totelemisa saut kaka soki drapeau _isJumping ezali active. Condition mosusu ya ntina ezali que personnage asengeli kozala kokende likolo. Yango epekisaka kopekisa mbangu ya kokweya soki bouton ya kopumbwa ebimisami ntango ezali kokita. Soki ba conditions nionso ekokisami, to réinitialiser drapeau _isJumping mpe tokitisaka vitesse verticale na facteur ya _stopJumpFactor .

Kobongisa Moto oyo azali kosala

Sikoyo lokola ba composants nionso esili, bakisa ba composants PlayerInput na CharacterController na objet Capitaine na scène. Sala makasi pona composant CharacterController oyo tosalaki, kasi composante standard Unity oyo ebongisami pona ko contrôler ba caractères 3D te.


Mpo na CharacterController , pesa eteni ya CharacterBody oyo ezali uta na elembo. Mpo na PlayerInput , tia ba Contrôles oyo osala liboso na esika ya Actions .

Na sima, configurer composante PlayerInput pona kobenga ba méthodes oyo ebongi na CharacterController. Panza biteni ya Makambo mpe Bizaleli na mobongisi, mpe kangisa mayele oyo ekokani na misala ya Kokende mpe Kopumbwa.

Sikoyo, nionso ezali prêt ya ko tambuisa jeu pe ko tester ndenge ba composants nionso configurés esalaka elongo.


Mouvement ya Caméra

Sikoyo, esengeli tosala que camera elanda personnage esika nionso bakokende. Bomoko epesaka esaleli ya nguya mpo na boyangeli kamera — Cinemachine .


Cinemachine ezali solution révolutionnaire pona contrôle ya caméra na Unity oyo epesaka ba développeurs ba capacités ebele pona ko créer ba systèmes dynamiques, bien ajustés ya caméra oyo ezo adapter na ba besoins ya jeu. Esaleli oyo esalaka ete ezala mpasi te mpo na kosalela mayele ya mindɔndɔmindɔndɔ ya kamera, na ndakisa kolanda bato, kobongisama na ndenge ya automatique, mpe makambo mosusu mingi, mpe yango ebakisaka makasi mpe bomengo na scène nyonso.


Ya liboso, luka objet Camera Principal na scène, pe bakisa composant CinemachineBrain na yango.

Na sima, sala eloko ya sika na scène na kombo ya CaptainCamera . Oyo ekozala camera oyo ekolanda capitaine, kaka lokola cameraman professionnel. Bakisa eteni ya CinemachineVirtualCamera na yango. Botia esika ya Bolanda na kapiteni, pona Transposer ya encadrage mpo na esika ya Nzoto , mpe botia paramètre ya Taille Ortho ya Objectif na 4.


En plus, tokozala na besoin ya composant mosusu pona ko définir offset ya caméra par rapport na personnage — CinemachineCameraOffset . Botia motuya ya Y na 1.5 mpe motuya ya Z na -15.

Sikoyo, tomeka ndenge nini camera elandi ezaleli na biso.



Nakanisi ete esalemaki malamu mpenza. Namonaki ete kamera ebɛtaka ntango mosusu mwa moke. Mpo na kobongisa yango, natyaki esika ya Méthode ya mise à jour ya kosangisa ya objet Caméra principale na FixedUpdate.

Kobongisa ba Sauts

Tomeka ba mécaniques oyo ebongwani. Meká kopota mbangu mpe kopumbwa ntango nyonso. Bato oyo bayebi masano ya masano bakoki komona ete ba sauts ekomamaka ntango nyonso te. Na masano mingi, likambo oyo ezali likambo te.


Emonani ete ezali mpasi mpo na kosakola ngonga ya sikisiki ya kokita mpo na kofina lisusu bouton ya kopumbwa. Tosengeli kosala ete lisano yango elimbisa mingi na kopesaka nzela na basani ete báfina mwa moke kopumbwa liboso ya kokita mpe kosala ete moto oyo azali kosala yango apumbwa mbala moko ntango azali kokita. Comportement oyo eyokani na oyo ba joueurs ba mesana na yango.


Pona ko mettre en œuvre oyo, toko kotisa variable ya sika, _jumpActionTime , oyo ezali ko représenter fenêtre ya temps oyo na tango na yango saut ekoki kaka ko déclenché soki libaku ebimi.


 // CharacterController.cs [Min(0)] [SerializeField] private float _jumpActionTime = 0.1f;


Nabakisi esika _jumpActionEndTime , oyo ezali ko marquer suka ya fenêtre ya action ya jump. Na maloba mosusu, kino _jumpActionEndTime ekokoma, moto akopumbwa soki libaku ebimi. Tosala pe mise à jour ya handler ya action 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(); } }


Ntango bafinaka bouton ya kopumbwa, soki moto oyo azali kosala yango azali na mabele, bapumbwaka mbala moko. Soki te, tobombaka fenetre ya ngonga oyo na boumeli na yango kopumbwa ekoki naino kosalema.


Tolongola vérification ya état Grounded na méthode Jump yango moko.


 // CharacterController.cs private void Jump() { _isJumping = true; _characterBody.Jump(_jumpSpeed); }


Toko adapter pe méthode ya stop-jumping. Soki bouton ebimisamaki liboso ya kokita, saut moko te esengeli kosalema, yango wana to réinitialiser _jumpActionEndTime .


 // CharacterController.cs private void StopJumping() { _jumpActionEndTime = 0; //... }


Tango nini tosengeli ko vérifier soki personnage a atterrir pe ko déclencher saut? Etat CharacterBody esalemaka na FixedUpdate , nzoka nde traitement ya action esalemaka sima. Ata soki ezali Update to FixedUpdate , retard ya cadre moko kati na kokita mpe kopumbwa ekoki kosalema, oyo ezali komonana.


Tokobakisa likambo StateChanged na CharacterBody mpo na koyanola mbala moko na atterrissage. Argument ya liboso ekozala état ya liboso, mpe ya mibale ekozala état ya lelo.


 // CharacterBody.cs public event Action<CharacterState, CharacterState> StateChanged;


Tokobongisa boyangeli ya l'état mpo na ko déclencher événement ya changement ya l'état mpe tokokoma lisusu 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); } } }


Na refiné pe ndenge surfaceHit e traité na 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; }


Na CharacterController , tokosala abonné na événement StateChanged mpe tokobakisa handler.


 // 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(); } }


Tokolongola vérification ya état Grounded na Update mpe tokokende na OnGrounded .


 // CharacterController.cs private void Update() { _characterBody.SetLocomotionVelocity(_locomotionVelocity); } private void OnGrounded() { _isJumping = false; }


Sikoyo, bakisa code mpo na kotala soki esengeli ko déclenché saut.


 // CharacterController.cs private void OnGrounded() { _isJumping = false; if (_jumpActionEndTime > Time.unscaledTime) { _jumpActionEndTime = 0; Jump(); } }


Soki _jumpActionEndTime ezali monene koleka ngonga ya lelo, elakisi ete bouton ya kopumbwa efinamaki kala mingi te, yango wana tozongisaka _jumpActionEndTime mpe tosalaka saut.


Sikawa, meká kopumbwa ntango nyonso elongo na moto yango. Okomona ete bouton ya kopumbwa eyokaka ete ezali koyanola mingi, mpe kotambwisa ezaleli ekomi malamu. Kasi, namonaki ete na makambo mosusu, lokola coin oyo emonisami na elilingi oyo ezali awa na nse, état Grounded ezwa mwa retard, ekataka monyololo ya kopumbwa.

Mpo na kosilisa likambo oyo, natye esika ya Surface Anchor na eteni ya CharacterBody na 0.05 na esika ya 0.01. Valeur oyo ezali komonisa ntaka ya moke na surface moko mpo nzoto ekota na état Grounded .

Kopumbwa na Libanga

Ekoki kozala ete omoni ete koluka kopumbwa ntango ozali kokima na bisika oyo etɛlɛmi, esalaka ntango nyonso te. Ekoki koyoka lokola bouton ya kopumbwa ntango mosusu eyanolaka te.


Oyo ezali moko ya ba subtilités ya ko développer Contrôleur ya Character mpo na ba plateformes 2D. Basani basengeli na likoki ya kopumbwa ata soki bazali mwa na retare mpo na kofina bouton ya kopumbwa. Atako likanisi oyo ekoki komonana lokola likambo ya kokamwa, ezali ndenge oyo mingi ya ba plateformes basalaka. Litomba na yango ezali ete moto moko oyo azali komonana lokola azali kopusana na mopɛpɛ, ndenge emonisami na bililingi oyo ezali awa na nse.



To mettre en œuvre mécanicien oyo. Toko kotisa esika ya sika pona kobomba fenêtre ya temps (na ba secondes) na tango oyo personnage akoki kaka ko sauter sima ya ko perdre état Grounded .


 // CharacterController.cs [Min(0)] [SerializeField] private float _rememberGroundTime = 0.1f;


Tokobakisa mpe esika mosusu mpo na kobomba elembo ya ntango nsima na yango état Grounded "ebosani."


 // CharacterController.cs private float _lostGroundTime;


Etat oyo ekolandama na kosalelaka événement CharacterBody . Tokobongisa mosali OnStateChanged mpo na yango.


 // CharacterController.cs private void OnStateChanged(CharacterState previousState, CharacterState state) { if (state == CharacterState.Grounded) { OnGrounded(); } else if (previousState == CharacterState.Grounded) { _lostGroundTime = Time.unscaledTime + _rememberGroundTime; } }


Ezali na ntina kokesenisa soki personnage abungisaki état Grounded mpo na saut intentionnel to mpo na raison mosusu. Tozali déjà na drapeau _isJumping , oyo e désactivé chaque fois que StopJumping ebengami pona kopekisa ba actions redundantes.


Na décidé na ko introduire drapeau mususu te puisque annulation ya saut redundant e affectaka gameplay te. Bozala na bonsomi ya komeka. Drapeau _isJumping ekolongolama sikoyo kaka tango character akokita sima ya ko sauter. To mettre à jour code na kolanda yango.


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


Na nsuka, tokozongela lolenge ya 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(); } }


Sikawa, kopumbwa longwa na bisika oyo etɛlɛmi ebebisaka lisusu te rythme ya lisano mpe eyokaka mpenza ya bomoto, atako emonani ete ezali bozoba. Moto oyo azali kosala yango akoki mpenza kotindika mopɛpɛ, kokende mosika koleka ndenge emonani ete ezali na ntina. Kasi oyo ezali mpenza oyo esengeli mpo na platformer na biso.

Flip ya bizaleli

Kosimba ya nsuka ezali kosala ete moto oyo azali kosala yango atala na ngámbo ya kotambola. Tokosalela yango na ndenge ya pete — na kobongola échelle ya elembo na nzela ya axe x. Kotiya motuya ya mabe ekosala ete kapitɛni na biso atala na ngámbo mosusu.

Ya liboso, tóbomba échelle originale soki ekeseni na 1.


 // CharacterController.cs public class CharacterController : MonoBehaviour { //... private Vector3 _originalScale; private void Awake() { //... _originalScale = transform.localScale; } }


Sikawa, ntango tozali kokende na lobɔkɔ ya mwasi to ya mobali, tokosalela échelle ya malamu to ya mabe.


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


Tomeka résultat.


Kozinga makambo

Lisolo oyo emonanaki kozala assez détaillé, kasi to réussir ko couvrir ba aspects nionso ya essentiel ya contrôle ya personnage na plateforme 2D. Lokola bokundoli, okoki kotala mbano ya nsuka na etape “ Contrôleur ya bizaleli ” ya ebombelo.


Soki osepelaki to omoni oyo mpe lisolo ya liboso esalisaki, nakosepela na ba likes mpe ba stars na GitHub. Kokakatana te kosimba loboko soki okutani na makambo to omoni mabunga. Matɔndi mingi mpo na likebi na bino!

L O A D I N G
. . . comments & more!

About Author

Denis Kondratev HackerNoon profile picture
Denis Kondratev@deniskondratev
Software Engineer / С++ / C# / Unity / Game Developer

KOKANGA BA ÉTIQUES

ARTICLE OYO EZALAKI PRESENTE NA...