在 Unity 中根据方向启用和禁用碰撞体



我认为这很简单,但由于某种原因,我无法弄清楚。我的玩家周围有 4 个子游戏对象,每个子游戏对象都有自己的碰撞体。我需要能够根据角色移动的方向启用和禁用子游戏对象。因此,如果玩家向右移动,则左侧、顶部和底部碰撞体将被禁用,只保留右侧碰撞体处于活动状态。如果玩家向上移动,则左、右和底部碰撞体将被禁用,只留下顶部碰撞体。其他两个方向的原理相同。但出于某种原因,我的代码适用于左右方向,但是当我的角色向上或向下移动时,它会禁用除左碰撞体之外的所有内容。我可以得到一些帮助吗?这是我的代码:

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

面对右是真或假。这意味着你的第一对if语句(if(facingRight)if(!facingRight))总是为真,所以你的代码永远不会到达第三个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;
}
}