switch语句上的gameobjects(col.tag)

时间:2019-02-25 21:57:06

标签: c# unity3d

我现在正在开发游戏。似乎更像是在2d模式下捕获标志。但是我的角色脚本有问题。我想要的是,如果我的角色带有旗帜,并且他带着旗帜回到她的基地(游戏对象已打开),她将前往现场。但问题是,我也希望她在游戏对象未打开时不要继续进行Winscene。

这是剧本

GameObject[] toEnable, toDisable;

public GameObject CharFlag;   
private Rigidbody2D rb;
private Animator anim;
private float moveSpeed;
private float dirX;
private bool facingRight = true;
private Vector3 localScale;


//Use this for Initialization
private void Start(){
    rb = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
    localScale = transform.localScale;
    moveSpeed = 5f;

    // Finding game objects with tags "ToEnable" and "ToDisable"
    toEnable = GameObject.FindGameObjectsWithTag ("CharFlag");


    // Disabling game objects with tag "ToEnable"
    foreach (GameObject element in toEnable)
    {
        element.gameObject.SetActive (false);
    }



}   

//Update is called once per frame
private void Update()
{
    dirX = CrossPlatformInputManager.GetAxisRaw ("Horizontal") * moveSpeed;

    if (CrossPlatformInputManager.GetButtonDown ("Jump") && rb.velocity.y == 0)
        rb.AddForce(Vector2.up * 700f);

    if (Mathf.Abs(dirX) > 0 && rb.velocity.y == 0)
        anim.SetBool("isRunning", true);
    else
        anim.SetBool("isRunning", false);

    if (rb.velocity.y == 0) 
    {
        anim.SetBool("isJumping", false);
        anim.SetBool("isFalling", false);
    }

    if (rb.velocity.y > 0) 
    {
        anim.SetBool("isJumping", true);
    }

    if (rb.velocity.y < 0) 
    {
        anim.SetBool("isJumping", false);
        anim.SetBool("isFalling", true);
    }

}

private void FixedUpdate()
{
    rb.velocity = new Vector2(dirX, rb.velocity.y);
}

private void LateUpdate()
{
    if (dirX > 0)
        facingRight = true;
    else if (dirX < 0)
        facingRight = false;

    if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
        localScale.x *= -1;

    transform.localScale = localScale;
}



void OnTriggerEnter2D (Collider2D col)
{
    switch (col.tag) {

    case "EnemyField":
        CharFlag.gameObject.SetActive (true);
        break;

    case "AlyField":
        SceneManager.LoadScene ("YouWin");
        break;
}

}

}

1 个答案:

答案 0 :(得分:1)

这可以作为评论更好,但我只有40个声誉。

只需检查GameObject是否处于活动状态。

switch (col.tag) 
{
case "EnemyField":
    CharFlag.gameObject.SetActive (true);
    break;

case "AllyField":
    if(CharFlag.gameObject.activeSelf)
    {
        SceneManager.LoadScene ("YouWin");
    }

    break;