如何更改重叠框和小控件的方向?



经过大量搜索,我无法找到如何更改设置的重叠框的旋转以及用于可视化它们的小工具。

//draw a hitbox in front of the character to see which objects it collides with
Vector3 boxPosition = transform.position + (Vector3.up * lastAttack.collHeight) 
+ Vector3.right * lastAttack.collDistance;
Vector3 boxSize = new Vector3 (lastAttack.CollSize/2, lastAttack.CollSize/2, hitZRange/2);
Collider[] hitColliders = Physics.OverlapBox(boxPosition, boxSize, Quaternion.identity, 
HitLayerMask);

我用它来计算损坏。我希望 OverlapBox 采用与玩家相同的旋转,并始终在玩家的前面。

void OnDrawGizmos(){
if (lastAttack != null && (Time.time - lastAttackTime) < lastAttack.duration) {
Gizmos.color = Color.red;
Vector3 boxPosition = transform.position + (Vector3.up * lastAttack.collHeight) 
+ Vector3.right * ((int)lastAttackDirection * lastAttack.collDistance);
Vector3 boxSize = new Vector3 (lastAttack.CollSize, lastAttack.CollSize, hitZRange);
Gizmos.DrawWireCube (boxPosition, boxSize);
}
}

将框重叠绘制为小控件以显示当前正在测试的位置

若要使重叠框继承转换的旋转,可以使用transform.rotation而不是Quaternion.identity来表示重叠框的旋转。

对于小玩意儿来说,它有点复杂。解决此问题的一种方法是将 Gizmomatrix更改为带有Gizmos.matrix = transform.localToWorldMatrix的局部变换矩阵,这将使小控件继承玩家的旋转。但是,这也会使小玩意的位置相对于玩家的本地位置。因此,在绘制小控件之前,您需要将世界位置boxPosition转换为本地位置。您可以使用transform.InverseTransformPoint来执行此操作。

您可能希望将小控件设置还原到以前的设置,否则可能会导致在使用Gizmos的其他地方出现意外行为。

完全:

//draw a hitbox in front of the character to see which objects it collides with
Vector3 boxPosition = transform.position + (Vector3.up * lastAttack.collHeight) 
+ Vector3.right * lastAttack.collDistance;
Vector3 boxSize = new Vector3 (lastAttack.CollSize/2, lastAttack.CollSize/2, hitZRange/2);
Collider[] hitColliders = Physics.OverlapBox(boxPosition, boxSize,
transform.rotation, HitLayerMask);
...
void OnDrawGizmos(){
if (lastAttack != null && (Time.time - lastAttackTime) < lastAttack.duration) {
// cache previous Gizmos settings
Color prevColor = Gizmos.color;
Matrix4x4 prevMatrix = Gismos.matrix;
Gizmos.color = Color.red;
Gizmos.matrix = transform.localToWorldMatrix; 
Vector3 boxPosition = transform.position + (Vector3.up * lastAttack.collHeight) 
+ Vector3.right * ((int)lastAttackDirection * lastAttack.collDistance);
// convert from world position to local position 
boxPosition = transform.InverseTransformPoint(boxPosition); 
Vector3 boxSize = new Vector3 (lastAttack.CollSize, lastAttack.CollSize, hitZRange); 
Gizmos.DrawWireCube (boxPosition, boxSize);
// restore previous Gizmos settings
Gizmos.color = prevColor;
Gizmos.matrix = prevMatrix;
}
}

最新更新