Пример #1
0
    public bool followThePath(Path p)
    {
        var t = p.GetNetxtTarget();

        //ROTATE
        var targetRot = Quaternion.LookRotation((t.transform.position - this.transform.position), Vector3.up);
        var slerpRot  = Quaternion.Slerp((this.transform.rotation), targetRot, rotationSpeed * Time.deltaTime);

        //TRANSLATE
        //Build the Vector with a Max Length of 1
        var v = t.transform.position - this.transform.position;

        v.Normalize();
        Debug.DrawLine(this.transform.position, this.transform.position + v, Color.red);

        //Only Move if the target Field is Free (exeption: the target Field has a Resource you have to Pick up)
        Vector3 pos;

        if (t.FreeToMoveOn() || (t == targetPosition && t.ResourceWaitingForPickup())) //TODO Resource Waiting for Pickup
        {
            pos = this.transform.position + v * Time.deltaTime * actualPosition.getConnectionSpeed(t);
        }
        else
        {
            pos = this.transform.position;
        }

        this.transform.SetPositionAndRotation(pos, slerpRot);

        //ARRIVED
        var d = (t.transform.position - this.transform.position).magnitude;

        if (d <= arrivalDistance)
        {
            //Leave the Old Field
            actualPosition.CarriageMovesFromField(this);
            //Move to the New Field
            t.CarriageMovesOnField(this);
            return(p.Arrive());
        }

        return(false);
    }