【问题标题】:Unity2D, instantiated objects keep shakingUnity2D,实例化对象不断抖动
【发布时间】: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);
    }
}
}

Big Boss

Smaller Bosses

感谢您的帮助!

【问题讨论】:

  • 请发布你的boss移动脚本
  • @ryemoss 更新了!
  • 哦,我发现了问题!我没有取消选中 is Kinematic 选项

标签: c# unity3d gameobject unity2d


【解决方案1】:

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

引用 cmets:

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

【讨论】:

    【解决方案2】:

    对我来说,解决问题的是在刚体上选择插值选项

    【讨论】:

      猜你喜欢
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多