在Unity中基于方向启用和禁用对撞机

时间:2019-03-24 23:49:21

标签: c# unity3d 2d collider

我认为这很简单,但是由于某些原因,我无法弄清楚。我的玩家周围有4个子GameObject,每个都有它自己的对撞机。我需要能够根据角色移动的方向启用和禁用子GameObjects。因此,如果玩家向右移动,则将禁用顶部和底部对撞机,而仅使右侧对撞机处于活动状态。如果玩家向上移动,则将禁用“左”,“右”和“底部”对撞机,仅留下“顶部”对撞机。这与其他两个方向的原理相同。但是由于某种原因,我的代码可同时用于左右方向,但是当我的角色向上或向下移动时,它将禁用除左对撞机之外的所有内容。我可以帮忙吗?这是我的代码:

public BasicNPCPrototype NPCPrototype;
    private bool nearNPC = false;

    public bool facingRight;
    public bool facingUp;

    public GameObject RightCollider;
    public GameObject LeftCollider;
    public GameObject TopCollider;
    public GameObject BottomCollider;

    // Use this for initialization
    protected override void Start()
    {
        base.Start();
    }

    // Update is called once per frame
    protected override void Update()
    {
        // Call the GetInput Function
        GetInput();

        // Call the CheckIfNear function
        CheckIfNear();

        // Call the CheckDirection function
        CheckDirection();

        // Call the DisableInteractionColliders function
        DisableInteractionColliders();

        base.Update();
    }

    void GetInput()
    {
        if(playerActive)
        {
            direction = Vector2.zero;

            // Get the horizontal Axis and put in the X value of "direction"
            direction.x = Input.GetAxisRaw("Horizontal");
            // Get the Vertical Axis and put in the Y value of "direction"
            direction.y = Input.GetAxisRaw("Vertical");
        }    
    }

    // Look at our movement direction and decide which way were facing
    void CheckDirection()
    {
        if (direction.x > 0)
        {
            facingRight = true;
            facingUp = false;
        }
        else if (direction.x < 0)
        {
            facingRight = false;
            facingUp = false;
        }

        if (direction.y > 0) // && !facingRight)
        {
            facingUp = true;
            facingRight = false;
        }
        else if (direction.y < 0) // && !facingRight)
        {
            facingUp = false;
            facingRight = false;
        }

    }

    // Look at what direction the Player is facing and disable/enable the correct colliders to account for it
    void DisableInteractionColliders()
    {
        // WIP
        if(facingRight)
        {
            RightCollider.SetActive(true);
            LeftCollider.SetActive(false);
            TopCollider.SetActive(false);
            BottomCollider.SetActive(false);
        } else if(!facingRight)
        {
            LeftCollider.SetActive(true);
            RightCollider.SetActive(false);
            TopCollider.SetActive(false);
            BottomCollider.SetActive(false);
        }

         else if(facingUp)
        {
            TopCollider.SetActive(true);
            RightCollider.SetActive(false);
            LeftCollider.SetActive(false);
            BottomCollider.SetActive(false);
        } else if(!facingUp)
        {
            BottomCollider.SetActive(true);
            RightCollider.SetActive(false);
            LeftCollider.SetActive(false);
            TopCollider.SetActive(false);
        }
    }

如果更容易阅读,则可以在PasteBin上重新阅读:https://pastebin.com/y1jQT17w

忽略CheckIfNear();调用更新功能,这是为了将来的计划,但当前为空。

1 个答案:

答案 0 :(得分:2)

    if(facingRight)
    {
        RightCollider.SetActive(true);
        LeftCollider.SetActive(false);
        TopCollider.SetActive(false);
        BottomCollider.SetActive(false);
    } 
    else if(!facingRight)
    {
        LeftCollider.SetActive(true);
        RightCollider.SetActive(false);
        TopCollider.SetActive(false);
        BottomCollider.SetActive(false);
    }

    else if(facingUp)
    {
        TopCollider.SetActive(true);
        RightCollider.SetActive(false);
        LeftCollider.SetActive(false);
        BottomCollider.SetActive(false);
    } 
    else if(!facingUp)
    {
        BottomCollider.SetActive(true);
        RightCollider.SetActive(false);
        LeftCollider.SetActive(false);
        TopCollider.SetActive(false);
    }

faceingRight为True或False。这意味着您的第一对if语句(if(facingRight)if(!facingRight))始终为true,因此您的代码将永远不会到达第三个if语句,并且永远不会使用变量FaceingUp。

如果我是我,我将分配一个整数来存储方向,并使用开关选择对撞机。像这样:

int dir = 0;

void CheckDirection()
{
    if (direction.x > 0)
    {
        dir = 1;
    }
    else if (direction.x < 0)
    {
        dir = 2;
    }

    if (direction.y > 0)
    {
        dir = 3;
    }
    else if (direction.y < 0)
    {
        dir = 4;
    }

}

void DisableInteractionColliders()
{
        LeftCollider.SetActive(false);
        RightCollider.SetActive(false);
        TopCollider.SetActive(false);
        BottomCollider.SetActive(false);

        switch(dir)
        {
            case 1: RightCollider.SetActive(true);
                    break;
            case 2: LeftCollider.SetActive(true);
                    break;
            case 3: TopCollider.SetActive(true);
                    break;
            case 4: BottomCollider.SetActive(true);
                    break;

            default: break;
        }
}