根据Unity中的方向启用和禁用碰撞器

问题描述 投票:1回答:1

我认为这很简单,但出于某种原因,我无法弄清楚这一点。我的玩家周围有4个孩子游戏对象,每个游戏对象都有自己的对手。我需要能够根据角色移动的方向启用和禁用子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();调用更新功能,因为它是未来的计划,但它目前是空的。

c# unity3d 2d collider
1个回答
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);
    }

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

如果我是你,我将指定使用整数来存储方向并使用开关选择碰撞器。像这样:

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;
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.