2017-08-30 43 views
0

我正在为我的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); 
    } 
} 
} 

Big Boss

Smaller Bosses

感谢您的帮助!

+0

的插值方法,请发表您的脚本老板 – ryeMoss

+0

@ryemoss更新的运动! – Daaenerys

+1

哦,我发现了问题!我没有取消选中是Kinematic选项 – Daaenerys

回答

0

用户最终通过取消选中预制件刚体上的Kinematic选项来解决他的问题。

引述评论:

哦,我发现了这个问题!我没有取消选中的是运动的选择

0

对我来说有什么解决的问题是选择对刚体