ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2020ǯ05·î29Æü(¶â) 15:54:53ÍúÎò
¡¡¡PlayerController¥¯¥é¥¹¤Î½¤Àµ
¡¡¢¿·¤·¤¤¥¹¥¯¥ê¥×¥È¡ÊSpeedItem¡Ë¤ÎºîÀ®
¡¡£¿·¤·¤¤¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¡ÊSpeedUp_Item¡Ë¤ÎºîÀ®
¡¡¤Player¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Î½¤Àµ
¤³¤³¤Ç¤Ï¤½¤ÎmoveSpeedÊÑ¿ô¤òÊѹ¹¤¹¤ë¤¿¤á¤Î´Ø¿ô¤òÍÑ°Õ¤·¡¢¤½¤Î´Ø¿ôÆâ¤ÇmoveSpeed¤òÊѹ¹¤¹¤ë½èÍý¤òÍÑ°Õ¤·¤Æ¤¤¤¤Þ¤¹¡£
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { private float moveSpeed; // Start is called before the first frame update void Start() { moveSpeed = 0.05f; } // Update is called once per frame void Update() { // º¸Ìð°õ¤¬²¡¤µ¤ì¤¿»þ if(Input.GetKey(KeyCode.LeftArrow)) { // º¸¤Ë¡Ö3¡×Æ°¤«¤¹ transform.Translate(-moveSpeed, 0, 0); } // ±¦Ìð°õ¤¬²¡¤µ¤ì¤¿»þ if(Input.GetKey(KeyCode.RightArrow)) { // ±¦¤Ë¡Ö£³¡×Æ°¤«¤¹ transform.Translate(moveSpeed, 0, 0); } } /* °Ê²¼¤òÄɵ¤·¤Þ¤¹ */ /// <summary> /// °Üư®ÅÙ¤òÊѹ¹¤¹¤ë /// ItemSpeed¥¯¥é¥¹¤«¤é¸Æ¤Ó½Ð¤µ¤ì¤ë /// </summary> public void ChangeMoveSpeed(float newMoveSpeed) { moveSpeed = newMoveSpeed; Debug.Log(moveSpeed); } /* ¤³¤³¤Þ¤Ç */ }
¡¡PlayerControoler¥¯¥é¥¹¤Ë°Üư®ÅÙÍѤËmoveSpeedÊÑ¿ô¤È¡¢¤½¤ì¤òÊѹ¹¤¹¤ë¤¿¤á¤ÎChangeMoveSpeed´Ø¿ô¤¬Äɲ䵤ì¤Þ¤·¤¿¤Î¤Ç¡¢
¤³¤³¤Ç¤Ï¤½¤ÎmoveSpeedÊÑ¿ô¤òÊѹ¹¤µ¤»¤ë¤¿¤á¤Î¥¯¥é¥¹¤òºîÀ®¤·¡¢¤½¤ì¤òÍøÍѤ·¤Æ°Üư®ÅÙ¤òÊѹ¹¤¹¤ë¥¢¥¤¥Æ¥à¤òºîÀ®¤·¤Æ¤¤¤¤Þ¤·¤ç¤¦¡£
¡¡¤³¤ì¤Ï¿·¤·¤¯ºîÀ®¤¹¤ë¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¡Ê¥¹¥Ô¡¼¥ÉÊѹ¹ÍÑ¥¢¥¤¥Æ¥à¡Ë¤Ë¥¢¥¿¥Ã¥Á¤¹¤ë¤³¤È¤òÁÛÄꤷ¤Æ¤¤¤Þ¤¹¡£
Ìð¤È°ì½ï¤Ë¤³¤Î¥¯¥é¥¹¤¬¥¢¥¿¥Ã¥Á¤µ¤ì¤¿¥¢¥¤¥Æ¥à¤¬¹ß¤Ã¤Æ¤¤Þ¤¹¤Î¤Ç
¤½¤ì¤ËÀÜ¿¨¤¹¤ë¤³¤È¤ÇPlayer¤Î°Üư®ÅÙ¤¬Êѹ¹¤µ¤ì¤ë¤è¤¦¤Ëºî¤Ã¤Æ¤¤¤¤Þ¤¹¡£
using UnityEngine; /// <summary> /// Player¤Î°Üư®ÅÙ¤òÊѹ¹¤¹¤ë¥¯¥é¥¹ /// ¥¢¥¤¥Æ¥à¤Ë¥¢¥¿¥Ã¥Á¤·¤Æ»ÈÍѤ¹¤ë /// </summary> public class SpeedItem : MonoBehaviour { [Header("Player¤Î°Üư®ÅÙ¤ÎÊѹ¹ÃÍ")] public float speed; void Update() { if (transform.position.y < -5.0f) { // ²èÌ̳°¤Ë¥¢¥¤¥Æ¥à¤¬½Ð¤¿¤éÇ˲õ¤¹¤ë Destroy(gameObject); } } private void OnTriggerEnter2D(Collider2D col) { // ÀÜ¿¨¤·¤¿¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Î¥¿¥°¤¬¡ÉPlayer¡É¤«³Îǧ¤¹¤ë if (col.gameObject.tag == "Player") { // ¡ÉPlayer¡É¤Ç¤¢¤Ã¤¿¤Ê¤é¡¢¤½¤ÎPlayer¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤¬½ê»ý¤·¤Æ¤¤¤ëPlayerController¥¯¥é¥¹¤Î¼èÆÀ¤ò»î¤ß¤ë if (col.gameObject.TryGetComponent(out PlayerController playerController)) { // PlayerController¥¯¥é¥¹¤ò¼èÆÀ¤Ç¤¤¿¤Ê¤é¡¢playerControllerÊÑ¿ô¤ËÂåÆþ¤µ¤ì¤¿PlayerContoller¥¯¥é¥¹¤ÎChangeMoveSpeed´Ø¿ô¤ò¸Æ¤Ó½Ð¤·¡¢°ú¿ô¤È¤·¤ÆspeedÊÑ¿ô¤òÅϤ¹ playerController.ChangeMoveSpeed(speed); } // ¤³¤Î¥¢¥¤¥Æ¥à¤òÇ˲õ¤¹¤ë Destroy(gameObject); } } }
Unity¤Î¥Ò¥¨¥é¥ë¥¡¼¾å¤Ç±¦¥¯¥ê¥Ã¥¯¤ò¤·¤Æ¥á¥Ë¥å¡¼¤òɽ¼¨¤·¡¢[2D Object] => [Sprite]¤òÁªÂò¤·¤Æ
¿·¤·¤¤¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤ò£±¤ÄºîÀ®¤·¤Þ¤·¤ç¤¦¡£
¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Î̾¾Î¤Ï¡ÖSpeedUp_Item¡×¤È¤·¤Þ¤¹¡£
¡¡Sprite¤Î¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤òºîÀ®¤·¤¿¤é¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤ò³Îǧ¤·¤Þ¤·¤ç¤¦¡£
½é´ü¾ðÊó¤È¤·¤Æ¡¢Transform¥³¥ó¥Ý¡¼¥Í¥ó¥È¤ÈSpriteRenderer¥³¥ó¥Ý¡¼¥Í¥ó¥È¤¬¥¢¥¿¥Ã¥Á¤µ¤ì¤Æ¤¤¤Þ¤¹¡£
¡¡Transform¥³¥ó¥Ý¡¼¥Í¥ó¥È¤Î°ÌÃÖ¾ðÊó¤ò³Îǧ¤·¤Þ¤¹¡£
Position¤¬{0, 0, 0}¤Ç¤Ê¤±¤ì¤Ð¡¢±¦¾å¤Î¡Ö¡Ä¡×¥¢¥¤¥³¥ó¤ò¥¯¥ê¥Ã¥¯¤·¡¢Reset¤ò²¡¤·¤Æ°ÌÃÖ¾ðÊó¤òPosition {0, 0, 0}¤Ë½é´ü²½¤·¤Æ¤ª¤¤Þ¤¹¡£
¡¡SpriteRenderer¥³¥ó¥Ý¡¼¥Í¥ó¥È¤Ï2D¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥¹¥×¥é¥¤¥È(³¨)¤òÉÁ²è¤¹¤ë¤¿¤á¤Î¥³¥ó¥Ý¡¼¥Í¥ó¥È¤Ç¤¹¡£
sprite¥×¥í¥Ñ¥Æ¥£¤¬None¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¤Î¤Ç¡¢»Ï¤á¤ÏÇò¤¯É½¼¨¤µ¤ì¤Æ¤¤¤Þ¤¹¡£
¡¡Ç¤°Õ¤Îsprite¤òÀßÄꤷ¤Æ²èÌ̤Ëɽ¼¨¤µ¤»¤Þ¤·¤ç¤¦¡£¤³¤Î¤È¤¡¢ÀßÄê¤Ç¤¤ë³¨¤ÏTextureType¤¬Sprite¤ËÀßÄꤵ¤ì¤Æ¤¤¤ë¤â¤Î¤À¤±¤Ç¤¹¡£
¤â¤·¤âUnityÆâ¤Ë³¨¤ÎÁǺà¡ÊTexture¡Ë¤¬Æþ¤Ã¤Æ¤¤¤ë¤Ë¤â¤«¤«¤ï¤é¤ºÀßÄ꤬½ÐÍè¤Ê¤¤¾ì¹ç¤Ë¤Ï¡¢ÁǺà¤ÎTexutreType¤ò³Îǧ¤·¡¢Sprite(2D or UI)¤ËÊѹ¹¤·¤Æ¤ª¤¤Þ¤·¤ç¤¦¡£
¡¡Â³¤¤¤Æ¡¢¤³¤ÎSpeedUp_Item¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤Ë¡¢¿·¤·¤¤¥³¥ó¥Ý¡¼¥Í¥ó¥È¤òÄɲ䷤Ƥ¤¤¤Þ¤¹¡£
¥¤¥ó¥¹¥Ú¥¯¥¿¡¼¤Î°ìÈÖ²¼¤Ë¤¢¤ë¡ÖAdd Component¡×¤ò¥¯¥ê¥Ã¥¯¤·¡¢¤Þ¤º¤ÏBoxCollider2D¥³¥ó¥Ý¡¼¥Í¥ó¥È¤òÄɲä·¤Þ¤¹¡£
IsTrigger¥×¥í¥Ñ¥Æ¥£¤Î¥Á¥§¥Ã¥¯¤òÆþ¤ì¤Æ¤ª¤¤Þ¤¹¡£
¡¡
¡¡¼¡¤ËRigidBody2D¥³¥ó¥Ý¡¼¥Í¥ó¥È¤òÄɲä·¤Þ¤¹¡£
Constraits¥×¥í¥Ñ¥Æ¥£¤ò¥¯¥ê¥Ã¥¯¤·¡¢¤½¤ÎÃæ¤ÎFreezeRotation.z¤Ë¥Á¥§¥Ã¥¯¤ò¤¤¤ì¤ÆZ¼´¤Î²óž¤òÍ޻ߤ·¤Æ¤ª¤¤Þ¤¹¡£
¤³¤Î£²¤Ä¤ò¥¢¥¿¥Ã¥Á¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢¤³¤Î¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤ÈPlayer¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤È¤ÎÀÜ¿¨È½Äê¤ò¼è¤ì¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£
IsTrigger¤Ë¥Á¥§¥Ã¥¯¤¬Æþ¤Ã¤Æ¤¤¤ë¤Î¤Ç¡¢ColliderƱ»Î¤Ï´¶ÃΤ¹¤ë¤â¤Î¤Î¡¢Ä¾Àܤ֤Ĥ«¤ë¤³¤È¤Ï¤Ê¤¯¤Ê¤ê¤Þ¤¹¡£
¡¡SpeedItem¥¹¥¯¥ê¥×¥È¤ò¥Ò¥¨¥é¥ë¥¡¼¤ÎSpeedUp_Item¤Ë¥É¥é¥Ã¥°¥¢¥ó¥É¥É¥í¥Ã¥×¤·¤Æ¥¢¥¿¥Ã¥Á¤µ¤»¤Þ¤·¤ç¤¦¡£
¤½¤ÎºÝ¡¢Speed¤ÎÃͤ¬0¤Ë¤Ê¤Ã¤Æ¤¤¤ë¤Î¤Ç¡¢¥¢¥¿¥Ã¥Á¤Î³Îǧ¤È°ì½ï¤Ë¡¢Ëº¤ì¤º¤ËÀßÄê¤ò¤·¤Þ¤·¤ç¤¦¡£
¡¡Player¤ÎmoveSpeed¤ÎÃͤ¬ 0.05f ¤Ç¤¹¤Î¤Ç¡¢¤³¤ÎÃͤè¤ê¤âÂ礤ÊÃͤòÀßÄꤹ¤ì¤Ð°Üư®ÅÙ¤ò¾å¤²¤ë¤³¤È¤Ç¤¤Þ¤¹¤«¤é
¤Þ¤º¤Ï0.1f¤Ê¤É¤òÀßÄꤷ¤ÆÄ´À°¤·¤Æ¤¤¤¤Þ¤·¤ç¤¦¡£
CapsuleCollider2D¥³¥ó¥Ý¡¼¥Í¥ó¥È¤òÄɲä·¤Þ¤¹¡£
¡¡Edit Collider¤ò¥¯¥ê¥Ã¥¯¤·¤Æ¡¢Player¤ÎÂ礤µ¤Ë¹ç¤ï¤»¤Æ¥³¥é¥¤¥À¡¼¤ÎÂ礤µ¤âŬµ¹Ä´À°¤·¤Þ¤·¤ç¤¦¡£
¡¡¥²¡¼¥à¤ò¼Â¹Ô¤·¡¢¾å¶õ¤«¤é¹ß¤Ã¤Æ¤¯¤ëSpeedUp_Item¤òPlayer¤ò°ÜÆ°¤·¤ÆÀÜ¿¨¤µ¤»¤Æ¤ß¤Þ¤·¤ç¤¦¡£
SpeedUp_Item¤¬Ç˲õ¤µ¤ì¤ë¤È¤È¤â¤Ë¡¢Player¤Î°Üư®ÅÙ¤¬¾å¾º¤·¤Æ¤¤¤ì¤ÐÀ®¸ù¤Ç¤¹¡£
¤â¤¦°ìÅÙ¥²¡¼¥à¤ò¼Â¹Ô¤·¡¢Scene¤ò³Îǧ¤·¤Ê¤¬¤é¡¢º£ÅÙ¤ÏSpeedUp_Item¤ò¼è¤é¤º¤Ë¸«Æ¨¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
²èÌ̳°¤Ë½Ð¤¿¤È¤¤Ë¡¢SpeedUp_Item¤¬Ç˲õ¤µ¤ì¤Æ¤¤¤ì¤Ð¡¢¤³¤Á¤é¤â½èÍý¤ÏÀ®¸ù¤Ç¤¹¡£
¡¡
¡¡¤³¤Î¤è¤¦¤Ë¡¢¥¢¥¤¥Æ¥à¤¬Ç˲õ¤µ¤ì¤ë¾ì¹ç¤Î½èÍý¤¬Ê£¿ô¤¢¤ë¾ì¹ç¤Ë¤Ï¡¢¤½¤ì¤é¤ò½çÈ֤˳Îǧ¤·¤ÆÌäÂêÅÀ¤¬¤Ê¤¤¤«¤ò³Îǧ¤·¤Æ¤¤¤¤Þ¤·¤ç¤¦¡£
¡¡¼¡¤Ï¤³¤ÎSpeedUp_Item¤òÌð¤Î¤è¤¦¤Ë¥é¥ó¥À¥à¤ËÀ¸À®¤·¤Æ¹ß¤é¤»¤ë½èÍý¤ò¼ÂÁõ¤·¤Æ¤¤¤¤Þ¤¹¡£
ÌäÂê¤Ê¤¯Æ°¤¤¤Æ¤¤¤ì¤Ð¡¢SpeedUp_Item¤ò¥×¥ì¥Õ¥¡¥Ö²½¤·¤Æ¤ª¤¤Þ¤¹¡ÊProjectÆâ¤ÎPrefab¥Õ¥©¥ë¥À¤ËÆþ¤ì¤Æ¤ª¤¤Þ¤¹¡£¡Ë
¤½¤Î¸å¡¢¥Ò¥¨¥é¥ë¥¡¼¤«¤éºï½ü¤·¤Æ¤ª¤¤Þ¤·¤ç¤¦¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- Áí¹ç
¥³¥á¥ó¥È¤ò¤«¤¯