Exemplo n.º 1
0
    override public void OnInspectorGUI()
    {
        DrawDefaultInspector();
        C_Obsticle obsticle = (C_Obsticle)target;

        if (GUILayout.Button("Instantiate obsticle points"))
        {
            obsticle.Spawn();
        }
    }
Exemplo n.º 2
0
    //Should the AI go clockwise around the obsticle?

    /*
     *      The obsticle goes through the obsticlepoints one at a time (alternating between clockwise and counter clockwise) and raycast towards our target,
     *      if the raycast hits the obsticle, we move on to the next node, otherwise, we tell our caller whether the obsticlepoint that
     *      missed was clockwise or not.
     * */
    public bool NavigateClockwise(C_Obsticle obsticle, C_ObsticlePoint point, Vector3 ultimateTarget)
    {
        Transform _obsTransform = obsticle.transform;

        C_ObsticlePoint _next     = point.next;
        C_ObsticlePoint _previous = point.previous;

        for (int i = 0; i < obsticle.obsticlePoints.Length * 0.5f; i++)
        {
            RaycastHit _hit;
            Vector3    _direction = ultimateTarget - _next.transform.position;
            Debug.DrawRay(_next.transform.position, _direction, Color.blue, 5);
            if (Physics.Raycast(_next.transform.position, _direction, out _hit))
            {
                if (_hit.transform.GetComponent <C_Obsticle>() != obsticle)
                {
                    return(false);
                }
                else
                {
                    _next = _next.next;
                }
            }
            else
            {
                return(false);
            }
            _direction = ultimateTarget - _previous.transform.position;
            Debug.DrawRay(_previous.transform.position, _direction, Color.red, 5);
            if (Physics.Raycast(_previous.transform.position, _direction, out _hit))
            {
                if (_hit.transform.GetComponent <C_Obsticle>() != obsticle)
                {
                    return(true);
                }
                else
                {
                    _previous = _previous.previous;
                }
            }
            else
            {
                return(true);
            }
        }
        return(true);
    }
Exemplo n.º 3
0
    //Finds the closest obsticle point from a given point
    public C_ObsticlePoint FindClosestPoint(C_Obsticle obsticle, Vector3 point)
    {
        C_ObsticlePoint closestPoint = obsticle.obsticlePoints[0];
        float           _distance    = int.MaxValue;

        foreach (C_ObsticlePoint op in obsticle.obsticlePoints)
        {
            float _tempDistance = Vector2.Distance(op.transform.position - op.transform.position.y * Vector3.up, point - point.y * Vector3.up);
            if (_tempDistance < _distance)
            {
                _distance    = _tempDistance;
                closestPoint = op;
            }
        }
        Debug.Log("Returning " + closestPoint.name + " as the closest point");
        return(closestPoint);
    }
    private bool RayCastObsticle(C_Enemy e)
    {
        Vector3    _direction = e.target - e.transform.position;
        RaycastHit _hit;

        if (Physics.Raycast(e.transform.position, _direction, out _hit))
        {
            if (_hit.transform.tag == "Obsticle")
            {
                print("<color=red>Obsticle found!</color>");
                C_Obsticle _obs = _hit.transform.GetComponent <C_Obsticle>();
                e.obsticlePoint = obsticleHandler.FindClosestPoint(_obs, e.transform.position);
                e.target        = e.obsticlePoint.transform.position;
                return(true);
            }
        }

        return(false);
    }
    public void Tick()
    {
        float _deltaTime = Time.deltaTime;

        foreach (C_Enemy _enemy in enemies)
        {
            if (_enemy.targetTransform != null)
            {
                //Calculate distance and direction to target
                Vector3 _direction    = _enemy.target - _enemy.transform.position;
                float   _directionMag = _direction.magnitude;

                if (_enemy.rayCastObsticle)
                {
                    _enemy.target = _enemy.targetTransform.position - _enemy.targetTransform.position.y * Vector3.up;
                    if (_directionMag >= _enemy.minDistanceToTarget)
                    {
                        //Movement
                        _direction = _direction / _directionMag;
                        _enemy.transform.position += _direction * _enemy.speed * _deltaTime;

                        //Rotation
                        _enemy.transform.LookAt(_enemy.transform.position + _direction);
                    }
                    else
                    {
                        ChangeTarget(_enemy);
                    }
                    _enemy.rayCastObsticle = !RayCastObsticle(_enemy);
                }
                else
                {
                    //If we have not reached our destination
                    if (_directionMag >= 0.1f)
                    {
                        //Movement
                        _direction = _direction / _directionMag;
                        _enemy.transform.position += _direction * _enemy.speed * _deltaTime;

                        //Rotation
                        _enemy.transform.LookAt(_enemy.transform.position + _direction);
                    }
                    //If we have reached our destination
                    else
                    {
                        Vector3    _dir = _enemy.targetTransform.position - _enemy.transform.position;
                        RaycastHit _hit;

                        //if the obsticle still is in the way
                        if (Physics.Raycast(_enemy.transform.position, _dir, out _hit))
                        {
                            if (_hit.transform.tag == "Obsticle")
                            {
                                C_Obsticle _obs = _hit.transform.GetComponent <C_Obsticle>();
                                if (obsticleHandler.NavigateClockwise(_obs, obsticleHandler.FindClosestPoint(_obs, _enemy.transform.position), _enemy.targetTransform.position))
                                {
                                    _enemy.obsticlePoint = _enemy.obsticlePoint.previous;
                                    _enemy.target        = _enemy.obsticlePoint.transform.position;
                                }
                                else
                                {
                                    _enemy.obsticlePoint = _enemy.obsticlePoint.next;
                                    _enemy.target        = _enemy.obsticlePoint.transform.position;
                                }
                            }
                            else
                            {
                                _enemy.rayCastObsticle = true;
                                _enemy.target          = _enemy.targetTransform.position;
                            }
                        }
                        else
                        {
                            _enemy.rayCastObsticle = true;
                            _enemy.target          = _enemy.targetTransform.position;
                        }
                    }
                }
            }
        }
    }