【每日一句:清晨和夜晚都請用盡全力去生活】
目錄
一、環(huán)境搭建
二、人物
三、相機跟隨人物移動
四、平鋪精靈
五、血條跟隨敵人行走
六、腳本邏輯
【玩家行走方法】
【玩家跳躍方法】
【改變玩家血量值方法】
【創(chuàng)建玩家子彈方法】
【主角血量,改變血條遮罩】
【敵人(減血物體)觸發(fā)檢測,調用玩家改變血量值方法ChangeHealth(int amount)】
【敵人來回移動代碼】
【改變敵人血量方法EnemyChangeHealth() 引用敵人血量腳本EnemyHealth EnemySetValue(currentHealth / (float)maxHealth);】
一、環(huán)境搭建
1.瓦片地圖Tilemaps
創(chuàng)建 2DObject——>Tilemap——>Rectangular
?????? ?Window——>2D——>Tile Palette
瓦片集 Sprite Mode從Single改為Multiple
???????????? Pixel Per Unit值從100更改為64
2.添加瓦片地圖碰撞:Tilemap Collider2D組件
選擇的瓦片不再被視為碰撞體:在Inspector找到Collider Type屬性,Sprite更改為None
優(yōu)化瓦片地圖碰撞體Composite Collider2D組件,會自動添加Rigidbody2D,在Tilemap Collider2D,啟用Used By Composite
在Rigidbody2D,將Rigidbody Body Type屬性設為Static
二、人物
動畫
Right_Run——>Left_Run
單擊Add Property,然后單擊Sprite Render旁邊的三角形,再單擊Flip X旁邊的+圖標
使用混合樹:
【待完善……】
【上下左右移動】
三、相機跟隨人物移動
使用Cinemachine包
Window——>PackageManager——>Cinemachine
使用GameObject——>Cinemachine——>Virtual Camera
Cinemachine Virtual Camera組件
調整Orthgraphic Size(攝像機的一半高度內可容納多少個單位);Follow 跟隨主角
四、平鋪精靈
1.首先確保游戲對象的縮放在Transform組件1:1:1
2.在SpriteRender組件中將Draw Mode設為Tiled,Tile Mode改為Adaptive
精靈的Inspector面板,Mesh Type改為Full Rect
五、血條跟隨敵人行走
public class FollowBlood : MonoBehaviour
{
??? public Transform player;
??? public Image hp;
??? public? Camera cam;
??? void Update()
??? {
??????? //把人物的坐標轉化到屏幕坐標
??????? var playerScreenPos = cam.WorldToScreenPoint(player.position);
??????? //再把人物坐標Y加一個高度給到人物
??????? hp.rectTransform.position = new Vector2(playerScreenPos.x, playerScreenPos.y + 35f);
??? }文章來源地址http://www.zghlxwxcb.cn/news/detail-485228.html
???
}
六、腳本邏輯
【玩家行走方法】
void Movement()
??? {
??????? float horizontal = Input.GetAxisRaw("Horizontal");
??????? rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
??????? Vector2 move = new Vector2(horizontal, 0);
??????? if (horizontal != 0)
??????? {
??????????? //lookDir.Set(move.x, 0);
??????????? lookDir.Normalize();
??????????? ani.SetBool("IsSpeed",true);
??????? }
??????? else? if(horizontal == 0)
??????? {
??????????? ani.SetBool("IsSpeed", false);
??????? }
??????? //Debug.Log(horizontal);
??????? if (horizontal > 0)
? ??????{
??????????? ani.SetFloat("LookX", 1);
??????? }
??????? else if (horizontal < 0)
??????? {
??????????? ani.SetFloat("LookX", 0f);
??????? }
??????
}
【玩家跳躍方法】
void Jump()
??? {
??????? if (isGround)
??????? {
??????????? jumpCount = 2;
??????? }
??????? if (isJump && isGround)
??????? {
??????????? rb.velocity = new Vector2(rb.velocity.x, jumpForce);
??????????? //jumpForce--;
??????????? isJump = false;
??????? }
??????? else if (isJump && !isGround && jumpCount > 0)
??????? {
???????? ???rb.velocity = new Vector2(rb.velocity.x, jumpForce);
??????????? jumpCount--;
??????????? isJump = false;
??????? }
【改變玩家血量值方法】
public void ChangeHealth(int amount)
??? {
??????? if (amount < 0)
??????? {
??????????? if (isInvincible)
??????????????? return;
??????????? isInvincible = true;
??????????? invincibleTimer = timerInvincible;
??????? }
??????? currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
??????? UIHealth.instance.SetValue(currentHealth / (float)maxHealth);
??????? Debug.Log(currentHealth + "/" + maxHealth);文章來源:http://www.zghlxwxcb.cn/news/detail-485228.html
??? }
【創(chuàng)建玩家子彈方法】
void CreatBullet()
??? {
??????? GameObject bul = Instantiate(bullet, rb.position + Vector2.right * 0.5f,Quaternion.identity) as GameObject;
??????? Projectile proj = bul.GetComponent<Projectile>();
??????? proj.Launch(300);
??? }
—————————————————————————————————————————
【主角血量,改變血條遮罩】
public class UIHealth : MonoBehaviour
{
??? public static UIHealth instance
??? {
??????? get;private set;
??? }
??? public Image mask;
??? float originalSize;
??? private void Awake()
??? {
??????? instance = this;
??? }
??? // Start is called before the first frame update
??? void Start()
??? {
??????? originalSize = mask.rectTransform.rect.width;
??? }
??? public void SetValue(float value)
??? {
??????? mask.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, originalSize * value);
??? }
}
【敵人(減血物體)觸發(fā)檢測,調用玩家改變血量值方法ChangeHealth(int amount)】
private void OnTriggerStay2D(Collider2D collision)
??? {
??????? PlayerController controller = collision.GetComponent<PlayerController>();
??????? if (controller != null)
??????? {
??????????? controller.ChangeHealth(-1);
??????? }
??? }
【敵人來回移動代碼】
void Update()
??? {
??????? timer -= Time.deltaTime;
??????? if (timer < 0)
??????? {
??????????? direction = -direction;
??????????? timer = changeTime;
??????? }
??????? if (direction == 1)
??????? {
??????????? ani.SetBool("isLeft", true);
??????? }
??????? else
??????? {
??????????? ani.SetBool("isLeft", false);
??????? }
}
private void FixedUpdate()
??? {
??????? Vector2 position = rb.position;
??????? if (vertical)
??????? {
??????????? position.y = position.y + Time.deltaTime * speed * direction;
??????? }
??????? else
??????? {
??????????? position.x = position.x + Time.deltaTime * speed * direction;
??????? }
??????? rb.MovePosition(position);
??? }
【改變敵人血量方法EnemyChangeHealth() 引用敵人血量腳本EnemyHealth EnemySetValue(currentHealth / (float)maxHealth);】
public void EnemyChangeHealth(int amount)
??? {
??????
??????? currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
??????? Debug.Log("敵人當前血量+" + currentHealth + amount);
??????? eh.EnemySetValue(currentHealth / (float)maxHealth);
??????? Debug.Log(currentHealth + "/" + maxHealth);
??? }
到了這里,關于Unity——2D小游戲筆記整理的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!