Hello! There’s probably already many questions regarding this but I can’t seem to find an answer that works for my problem! The problem I’m having is that I want my circle collider to not only tell my enemy if a gameobject with the tag “Player” enters the circle collider, but I also want the target to be equal to the gameobject with the “player” tag! As in, I want it to automatically find the tagged gameobject and make that the “target” rather than how it is set up currently where I assign a gameobject to the “public transform target”!
Heres my code and if I can figure it out before anyone gets back to me, I’ll post it on here! (Which seems to be the most common thing here bc last question I asked I waited for an answer for a long time and got 0 response and in the end fixed it myself!) The code is a bit lengthy though…
private bool moving;
public float timeBetweenMove;
private float timeBetweenMoveCounter;
private Rigidbody2D myRigidbody;
public float timeToMove;
private float timeToMoveCounter;
//public float waitToReload;
//private GameObject thePlayer;
private Vector3 moveDirection;
public float moveSpeed;
// The target marker.
public Transform target;
// Speed in units per sec.
public float speed;
public GameObject bullet;
public bool shooting;
public bool following;
public float attackTime;
private float attackTimeCounter;
private void Start()
{
following = false;
myRigidbody = GetComponent<Rigidbody2D>();
timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
}
void Update()
{
//following movement
if (following)
{
if (moving)
{
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
// Move enemy position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
timeToMoveCounter -= Time.deltaTime;
if (timeToMoveCounter < 0f)
{
moving = false;
//timeBetweenMoveCounter = timeBetweenMove;
timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.25f, timeBetweenMove * 1.25f);
}
}
else
{
timeBetweenMoveCounter -= Time.deltaTime;
myRigidbody.velocity = Vector2.zero;
if (timeBetweenMoveCounter < 0f)
{
moving = true;
//timeToMoveCounter = timeToMove;
timeToMoveCounter = Random.Range(timeToMove * 0.25f, timeToMove * 1.25f);
}
}
}
//regular movement
else
{
if (moving)
{
timeToMoveCounter -= Time.deltaTime;
myRigidbody.velocity = moveDirection;
if (timeToMoveCounter < 0f)
{
moving = false;
//timeBetweenMoveCounter = timeBetweenMove;
timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
}
}
else
{
timeBetweenMoveCounter -= Time.deltaTime;
myRigidbody.velocity = Vector2.zero;
if (timeBetweenMoveCounter < 0f)
{
moving = true;
//timeToMoveCounter = timeToMove;
timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
moveDirection = new Vector3(Random.Range(-1f, 1f) * moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
}
}
}
if (attackTimeCounter >= 0)
{
attackTimeCounter -= Time.deltaTime;
}
if (attackTimeCounter < 0)
{
shooting = false;
}
if (shooting)
{
shooting = true;
//attackTimeCounter = attackTime - shootSpeed;
attackTimeCounter = attackTime;
Instantiate(bullet);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
following = true;
shooting = true;
//GameObject shootTowards = GameObject.FindGameObjectWithTag("Player");
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
shooting = false;
following = false;
}
}
}
Where I want the ontriggerenter to also pick the target rather than me assigning the target in unity! Thanks!