/// <summary> /// Ignore the specified _mask. /// </summary> /// <param name="_mask">Mask.</param> public LayerMask Ignore(LayerMask _mask) { foreach (string _name in m_Layers) { int _layer = LayerMask.NameToLayer(_name); if (_layer != -1 && SystemTools.IsInLayerMask(_layer, _mask)) { _mask |= (1 >> _layer); } } return(_mask); }
public Vector3 Scan(Transform _transform, Vector3 _position, LayerMask _ground_mask, LayerMask _water_mask, float _offset) { if (ScanningRange == 0 || ScanningAngle == 0 || (!UseSlopeLimits && !AvoidWater)) { return(_position); } if (Owner == null) { SetOwner(_transform.gameObject); } RaycastHit _hit; Vector3 _pos = PositionTools.GetDirectionPosition(_transform, 0, ScanningRange); _offset += m_VerticalRaycastOffset + ScanningRange; _pos.y += _offset; if (UseSlipping && MaxSurfaceSlopeAngle > 0) { if (Physics.Raycast(_transform.position + Vector3.up, Vector3.down, out _hit, Mathf.Infinity, _ground_mask, WorldManager.TriggerInteraction)) { if (Mathf.Abs(Vector3.Angle(_hit.normal, Vector3.up)) > MaxSurfaceSlopeAngle) { m_SlopePathPosition = Vector3.zero; _transform.position += new Vector3(_hit.normal.x * 9.8f * Time.deltaTime, 0, _hit.normal.z * 9.8f * Time.deltaTime); return(_position); } } } if (m_SlopePathPosition == Vector3.zero || PositionTools.Distance(_transform.position, m_SlopePathPosition) < ScanningRange * 0.25f) { LayerMask _combined_mask = _ground_mask; if (_water_mask.value != 0) { _combined_mask |= _water_mask; } if (Physics.Raycast(_pos, Vector3.down, out _hit, Mathf.Infinity, _combined_mask, WorldManager.TriggerInteraction)) { Vector3 _dir = (_hit.point - _transform.position).normalized; float _path_angle = Vector3.Angle(_dir, Vector3.down) - 90; float _surface_angle = Vector3.Angle(_hit.normal, Vector3.up); if ((MaxSurfaceSlopeAngle > 0 && Mathf.Abs(_surface_angle) > MaxSurfaceSlopeAngle) || (MaxPathSlopeAngle > 0 && Mathf.Abs(_path_angle) > MaxPathSlopeAngle) || (AvoidWater && SystemTools.IsInLayerMask(_hit.transform.gameObject, _water_mask))) { DebugLine(_hit.point, _hit.point + (Vector3.up * 2), Color.yellow); for (int i = ScanningAngle; i <= 180; i += ScanningAngle) { Vector3 _pos_right = Vector3.zero; Vector3 _pos_left = Vector3.zero; int _right_angle = i; _pos = PositionTools.GetDirectionPosition(_transform, _right_angle, ScanningRange); _pos.y += _offset; if (Physics.Raycast(_pos, Vector3.down, out _hit, Mathf.Infinity, _combined_mask, WorldManager.TriggerInteraction)) { _dir = (_hit.point - _transform.position).normalized; _path_angle = Vector3.Angle(_dir, Vector3.down) - 90; _surface_angle = Vector3.Angle(_hit.normal, Vector3.up); bool _walkable_right = true; bool _water = false; if (MaxSurfaceSlopeAngle > 0 && Mathf.Abs(_surface_angle) > MaxSurfaceSlopeAngle) { _walkable_right = false; } if (MaxPathSlopeAngle > 0 && _walkable_right && Mathf.Abs(_path_angle) > MaxPathSlopeAngle) { _walkable_right = false; } if (AvoidWater && SystemTools.IsInLayerMask(_hit.transform.gameObject, _water_mask)) { _walkable_right = false; _water = true; } if (DebugRayIsEnabled) { float _h = (MaxPathSlopeAngle > 0 ? MathTools.Normalize(MaxPathSlopeAngle - Mathf.Abs(_path_angle), 0, MaxPathSlopeAngle) : MathTools.Normalize(MaxSurfaceSlopeAngle - _surface_angle, 0, MaxSurfaceSlopeAngle)); //DebugLine( _pos, _hit.point, ( _water ? Color.blue : ( _walkable_right ? Color.green : new HSBColor( _h * 0.3333333f, 1f, 1f ).ToColor() ) ) ); DebugLine(_hit.point, _hit.point + (Vector3.up * 2), (_water ? Color.blue : (_walkable_right ? Color.green : new HSBColor(_h * 0.3333333f, 1f, 1f).ToColor()))); } if (_walkable_right) { _pos_right = _hit.point; } } else { m_VerticalRaycastOffset += 0.25f; } int _left_angle = 360 - i; _pos = PositionTools.GetDirectionPosition(_transform, _left_angle, ScanningRange); _pos.y += _offset; if (Physics.Raycast(_pos, Vector3.down, out _hit, Mathf.Infinity, _combined_mask, WorldManager.TriggerInteraction)) { _dir = (_hit.point - _transform.position).normalized; _path_angle = Vector3.Angle(_dir, Vector3.down) - 90; _surface_angle = Vector3.Angle(_hit.normal, Vector3.up); bool _walkable_left = true; bool _water = false; if (MaxSurfaceSlopeAngle > 0 && Mathf.Abs(_surface_angle) > MaxSurfaceSlopeAngle) { _walkable_left = false; } if (MaxPathSlopeAngle > 0 && _walkable_left && Mathf.Abs(_path_angle) > MaxPathSlopeAngle) { _walkable_left = false; } if (AvoidWater && SystemTools.IsInLayerMask(_hit.transform.gameObject, _water_mask)) { _walkable_left = false; _water = true; } if (DebugRayIsEnabled) { float _h = (MaxPathSlopeAngle > 0 ? MathTools.Normalize(MaxPathSlopeAngle - Mathf.Abs(_path_angle), 0, MaxPathSlopeAngle) : MathTools.Normalize(MaxSurfaceSlopeAngle - _surface_angle, 0, MaxSurfaceSlopeAngle)); //DebugLine( _pos, _hit.point, ( _water ? Color.blue : ( _walkable_left ? Color.green : new HSBColor( _h * 0.3333333f, 1f, 0.25f ).ToColor() ) ) ); DebugLine(_hit.point, _hit.point + (Vector3.up * 2), (_water ? Color.blue : (_walkable_left ? Color.green : new HSBColor(_h * 0.3333333f, 1f, 1f).ToColor()))); } if (_walkable_left) { _pos_left = _hit.point; } } else { m_VerticalRaycastOffset += 0.25f; } if (_pos_right != Vector3.zero && _pos_left != Vector3.zero) { //if( Vector3.Distance( _position, _pos_right ) <= Vector3.Distance( _position, _pos_left ) ) if (UnityEngine.Random.Range(0, 2) == 0) { m_SlopePathPosition = _pos_right; } else { m_SlopePathPosition = _pos_left; } break; } else if (_pos_right != Vector3.zero) { m_SlopePathPosition = _pos_right; break; } else if (_pos_left != Vector3.zero) { m_SlopePathPosition = _pos_left; break; } } } else { m_SlopePathPosition = Vector3.zero; if (DebugRayIsEnabled) { float _h = (MaxPathSlopeAngle > 0 ? MathTools.Normalize(MaxPathSlopeAngle - Mathf.Abs(_path_angle), 0, MaxPathSlopeAngle) : MathTools.Normalize(MaxSurfaceSlopeAngle - _surface_angle, 0, MaxSurfaceSlopeAngle)); DebugLine(_pos, _hit.point + (Vector3.up * 2), new HSBColor(_h * 0.3333333f, 1f, 0.25f).ToColor()); DebugLine(_hit.point, _hit.point + (Vector3.up * 2), new HSBColor(_h * 0.3333333f, 1f, 1f).ToColor()); } } } else { m_VerticalRaycastOffset += 0.25f; } } if (m_SlopePathPosition != Vector3.zero) { DebugLine(m_SlopePathPosition, m_SlopePathPosition + (Vector3.up * 2), Color.green); _position = m_SlopePathPosition; } return(_position); }
public bool Contains(int _layer) { return(SystemTools.IsInLayerMask(_layer, Mask)); }