I’m trying to make a game about the immune system because it seems like an interesting idea. This code for a bacteria makes it find a cell and go towards it. However, when I run it, because it is in the Update function, it constantly searches for a new target. How do I make it so that it runs it once, until it actually collides with the target and then runs again. Here’s the code
void Update()
{
allCells = GameObject.FindGameObjectsWithTag("Cells");
if(allCells != null){
index = Random.Range(0, allCells.Length);
target = allCells[index];
transform.position = Vector2.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
transform.up = target.transform.position - transform.position;
}
}
I am a relatively new unity user and this is the first actual project I am making
,I’m trying to make a game about the immune system because it seems like an interesting idea. This code for a bacteria makes it find a cell and go towards it. However, when I run it, because it is in the Update function, it constantly searches for a new target. How do I make it so that it runs it once, until it actually collides with the target and then runs again. Here’s the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BacteriaMovement : MonoBehaviour
{
private GameObject allCells;
private GameObject target;
public float speed = 5f;
int index;
void Update()
{
allCells = GameObject.FindGameObjectsWithTag("Cells");
if(allCells != null){
index = Random.Range(0, allCells.Length);
target = allCells[index];
transform.position = Vector2.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
transform.up = target.transform.position - transform.position;
}
}
}