示例#1
0
    void Update()
    {
        if (!grid)
        {
            return;
        }

        if (doMove)
        {
            //move towards the desination
            Vector3 newPosition = cachedTransform.position;
            newPosition.x            = Mathf.MoveTowards(cachedTransform.position.x, goal.x, roamingSpeed * Time.deltaTime);
            newPosition.y            = Mathf.MoveTowards(cachedTransform.position.y, goal.y, roamingSpeed * Time.deltaTime);
            cachedTransform.position = newPosition;
            //check if we reached the destination (use a certain tolerance so we don't miss the point becase of rounding errors)
            if (Mathf.Abs(cachedTransform.position.x - goal.x) < 0.01f && Mathf.Abs(cachedTransform.position.y - goal.y) < 0.01f)
            {
                doMove = false;
            }
            //if we did stop moving
        }
        else
        {
            //make sure the time is always positive
            if (roamingTime < 0.01f)
            {
                roamingTime = 0.01f;
            }
            //find the next destination
            goal = FindNextFace();
            //--- let's check if the goal is allowed, if not we will pick another direction during the next frame ---
            if (ForbiddenTilesExample.CheckSquare(goal))
            {
                //calculate speed by dividing distance (one of the two distances will be 0, we need the other one) through time
                roamingSpeed = Mathf.Max(Mathf.Abs(cachedTransform.position.x - goal.x), Mathf.Abs(cachedTransform.position.y - goal.y)) / roamingTime;
                //resume movement with the new goal
                doMove = true;
            }
            else
            {
                Debug.Log("hit the obstacle");
            }
        }
    }
 // Awake is called before Start()
 void Awake()
 {
     //We will build the matrix based on the grid that is attached to this object.
     //All entries are true by default, then each obstacle will mark its entry as false
     ForbiddenTilesExample.Initialize(GetComponent <GFRectGrid>());
 }
 void OnGUI()
 {
     GUI.TextArea(new Rect(10, 10, 250, 200), ForbiddenTilesExample.MatrixToString());
 }
示例#4
0
 // Start() is called after Awake(), this ensures that the matrix has alrady been built
 void Start()
 {
     //Set the entry that corresonds to the obstacle's position as false
     ForbiddenTilesExample.RegisterSquare(transform.position, false);
 }