Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        Debug.Log(behaviour);

        Vector3 forward = transform.TransformDirection(Vector3.forward);

        Debug.DrawRay(transform.position, forward, Color.green);

        RaycastHit hit;


        if (behaviour == Behaviour.LookingFor)
        {
            if (Physics.Raycast(transform.position, forward, out hit))
            {
                Vector3 HitForward = hit.transform.TransformDirection(Vector3.forward);

                RB.velocity = Vector3.zero;

                if (Vector3.Dot(forward, HitForward) > 0)
                {
                    transform.Rotate(Vector3.up, -5 * turnSpeed * Time.deltaTime);
                }
                else
                {
                    transform.Rotate(Vector3.up, 5 * turnSpeed * Time.deltaTime);
                }


                if (hit.distance > 5.0f)
                {
                    behaviour = Behaviour.MoveTo;
                    target    = transform.position + forward * 5.0f;
                    MT.SetTarget(target);
                }

                lookWhereYoureGoing();

                return;
            }
        }


        if (behaviour == Behaviour.MoveTo)
        {
            Vector3 acceleration = MT.getSteering();

            Debug.Log("MoveTo: " + acceleration);

            RB.velocity += acceleration * Time.deltaTime;

            if (MT.GetTarget())
            {
                behaviour = Behaviour.Wander;
            }

            if (RB.velocity.magnitude > maxVelocity)
            {
                RB.velocity = RB.velocity.normalized * maxVelocity;
            }


            lookWhereYoureGoing();

            return;
        }
        else
        {
            Vector3 acceleration = WD.getSteering();

            RB.velocity += acceleration * Time.deltaTime;

            if (Physics.Raycast(transform.position, forward, out hit))
            {
                if (hit.distance <= 2.0f)
                {
                    RB.velocity = Vector3.zero;
                    behaviour   = Behaviour.LookingFor;
                    return;
                }
            }

            if (RB.velocity.magnitude > maxVelocity)
            {
                RB.velocity = RB.velocity.normalized * maxVelocity;
            }

            lookWhereYoureGoing();
        }
    }