示例#1
0
 public void RefreshPath()
 {
     if (pathCreator != null)
     {
         path = pathCreator.path;
     }
     distanceTravelled = path.GetClosestDistanceAlongPath(transform.position);
 }
示例#2
0
    IEnumerator Following(VertexPath path)
    {
        Vector3 lastPoint = path.GetPointAtTime(1f, EndOfPathInstruction.Stop);
        float   befDist   = 0.5f;

        if (stopped == false)
        {
            while (DistanceXZ(transform.position, lastPoint) > 0.5f)
            {
                float distance = Vector3.Distance(this.transform.position, lastPoint);

                if (distance < stopDistance)
                {
                    stopped       = true;
                    body.velocity = Vector3.zero;
                }

                body.velocity = Vector3.ClampMagnitude(body.velocity, maxSpeed);
                body.AddForce(transform.forward * followSpeed, ForceMode.VelocityChange);
                float dist = path.GetClosestDistanceAlongPath(transform.position);
                if (dist < befDist)
                {
                    dist = befDist;
                }
                else
                {
                    befDist = dist;
                }

                Vector3 nextPoint = path.GetPointAtDistance(dist + followSpeed, EndOfPathInstruction.Stop);
                Vector3 diff      = nextPoint - transform.position;
                diff.y = 0;
                diff.Normalize();
                Debug.DrawLine(transform.position, nextPoint, Color.red);
                Debug.DrawRay(transform.position, diff, Color.gray);
                float angle = Mathf.Atan2(diff.x, diff.z) * Mathf.Rad2Deg;
                transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.AngleAxis(angle, Vector3.up), rotateSpeed * Time.fixedDeltaTime);
                yield
                return(new WaitForFixedUpdate());
            }
            body.velocity = Vector3.zero;
        }
    }
示例#3
0
    private void FixedUpdate()
    {
        for (int i = 0; i < AICars.Length; i++)
        {
            Vector3 nextPoint   = path.GetPointAtDistance(path.GetClosestDistanceAlongPath(AICars[i].transform.position) + AICars[i].AIDistanceFactor);
            float   signedAngle = Vector2.SignedAngle(AICars[i].transform.up, nextPoint - AICars[i].transform.position);
            bool    invert      = false;

            if (Mathf.Abs(signedAngle) < 107)
            {
                AICars[i].Forward();
            }
            else
            {
                AICars[i].Backward();
                invert = true;
            }

            if (signedAngle > 5)
            {
                if (!invert)
                {
                    AICars[i].Left();
                }
                else
                {
                    AICars[i].Right();
                }
            }
            else if (signedAngle < -5)
            {
                if (!invert)
                {
                    AICars[i].Right();
                }
                else
                {
                    AICars[i].Left();
                }
            }
        }
    }
示例#4
0
    //public override void CollectObservations(VectorSensor sensor)
    //{
    //    // # Basic information
    //    Rigidbody rBody = GetComponent<Rigidbody>();
    //    // Agent positions & velocity
    //    sensor.AddObservation(this.transform.localPosition);
    //    sensor.AddObservation(rBody.velocity);
    //    // # Collect Path's observations
    //    float centerDistance = vertexPath.GetClosestDistanceAlongPath(this.transform.localPosition);
    //    for (int d = tickerStart; d <= tickerEnd; d++)
    //    {
    //        float distance = centerDistance + d * tickerSpace;
    //        Vector3 point = vertexPath.GetPointAtDistance(distance, EndOfPathInstruction.Loop);
    //        Vector3 normal = vertexPath.GetNormalAtDistance(distance, EndOfPathInstruction.Loop);

    //        AddObservationInXYZMode(sensor, point);
    //        AddObservationInXYZMode(sensor, normal);
    //    }
    //    // # Padding observations
    //    // If custom setting will have too more observations and cause the buffer overflow, We
    //    // must warn user (in Pyhon interface level).
    //    // Here we are padding the observation buffer with all zero
    //    int MaxObservationSize = GetComponent<BehaviorParameters>().BrainParameters.VectorObservationSize;
    //    for (int i = 0; i < MaxObservationSize - ObservationSize; i++)
    //        sensor.AddObservation(0);
    //}
    public override void CollectObservations(VectorSensor sensor)
    {
        // # Basic information
        Rigidbody rBody = GetComponent <Rigidbody>();

        // Agent velocity
        sensor.AddObservation(this.transform.localPosition);
        sensor.AddObservation(rBody.velocity);
        // # Collect Path's basic information
        float   centerDistance = vertexPath.GetClosestDistanceAlongPath(this.transform.localPosition);
        Vector3 direction      = vertexPath.GetDirectionAtDistance(centerDistance);
        // Check clockwise direction & determine ticker's start and end
        bool clockwise = (Vector3.Dot(rBody.velocity, direction) >= 0);
        int  t_s       = clockwise ? tickerStart : -tickerEnd;
        int  t_e       = clockwise ? tickerEnd : -tickerStart;

        // # Collect Path's observations
        for (int d = t_s; d <= t_e; d++)
        {
            float   distance = centerDistance + d * tickerSpace;
            Vector3 point    = vertexPath.GetPointAtDistance(distance, EndOfPathInstruction.Loop);
            Vector3 normal   = vertexPath.GetNormalAtDistance(distance, EndOfPathInstruction.Loop);
            AddObservationInXYZMode(sensor, point - this.transform.localPosition);
            AddObservationInXYZMode(sensor, normal);
        }
        // # Padding observations
        // If custom setting will have too more observations and cause the buffer overflow, We
        // must warn user (in Pyhon interface level).
        // Here we are padding the observation buffer with all zero
        int MaxObservationSize = GetComponent <BehaviorParameters>().BrainParameters.VectorObservationSize;

        for (int i = 0; i < MaxObservationSize - ObservationSize; i++)
        {
            sensor.AddObservation(0);
        }
    }