((对不起我的英语不好)) 我在其中一个脚本中创建了一个公共静态布尔变量并将其命名为
inDamage
。然后我根据价值使用它。在另一个脚本中,我使用 RayCast,如果光线撞击对撞机,inDamage = true
,否则 - 错误。这个脚本(带有 RayCast)在几个对象上,也许这就是问题所在。但是如果我删除了这个脚本的所有副本,除了一个,它就不会工作。那么为什么变量的值没有改变呢?
有脚本:
Ray2D ray2D = new Ray2D(transform.position, transform.right);
RaycastHit2D hitInfo = Physics2D.Raycast(ray2D.origin, ray2D.direction, 10);
if (hitInfo.collider.CompareTag("Player"))
{
ScriptName.inDamage = true;
}
else
{
ScriptName.inDamage = false;
}
我试图让这个变量不是静态的,并在声明它的脚本中更改值,它起作用了。但我需要从 RayCast 获取信息。
完整脚本:
public class Light : MonoBehaviour
{
public int DefaultLenght = 10; // Length of ray if it doesn't hit anything
public int MaxReflectionCount = 5; // Max count of ray reflections
private List<Vector3> _hitPoints = new List<Vector3>();
private LineRenderer _lineRenderer;
void Awake()
{
_lineRenderer = GetComponent<LineRenderer>();
}
void FixedUpdate()
{
RaycastWithReflection();
}
private void RaycastWithReflection()
{
_hitPoints.Clear(); // Clearing array of points to move line in a new frame
_hitPoints.Add(transform.position); // Adding starting point into the array
Ray2D ray2D = new Ray2D(transform.position, transform.right);
while (_hitPoints.Count <= MaxReflectionCount) // Line will reflect till it reaches the max count of ray reflections
{
RaycastHit2D hitInfo = Physics2D.Raycast(ray2D.origin, ray2D.direction, 10); // Variable with an information about raycast
if (hitInfo.collider.CompareTag("Mirror")) // Ray will reflect if it hits the mirror
{
_hitPoints.Add(hitInfo.point);
ray2D.origin = hitInfo.point - ray2D.direction * 0.01f;
ray2D.direction = Vector2.Reflect(ray2D.direction, hitInfo.normal);
}
else if (hitInfo.collider.CompareTag("Untagged")) // Ray ends if it collides with a regular wall
{
_hitPoints.Add(hitInfo.point);
}
else // If ray is not colliding with anything its length equals DefaultLength
{
_hitPoints.Add(ray2D.origin + ray2D.direction * DefaultLenght);
}
Vampire.inDamage = hitInfo.collider.CompareTag("Player");
}
_lineRenderer.positionCount = _hitPoints.Count;
_lineRenderer.SetPositions(_hitPoints.ToArray()); // Finally, rendering the ray itself
}
}
我认为问题在于静态 bool 是由 while 循环中的几个光线投射设置的。他们中的大多数将值设置为 false。因此,当光线命中并且 inDamage 设置为 true 时,它会立即被下一个光线投射覆盖为 false。
我想你想要达到的目标是用镜子中的一些反射等来击中玩家
所以实际上,一旦你击中玩家,我猜你只是不想进一步处理任何光线,只是做例如
private void RaycastWithReflection()
{
_hitPoints.Clear(); // Clearing array of points to move line in a new frame
_hitPoints.Add(transform.position); // Adding starting point into the array
Ray2D ray2D = new Ray2D(transform.position, transform.right);
// Line will reflect till it reaches the max count of ray reflections
// OR hits the player
while (_hitPoints.Count <= MaxReflectionCount)
{
var hitInfo = Physics2D.Raycast(ray2D.origin, ray2D.direction, 10);
// Ray will reflect if it hits the mirror
if (hitInfo.collider.CompareTag("Mirror"))
{
_hitPoints.Add(hitInfo.point);
ray2D.origin = hitInfo.point - ray2D.direction * 0.01f;
ray2D.direction = Vector2.Reflect(ray2D.direction, hitInfo.normal);
}
// Ray ENDS if it collides with a regular wall
else if (hitInfo.collider.CompareTag("Untagged"))
{
_hitPoints.Add(hitInfo.point);
// leave the while loop -> no further raycasts!
break;
}
// Ray ENDS if it hits the player and sets Vampire.inDamage
else if(hitInfo.collider.CompareTag("Player"))
{
Vampire.inDamage = true;
// leave the while loop -> no further raycasts!
break;
}
// If ray is not colliding with anything its length equals DefaultLength and ENDS
else
{
_hitPoints.Add(ray2D.origin + ray2D.direction * DefaultLenght);
// leave the while loop -> no further raycasts!
break;
}
}
// Finally, rendering the ray itself
_lineRenderer.positionCount = _hitPoints.Count;
_lineRenderer.SetPositions(_hitPoints.ToArray());
}
然后实际处理
Vampire.inDamage
的人也将负责事后重置它,这样可能有多个此脚本实例就不会成为问题