- Home /
AI movement in D
I am trying to make an AI who picks up blocks and brings them to appropriate container. For some reason my AI stops at random times sometimes he picks like 4 or 5 blocks and brings them to the container sometimes he managed to get 16 blocks in container. Can somebody check my script because i dont know why my AI just stops random at the times and doesnt want to pick up blocks. I tried to put Debug log in my if statemates to see if it goes in and it does. My AI goes in the if statemant but he still doesnt move.
[Header("AI Stats")] public float speed = 4.0f; public float diststop = 1f;
bool bluePickedUp;
bool redPickedUp;
int blueBlockNumber=0;
int redBlockNumber=0;
[Header("Posude")]
public Transform bluePosuda;
public Transform redPosuda;
[Header("Blocks")]
public Transform blueBlock;
public Transform redBlock;
[Header("Text")]
public Text blueBlockNumberText;
public Text redBlockNumberText;
private Rigidbody2D rb;
[Header(" ")]
public Transform hand;
private Vector2 Position
{
get
{
return transform.position;
}
set
{
transform.position = value;
}
}
private void Start()
{
rb = GetComponent<Rigidbody2D>();
blueBlockNumberText.text = "Number of blue blocks in container is: " + blueBlockNumber;
redBlockNumberText.text = "Number of red blocks in container is: " + redBlockNumber;
}
private void FixedUpdate()
{
if (bluePickedUp == false && redPickedUp == false)
{
if (redBlockNumber >= blueBlockNumber)
{
Target(blueBlock);
Debug.Log("Blue block);
}
if(redBlockNumber < blueBlockNumber)
{
Target(redBlock);
Debug.Log("Red block");
}
}
if (bluePickedUp == true) {
Target(bluePosuda);
Debug.Log("Blue container");
}
if(redPickedUp == true)
{
Target(redPosuda);
Debug.Log("red containter);
}
}
void Target(Transform target)
{
float dist = Vector2.Distance(Position, target.position);
float step = speed * Time.deltaTime;
if (dist >= diststop)
{
Position = Vector2.MoveTowards(Position, target.position, step);
rb.MovePosition(Position);
}
}
private void OnTriggerEnter2D(Collider2D target)
{
if (target.CompareTag("Blue") && redBlockNumber >= blueBlockNumber && bluePickedUp == false && redPickedUp == false)
{
bluePickedUp = true;
target.transform.position = hand.position;
target.transform.parent = GameObject.Find("Hand").transform;
}
if (target.CompareTag("Red") && redBlockNumber < blueBlockNumber && bluePickedUp == false && redPickedUp == false)
{
redPickedUp = true;
target.transform.position = hand.position;
target.transform.parent = GameObject.Find("Hand").transform;
}
if (target.CompareTag("BluePosuda"))
{
bluePickedUp = false;
blueBlockNumber++;
blueBlockNumberText.text = "Broj plavih blokova u posudama je: " + blueBlockNumber;
transform.eulerAngles = new Vector2(0, 180);
}
if (target.CompareTag("RedPosuda"))
{
redPickedUp = false;
redBlockNumber++;
redBlockNumberText.text = "Broj crvenih blokova u posudama je " + redBlockNumber;
transform.eulerAngles = new Vector2(0, 0);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613031223im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AI phyics movement doesnt work 1 Answer
How to boxcast where a dynamic rigidbody2D's box collider will be in the next frame? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
AI Movement with Rigidbody Physics? 0 Answers