如何更改重叠框和 Gizmo 的方向?
How to change the orientation for an overlapbox and a gizmo?
经过大量搜索后,我无法找到如何更改设置的重叠框的旋转以及用于可视化它们的小工具。
//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);
}
}
将 Box Overlap 绘制为小工具以显示它当前正在测试的位置
要让重叠框继承变换的旋转,您可以使用 transform.rotation
而不是 Quaternion.identity
来进行重叠框的旋转。
对于 gizmo,它稍微复杂一些。解决此问题的一种方法是将 Gizmo matrix
更改为具有 Gizmos.matrix = transform.localToWorldMatrix
的局部变换矩阵,这将使 Gizmo 继承玩家的旋转。但是,它也会使 Gizmo 的位置相对于玩家的本地位置。因此,在绘制 Gizmo 之前,您需要将世界位置 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;
}
}
经过大量搜索后,我无法找到如何更改设置的重叠框的旋转以及用于可视化它们的小工具。
//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);
}
}
将 Box Overlap 绘制为小工具以显示它当前正在测试的位置
要让重叠框继承变换的旋转,您可以使用 transform.rotation
而不是 Quaternion.identity
来进行重叠框的旋转。
对于 gizmo,它稍微复杂一些。解决此问题的一种方法是将 Gizmo matrix
更改为具有 Gizmos.matrix = transform.localToWorldMatrix
的局部变换矩阵,这将使 Gizmo 继承玩家的旋转。但是,它也会使 Gizmo 的位置相对于玩家的本地位置。因此,在绘制 Gizmo 之前,您需要将世界位置 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;
}
}