Ejemplo n.º 1
0
 // Use this for initialization
 void Start()
 {
     UpdateDestination();
     _speed         = Random.Range(5, 10);
     _rotateSpeed   = 20;
     _rigidbody     = GetComponent <Rigidbody>();
     _socialCircle  = null;
     nextActionTime = Random.Range(3, 10);
 }
Ejemplo n.º 2
0
    private void FixedUpdate()
    {
        if (_socialCircle != null)
        {
            if (Time.time > nextActionTime)
            {
                _socialCircle.Leave();
                _socialCircle = null;
                freeUntil     = Time.time + Random.Range(3, 6);
            }
        }

        GameObject socialAgentAround = TalkingAvailableSocialAgentAround();

        if (socialAgentAround != null && _socialCircle == null && Time.time > freeUntil)
        {
            SocialAgent socialAgent = socialAgentAround.GetComponent <SocialAgent>();
            if (socialAgent._socialCircle == null)
            {
                _socialCircle = new SocialCircle(transform.position + Vector3.Normalize(transform.forward));
            }
            else
            {
                _socialCircle = socialAgent._socialCircle;
            }

            nextActionTime = Time.time + Random.Range(3, 10);
            _socialCircle.Join();
        }

        if (_socialCircle != null)
        {
            destination = _socialCircle.Center;
        }

        Vector3 desiredVelocity = destination - transform.position;
        var     distance        = desiredVelocity.magnitude;
        var     slowingRange    = 4;
        var     facingCenter    = false;

        if (_socialCircle != null && Math.Abs(distance - _socialCircle.Radius) < 0.2)
        {
            _rigidbody.velocity = new Vector3();
            facingCenter        = true;
            if (_socialCircle.MemberCount == 1)
            {
                _socialCircle.Leave();
                _socialCircle = null;
            }
        }
        else if (distance < 0.5 || Physics.CheckSphere(destination, 3) && _socialCircle == null)
        {
            UpdateDestination();
        }
        else if (distance < slowingRange)
        {
            if (_socialCircle != null && distance < _socialCircle.Radius)
            {
                _rigidbody.velocity = -transform.forward * _speed;
            }
            else
            {
                _rigidbody.velocity = transform.forward * _speed * distance / slowingRange;
            }
        }
        else
        {
            _rigidbody.velocity = transform.forward * _speed;
        }


        if (facingCenter || !ReactToObstacles())
        {
            var rotation = Quaternion.LookRotation(destination - transform.position);
            _rigidbody.MoveRotation(Quaternion.RotateTowards(transform.rotation, rotation, _rotateSpeed));
        }
    }