簡體   English   中英

在Unity中基於方向啟用和禁用對撞機

[英]Enabling and Disabling Collider based on direction in Unity

我認為這很簡單,但是由於某些原因,我無法弄清楚。 我的玩家周圍有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(); 調用更新功能,因為這是將來的計划,但當前為空。

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

faceRight是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;
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM