public static void ArrowArc(Transform _target, float _radius, float _degree, float _left, float _right, float _level, bool _invert = false)
        {
            if (_target == null || _degree == 0 || _radius == 0)
            {
                return;
            }



            for (float _angle = _left; _angle <= _right + 0.15f; _angle += _degree)
            {
                Vector3 _pos_1 = PositionTools.GetDirectionPosition(_target, _angle, _radius);
                Vector3 _pos_2 = PositionTools.GetDirectionPosition(_target, _angle, _radius + 1);

                _pos_1.y = _level;
                _pos_2.y = _level;

                if (_invert)
                {
                    Arrow(_pos_2, _pos_1 - _pos_2);
                }
                else
                {
                    Arrow(_pos_1, _pos_2 - _pos_1);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the nearest object.
        /// </summary>
        /// <returns>The nearest object.</returns>
        /// <param name="_objects">Objects.</param>
        /// <param name="_position">Position.</param>
        /// <param name="_distance">Distance.</param>
        public static GameObject GetNearestObject(GameObject[] _objects, Vector3 _position, float _distance)
        {
            if (_objects == null || _objects.Length == 0)
            {
                return(null);
            }

            if (_distance == 0)
            {
                _distance = Mathf.Infinity;
            }

            GameObject _best_object   = null;
            float      _best_distance = _distance;

            for (int i = 0; i < _objects.Length; i++)
            {
                GameObject _object = _objects[i];

                if (_object != null && _object.activeInHierarchy && _object.transform.position != _position)
                {
                    float _tmp_distance = PositionTools.Distance(_position, _object.transform.position);

                    if (_tmp_distance < _best_distance)
                    {
                        _best_distance = _tmp_distance;
                        _best_object   = _object;
                    }
                }
            }

            return(_best_object);
        }
Пример #3
0
        /// <summary>
        /// Gets the nearest object by name, position and distance.
        /// </summary>
        /// <returns>The nearest object by name.</returns>
        /// <param name="_name">Name.</param>
        /// <param name="_position">Position.</param>
        /// <param name="_distance">Distance.</param>
        public static GameObject GetNearestObjectByName(string _name, Vector3 _position, float _distance)
        {
            if (_name.Trim() == "")
            {
                return(null);
            }

            if (_distance == 0)
            {
                _distance = Mathf.Infinity;
            }

            GameObject _best_object   = null;
            float      _best_distance = _distance;

            GameObject[] _objects = GameObject.FindObjectsOfType <GameObject>();

            foreach (GameObject _tmp_object in _objects)
            {
                if (_tmp_object != null && _tmp_object.name == _name && _tmp_object.transform.position != _position && _tmp_object.activeInHierarchy)
                {
                    float _tmp_distance = PositionTools.Distance(_position, _tmp_object.transform.position);

                    if (_tmp_distance < _best_distance)
                    {
                        _best_distance = _tmp_distance;
                        _best_object   = _tmp_object;
                    }
                }
            }

            return(_best_object);
        }
        public static void ZickZackCircle(Vector3 center, float radius, string _text, bool _show_text)
        {
            float step = GetCircleDegrees(radius);



            Vector3 _text_pos = Vector3.zero;

            bool _draw = false;

            for (float _angle = 0; _angle <= 360; _angle += step)
            {
                //	_angle += space;
                float a1 = _angle * Mathf.PI / 180f;
                float a2 = (_angle + step) * Mathf.PI / 180f;

                Vector3 _inner_pos_1 = center + new Vector3(Mathf.Sin(a1) * radius, 0, Mathf.Cos(a1) * radius);
                Vector3 _inner_pos_2 = center + new Vector3(Mathf.Sin(a2) * radius, 0, Mathf.Cos(a2) * radius);
                float   lenght       = PositionTools.Distance(_inner_pos_1, _inner_pos_2);

                Vector3 _outer_pos_1 = center + new Vector3(Mathf.Sin(a1) * (radius - lenght), 0, Mathf.Cos(a1) * (radius - lenght));
                Vector3 _outer_pos_2 = center + new Vector3(Mathf.Sin(a2) * (radius - lenght), 0, Mathf.Cos(a2) * (radius - lenght));

                Vector3 _outer_pos_3 = Vector3.Lerp(_outer_pos_1, _outer_pos_2, 0.5f);


                Gizmos.DrawLine(_inner_pos_1, _outer_pos_3);
                Gizmos.DrawLine(_outer_pos_3, _inner_pos_2);

                if (_text_pos == Vector3.zero)
                {
                    _text_pos = _inner_pos_1;
                }

                if (_draw)
                {
                    _draw = false;
                }
                else
                {
                    _draw = true;
                }
            }

            if (_show_text && _text != "")
            {
                Color _color = Gizmos.color;
                _color.a = 255;
                Text(GUI.skin, _text, _text_pos, _color, 12, -50, 200);
                //Gizmos.color = _org_color;
            }
        }
        public static void Orbit(Vector3 _creature, Vector3 _center, float _radius, float _degress, float _shift, float _min, float _max, float _level)
        {
            float _angle = PositionTools.GetNormalizedVectorAngle(_creature - _center);

            Vector3 _last_position = Vector3.zero;

            while ((_shift > 0 && _radius < _max) || (_shift < 0 && _radius > _min))
            {
                _radius += _shift * 0.02f;

                if (_radius < _min)
                {
                    _radius = _min;
                }
                else if (_max > 0 && _radius > _max)
                {
                    _radius = _max;
                }

                _angle += _degress;

                if (_angle > 360)
                {
                    _angle = _angle - 360;
                }

                float _a = _angle * Mathf.PI / 180f;

                Vector3 _new_position = _center + new Vector3(Mathf.Sin(_a) * _radius, 0, Mathf.Cos(_a) * _radius);
                _new_position.y = _level;

                if (_last_position != Vector3.zero)
                {
                    Gizmos.DrawLine(_last_position, _new_position);
                }

                _last_position = _new_position;
            }

            if (_shift < 0)
            {
                Circle(_center, _min, 5, true, "", false);
            }
            else if (_shift > 0)
            {
                Circle(_center, _max, 5, true, "", false);
            }
            else
            {
                Circle(_center, _radius, 5, true, "", false);
            }
        }
        public static void UpdateTrees(TerrainData _terrain_data)
        {
            GameObject[] _tree_objects = new GameObject[3];
            _tree_objects[0] = (GameObject)Resources.Load("Trees/tree1");
            _tree_objects[1] = (GameObject)Resources.Load("Trees/tree2");
            _tree_objects[2] = (GameObject)Resources.Load("Trees/tree3");

            TreePrototype[] _trees = new TreePrototype[3];
            _trees[0]        = new TreePrototype();
            _trees[0].prefab = _tree_objects[0];

            _trees[1]        = new TreePrototype();
            _trees[1].prefab = _tree_objects[1];

            _trees[2]        = new TreePrototype();
            _trees[2].prefab = _tree_objects[2];
            //_trees[0].bendFactor =

            _terrain_data.treePrototypes = _trees;

            List <TreeInstance> _tree_instances = new List <TreeInstance>();

            for (int i = 0; i < TerrainTreesMax; i++)
            {
                Vector3 _pos = PositionTools.GetRandomRectPosition(1, 1);
                _pos.y = MathTools.Normalize(_terrain_data.GetInterpolatedHeight(_pos.x, _pos.z), 0, _terrain_data.size.y);

                Vector3 _ground = _terrain_data.GetInterpolatedNormal(_pos.x, _pos.z);
                float   _angle  = Mathf.Abs(Vector3.Angle(_ground, Vector3.up));

                //Debug.Log( _pos + " - " + _ground + " - " + Vector3.Angle( _ground, Vector3.up ) );

                if (_angle > 0 && _angle < TerrainTreesMaxAngle)
                {
                    TreeInstance _tree = new TreeInstance();
                    _tree.prototypeIndex = UnityEngine.Random.Range(0, 3);
                    _tree.position       = _pos;
                    _tree.rotation       = UnityEngine.Random.Range(0, 360);
                    _tree.heightScale    = UnityEngine.Random.Range(0.75f, 1.5f);
                    _tree.widthScale     = _tree.heightScale + UnityEngine.Random.Range(-0.15f, 0.15f);
                    _tree.color          = new Color(1, 1, 1);
                    _tree.lightmapColor  = new Color(1, 1, 1);


                    _tree_instances.Add(_tree);
                }
            }

            _terrain_data.treeInstances = _tree_instances.ToArray();
        }
        /// <summary>
        /// Gets the direction angle.
        /// </summary>
        /// <returns>The direction angle.</returns>
        /// <param name="_transform">Transform.</param>
        /// <param name="_position">Position.</param>
        public static float GetSignedDirectionAngle(Transform _transform, Vector3 _position)
        {
            if (_transform == null)
            {
                return(0);
            }

            float _angle = MathTools.NormalizeAngle(PositionTools.GetNormalizedVectorAngle(_position - _transform.position) - _transform.eulerAngles.y);

            if (_angle > 180)
            {
                _angle -= 360;
            }

            return(_angle);
        }
        public static Vector3 GetPathSection(Vector3 pos_1, Vector3 pos_2, float lenght, bool inverse)
        {
            float f        = 1;
            float distance = PositionTools.Distance(pos_1, pos_2);

            if (inverse)
            {
                lenght = distance - lenght;
            }

            if (lenght < distance)
            {
                f = lenght / (distance / 100) / 100;
            }

            return(Vector3.Lerp(pos_1, pos_2, f));
        }
        public static void Text(string _text, Vector3 _world_position, Color _color, float _font_size = 14, FontStyle _font_style = FontStyle.Normal)
        {
                        #if UNITY_EDITOR
            UnityEditor.Handles.BeginGUI();

            UnityEditor.SceneView view = UnityEditor.SceneView.currentDrawingSceneView;
            Vector3 _screen_position   = view.camera.WorldToScreenPoint(_world_position);
            Vector2 _size = GUI.skin.label.CalcSize(new GUIContent(_text));

            GUIStyle _style = new GUIStyle();
            _style.normal.textColor = _color;
            float _distance   = (int)PositionTools.Distance(view.camera.transform.position, _world_position);
            float _normalized = 1 - MathTools.Normalize(_distance, 0, 250);
            if (_normalized > 0.1f && _normalized < 1)
            {
                _style.fontSize  = (int)(_font_size * _normalized);
                _style.fontStyle = _font_style;
                GUI.Label(new Rect(_screen_position.x - (_size.x / 2), -_screen_position.y + view.position.height + 4, _size.x, _size.y), _text, _style);
            }
            UnityEditor.Handles.EndGUI();
                        #endif
        }
        public static void Arc(Transform _target, float _radius, float _degress, float _left, float _right, float _level, bool _dotted = true)
        {
            if (_target == null)
            {
                return;
            }

            Vector3 _center     = _target.position;
            Vector3 _center_pos = PositionTools.GetDirectionPosition(_target, 0, _radius + (_radius / 10));
            Vector3 _left_pos   = PositionTools.GetDirectionPosition(_target, _left, _radius);
            Vector3 _right_pos  = PositionTools.GetDirectionPosition(_target, _right, _radius);

            _center.y     = _level;
            _center_pos.y = _level;
            _left_pos.y   = _level;
            _right_pos.y  = _level;

            /*
             * Gizmos.DrawLine( _center, _center_pos );
             * Gizmos.DrawLine( _center, _left_pos );
             * Gizmos.DrawLine( _center, _right_pos );*/

            Vector3 _last_pos = _left_pos;

            for (float _angle = _left; _angle <= _right; _angle += _degress)
            {
                Vector3 _pos = PositionTools.GetDirectionPosition(_target, _angle, _radius);
                _pos.y = _level;
                if (!_dotted || ((int)_angle % 2 == 0))
                {
                    Gizmos.DrawLine(_last_pos, _pos);
                }

                _last_pos = _pos;
            }
        }