- Home /
How do I make something in the Update function run once, until a certain condition is met (a collision)?,How would I run through this code al the way through in the Update function until it has reached one
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;
}
}
}
Answer by landings · Mar 27 at 02:44 PM
You can add a state for your bacteria. It can decide what to do depending on its current state.
enum BacteriaState { Free, ApproachingCell, AttackingCell } // You can have more states
BacteriaState state = BacteriaState.Free;
GameObject target = null;
float speed = 1;
void Update()
{
switch (state)
{
case BacteriaState.Free:
var allCells = GameObject.FindGameObjectsWithTag("Cells");
if (allCells != null)
{
var index = Random.Range(0, allCells.Length);
target = allCells[index];
state = BacteriaState.ApproachingCell; // State transition
}
break;
case BacteriaState.ApproachingCell:
if (target != null)
{
transform.position = Vector2.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
transform.up = (target.transform.position - transform.position).normalized;
if ((target.transform.position - transform.position).magnitude < 0.1f) // You can use other collision detection method
{
state = BacteriaState.AttackingCell; // State transition
}
}
break:
case BacteriaState.AttackingCell:
// Logic to attack a cell
// ...
// After winning:
state = BacteriaState.Free; // State transition
target = null; // If target is gone
break;
default:
break;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to create a field of view for enemy ai that detects a player Unity 2d? 3 Answers
Instantiated object not showing in scene or hierarchy 2 Answers
Image display from IP camera 0 Answers