Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved AI movement script won't move the object

Discussion in 'Scripting' started by Chazzwazzler, Sep 19, 2020.

  1. Chazzwazzler

    Chazzwazzler

    Joined:
    May 2, 2018
    Posts:
    35
    I did some debugging and it does assign the target position for the object to move to, just the object isn't moving to it. Why is this? Here is my code (btw, my game is in 2d, the only reason I did Vector3 for the target position was so I could check if a position equaled the target position):
    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class creature : MonoBehaviour
    5. {
    6.      //creature stats
    7.      public int strength;
    8.      public int speed;
    9.      //script utilities
    10.      int oldIteration = 0;
    11.      public Vector3 targetPos;
    12.      gameController controller;
    13.      //eating
    14.      public bool vegetarian = false;
    15.      public bool carnivore = false;
    16.      public bool omnivore = false;
    17.      void Start()
    18.      {
    19.          //script utilities
    20.          controller = Camera.main.GetComponent<gameController>();
    21.          targetPos = transform.position;
    22.          //stats
    23.          strength = Random.Range(0, 6);
    24.          //eating
    25.          int randDietForm = Random.Range(0, 3);
    26.          switch (randDietForm)
    27.          {
    28.              case 0:
    29.                  vegetarian = true;
    30.                  break;
    31.              case 1:
    32.                  carnivore = true;
    33.                  break;
    34.              case 2:
    35.                  omnivore = true;
    36.                  break;
    37.          }
    38.      }
    39.      void Update()
    40.      {
    41.          //iterative things
    42.          if (controller.iteration > oldIteration)
    43.          {
    44.              //findCreatures();
    45.              moveCreature();
    46.              oldIteration++;
    47.          }
    48.          //continuous things
    49.          transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
    50.      }
    51.      /////////////////////////////////////////////////////////////////////////////////////////////
    52.      //////////////////////////////////////// Movement ///////////////////////////////////////////
    53.      /////////////////////////////////////////////////////////////////////////////////////////////
    54.      //Moves the creature to a random surrounding available position
    55.      void moveCreature()
    56.      {
    57.          //positions
    58.          Vector3 creaturePos = transform.position;
    59.          List<Vector3> neighbourPositions = availableNeighbourPositions(creaturePos.x, creaturePos.y);
    60.          int rand = Random.Range(0, neighbourPositions.Count);
    61.          Vector3 tempTargetPos = neighbourPositions[rand];
    62.          Collider2D[] hitColliders = Physics2D.OverlapCircleAll(creaturePos, 2f);
    63.          for (int i = 0; i < hitColliders.Length; i++)
    64.          {
    65.              if (hitColliders[i].GetComponent<creature>().targetPos == tempTargetPos && hitColliders[i].transform.position != creaturePos)
    66.              {
    67.                  rand = Random.Range(0, neighbourPositions.Count);
    68.              }
    69.          }
    70.    
    71.          targetPos = neighbourPositions[rand];
    72.      }
    73.      //accesses the available (positions without colliders) neighbour positions of a position
    74.      List<Vector3> availableNeighbourPositions(float posX, float posY)
    75.      {
    76.          List<Vector3> positions = new List<Vector3>();
    77.          Vector3 pos = new Vector3(posX, posY, 0);
    78.          for (int i = -1; i < 2; i++)
    79.          {
    80.              for (int j = -1; j < 2; j++)
    81.              {
    82.                  float neighbour_x = posX + i;
    83.                  float neighbour_y = posY + j;
    84.                  Vector3 neighbourPos = new Vector3(neighbour_x, neighbour_y, 0);
    85.                  positions.Add(neighbourPos);
    86.              }
    87.          }
    88.          Collider2D[] hitColliders = Physics2D.OverlapCircleAll(pos, 1f);
    89.          for (int index = 0; index < hitColliders.Length; index++)
    90.          {
    91.              for (int i = 0; i < positions.Count; i++)
    92.              {
    93.                  if (hitColliders[index].gameObject.transform.position == positions[i])
    94.                  {
    95.                      positions.Remove(positions[i]);
    96.                  }
    97.              }
    98.          }
    99.          return positions;
    100.      }
    101.      //accesses the neighbour positions of a position
    102.      List<Vector3> neighbourPositions(float posX, float posY)
    103.      {
    104.          List<Vector3> positions = new List<Vector3>();
    105.          for (int i = -1; i < 2; i++)
    106.          {
    107.              for (int j = -1; j < 2; j++)
    108.              {
    109.                  float neighbour_x = posX + i;
    110.                  float neighbour_y = posY + j;
    111.                  if (i == 0 && j == 0)
    112.                  {
    113.                  }
    114.                  else
    115.                  {
    116.                      Vector3 pos = new Vector3(neighbour_x, neighbour_y, 0);
    117.                      positions.Add(pos);
    118.                  }
    119.              }
    120.          }
    121.          return positions;
    122.      }
    123. }
    I have a feeling that it has to do with this line:
    Code (CSharp):
    1. transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
    but I'm not sure what is wrong with it. My script is supposed to choose a random position (which it does, I tested with debug.log) and then move the object based off of that.