using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class moving : MonoBehaviour
{
public Rigidbody2D rigid2D;
public Animator player;
public int hp;
public int max_hp;
public Image change;
//加入血量的設定與血條圖
void Start()
{
max_hp = 4;
hp = max_hp;
//設定最大血量
}
void Update()
{
//之前寫的角色控制暫時忽略
change.transform.localScale = new Vector3((float)hp / (float)max_hp, change.transform.localScale.y, change.transform.localScale.z);
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "frog")
{
Debug.Log("hit");
hp -= 1;
}
}
}
change.transform.localScale = new Vector3((float)hp / (float)max_hp, change.transform.localScale.y, change.transform.localScale.z);
這裡是想利用碰撞到敵人觸發減少HP的機制,來讓Scale的X軸進行變換,但是沒有成功的呈現,因此懷疑是在碰撞的地方發生問題。 {Debug.Log("hit");}
希望在發生碰撞時能在控制視窗中顯示出來,並且在後面也加入hp -= 1;
,讓之前的程式能偵測到HP-1了,使血量條變換,最後可惜的是失敗了!!!,可是在遊玩的過程中,角色確實會和敵人發生碰撞。1.https://www.youtube.com/watch?v=dX1ICR6ZVWw&list=PLh_nXDYbtPwYC9qESAnTw4ZfAUoYgi3MH&index=5&ab_channel=%E9%98%BF%E7%A9%BA%E7%9A%84%E9%81%8A%E6%88%B2%E9%83%A8%E5%B1%8B
2.http://www.cg.com.tw/UnityHealthBar/
3.https://cindyalex.pixnet.net/blog/post/239811343-unity-c%23-%E8%A1%80%E6%A2%9D%E6%95%88%E6%9E%9C