繁体   English   中英

Unity2D,实例化对象不断晃动

[英]Unity2D, instantiated objects keep shaking

我正在为2D平台游戏做一个老板,当大老板死后,我在实例化较小的老板​​时遇到了一个问题。 因此,我有一个名为BossHealthManager的脚本,在其中,当老板健康度达到<= 0时,它将实例化2个较小的老板​​。 但是,杀死大老板并实例化2个小老板时,他们一直在当场发抖,从不动弹。 所有的老板都附有一个动作脚本。 因此,我对为什么两个较小的老板​​不动会感到困惑。

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);
    }
}
}

大老板

小老板

感谢帮助!

用户最终取消了预制件刚体上的Kinematic选项,从而解决了他的问题。

引用评论:

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

对我来说,解决问题的方法是在Rigidbody上选择插值选项

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM