我正在爲我的2D平臺遊戲做老闆,當大老闆死後,我遇到了一個實例化小老闆的問題。所以我有一個名爲BossHealthManager的腳本,當boss的生命值達到< = 0時,它會實例化兩個更小的boss。但是,當殺死大老闆並將兩個小老闆實例化時,他們一直在場晃動,從不動彈。所有的老闆都附有一個運動腳本。所以我很困惑,爲什麼兩個小老闆不會動。Unity2D,實例化對象不斷晃動
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);
}
}
}
感謝您的幫助!
的插值方法,請發表您的腳本老闆 – ryeMoss
@ryemoss更新的運動! – Daaenerys
哦,我發現了問題!我沒有取消選中是Kinematic選項 – Daaenerys