((对不起我英语不好意思))我在其中一个脚本中创建了一个公共静态bool变量,命名为inDamage
。然后我使用它,取决于它的价值。在另一个脚本中,我使用RayCast,如果光线击中碰撞器,inDamage = true
,else - false。这个脚本(使用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;
}
我试图使这个变量不是静态的,并改变脚本中声明它的值,它工作。但我需要从光线投射仪上获取信息
完整脚本:
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
}
}
2条答案
按热度按时间nbysray51#
我想你要做的是用镜子中的反射来打击玩家
所以实际上,当你击中球员,我猜你只是想不进一步处理任何射线,只是做e。g的。
那么实际处理
Vampire.inDamage
的人也将负责在之后重置它,这样就不会有潜在的多个脚本示例的问题csga3l582#
我认为问题在于静态bool是由while循环中的几个光线投射设置的。而且大多数都将值设置为false。因此,当光线命中并且inDamage设置为true时,它会立即被下一个光线投射覆盖为false。