【发布时间】:2017-08-30 16:22:18
【问题描述】:
我正在为我的 2D 平台游戏制作一个 Boss,当大 Boss 死时,我在实例化较小的 Boss 时遇到了问题。所以我有一个名为 BossHealthManager 的脚本,当老板健康达到
public class BossHealthManager : MonoBehaviour {
public int enemyHealth;
public GameObject deathEffect;
public int pointsOnDeath;
public GameObject bossPrefab;
public float minSize;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (enemyHealth <= 0)
{
Instantiate(deathEffect, transform.position, transform.rotation);
ScoreManager.AddPoints(pointsOnDeath);
if(transform.localScale.y > minSize)
{
GameObject clone1 = Instantiate(bossPrefab, new Vector3(transform.position.x + 0.5f, transform.position.y, transform.position.z), transform.rotation) as GameObject;
GameObject clone2 = Instantiate(bossPrefab, new Vector3(transform.position.x - 0.5f, transform.position.y, transform.position.z), transform.rotation) as GameObject;
clone1.transform.localScale = new Vector3(transform.localScale.y * 0.5f, transform.localScale.y * 0.5f, transform.localScale.z);
clone1.GetComponent<BossHealthManager>().enemyHealth = 10;
clone2.transform.localScale = new Vector3(transform.localScale.y * 0.5f, transform.localScale.y * 0.5f, transform.localScale.z);
clone2 .GetComponent<BossHealthManager>().enemyHealth = 10;
}
Destroy(gameObject);
}
}
public void giveDamage(int damageToGive)
{
enemyHealth -= damageToGive;
// play whatever audio attached to the gameobject
GetComponent<AudioSource>().Play();
}
}
这是我给老板的动作脚本:
public class BossPatrol : MonoBehaviour {
public float moveSpeed;
public bool moveRight;
// wall check
public Transform wallCheck;
public float wallCheckRadius;
public LayerMask whatIsWall;
private bool hittingWall;
// edge check
private bool notAtEdge;
public Transform edgeCheck;
private float ySize;
void Start()
{
ySize = transform.localScale.y;
}
void Update()
{
hittingWall = Physics2D.OverlapCircle(wallCheck.position, wallCheckRadius, whatIsWall);
notAtEdge = Physics2D.OverlapCircle(edgeCheck.position, wallCheckRadius, whatIsWall);
if (hittingWall || !notAtEdge)
{
moveRight = !moveRight;
//Debug.Log("hit");
}
// moving right
if (moveRight == true)
{
transform.localScale = new Vector3(-ySize, transform.localScale.y, transform.localScale.z);
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
else if (moveRight == false)
{
transform.localScale = new Vector3(ySize, transform.localScale.y, transform.localScale.z);
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
}
}
感谢您的帮助!
【问题讨论】:
-
请发布你的boss移动脚本
-
@ryemoss 更新了!
-
哦,我发现了问题!我没有取消选中 is Kinematic 选项
标签: c# unity3d gameobject unity2d