- Home /
AI phyics movement doesnt work
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);
}
}
}
Answer by CroKJustice · Mar 24, 2021 at 01:08 PM
I found whats wrong. For some reason my targets position in debug log is 5.3 but in my unity scene my targets position is 10. Every time it happens in random position in random time. Please can somebody explain to me what is happening and how to fix it?
Your answer
Follow this Question
Related Questions
How to boxcast where a dynamic rigidbody2D's box collider will be in the next frame? 1 Answer
AI movement in D 0 Answers
How to make a rigid body move along a specific path using my mouse? 1 Answer
How can you make gravity to not have any acceleration but with const velocity? 1 Answer
Why does poligon collider 2d restricts movement of box collider? 0 Answers