- Home /
Help with 2d TowerDefense Game
Hello, I am currently following along with a few Tutorial videos and I am currently working on a Tower Defense game that was included in the Project. I am not entirely sure what the conflict is I think it has something to do with the OnTriggerEnter2d function being called because I can manually have my Enemies go to the next CheckPoint by changing the Target int. below is Screenshots of the game and the code I used. I am pretty new to coding and I have searched for a fix but I am stumped. If you can help that would be great Thank you in advance!
EDIT The Issue I am having is my enemy reaches the first checkpoint but he does not move to the next checkpoint he only stays at the first checkpoint
Scene View & Game View
Enemy & Checkpoint Inspector
Enemy Movement Script
public int target = 0;
public Transform exitPoint;
public Transform[] waypoints;
public float navUpdate;
private Transform enemy;
private float navTime = 0;
// Use this for initialization
void Start () {
enemy = GetComponent<Transform>();
}
// Update is called once per frame
void Update () {
if (waypoints != null)
{
navTime += Time.deltaTime;
if (navTime > navUpdate)
{
if (target < waypoints.Length)
{
enemy.position = Vector2.MoveTowards(enemy.position, waypoints[target].position, navTime);
} else
{
enemy.position = Vector2.MoveTowards(enemy.position, exitPoint.position, navTime);
}
navTime = 0;
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "waypoint")
target += 1;
else if (other.tag == "Finish")
GameManager.instance.RemoveEnemyFromScreen();
Destroy(gameObject);
}
}
GameManager Script
public static GameManager instance = null;
public GameObject[] enemies;
public Transform[] spawnPoint;
public float spawnTime = 1.5f;
public int maxEnemiesOnScreen;
public int totalEnemies;
public int enemiesPerSpawn;
private int enemiesOnScreen = 0;
void Awake()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
// Use this for initialization
void Start () {
SpawnEnemy();
InvokeRepeating("SpawnEnemy", spawnTime, spawnTime);
}
// Update is called once per frame
void Update () {
}
void FixedUpdate()
{
}
public void SpawnEnemy()
{
if (enemiesPerSpawn > 0 && enemiesOnScreen < totalEnemies)
{
for (int i = 0; i < enemiesPerSpawn; i++)
{
if (enemiesOnScreen < maxEnemiesOnScreen)
{
int spawnIndex = Random.Range(0, spawnPoint.Length);
GameObject newEnemy = Instantiate(enemies[0]) as GameObject;
newEnemy.transform.position = spawnPoint[spawnIndex].position;
enemiesOnScreen += 1;
}
}
}
}
public void RemoveEnemyFromScreen()
{
if (enemiesOnScreen > 0)
{
enemiesOnScreen -= 1;
}
}
}
Not sure if this is the primary issue, but you have a problematic else if
on line 38 in Enemy $$anonymous$$ovement Script. The else if
only captures the Game$$anonymous$$anager.instance.RemoveEnemyFromScreen();
statement, not the Destroy(gameObject);
, so the gameObject
is always destroyed.
Sorry I just saw this Comment it was hidden for some reason but in the video that is what the guy shows him implementing and his code worked fine. The Else if statement is for the Finish point in the game once the enemy reaches the final trigger it will be destroyed so it does not stay in memory "That is what he says at least in the video" Thank you for the suggestion I was thinking the code was Depreciated or something because many Unity tutorials have old code.
Answer by Lunezo · Aug 13, 2017 at 03:37 PM
WOW!!! LMAO, I am a so dumb... I unchecked the "Simulated" checkbox because I thought this had something to do with Simulated Gravity... Everything on my Scripts, and in the inspector was perfectly functional except the Triggers were not working because Simulated was disabled... Found this on another Unity Forum post after Hours of searching hopefully this helps someone
The Source I used to solve my problem https://forum.unity3d.com/threads/ontriggerenter2d-not-working.233398/
Answer by as30050273 · Aug 13, 2017 at 08:54 AM
Hey! Looks like you need trigger collider on enemy. OnTriggerEnter2D() works only if GameObject, that own this script has collider with 'Trigger' flag.
I'm sorry I don't fully understand what you mean... I am pretty new to coding but what I think you're suggesting is the Enemy $$anonymous$$ovement script that has the OnTriggerEnter() is attached the checkpoints. I have a 2dBoxCollider on both the Checkpoints & the enemy. On the Enemy, it does not have the Trigger Collider ticked because it has to be affected by projectiles that I plan to add in the future. The Checkpoints, however, do have the Trigger component ticked.
The checkpoints Are trigger colliders. Check the screenshots.
No, enemy should have trigger to make ontrigger function work. Look at my 2-nd suggestion.
Ok, if you need your enemy's collider to not be trigger, i can suggest you add a script to checkpoint object. Like that:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Script : $$anonymous$$onoBehaviour { void OnTriggerEnter2D(Collider2D col){ col.gameObject.GetComponent<Enemy$$anonymous$$ovement>().target ++; } }
And ofc remove OnTriggerEnter2d function from enemy script
EDIT: You should do same thing for finish
Oh, im sorry for that strange, 1-line code. Idk how to fix it
As for your suggestion, I just tried adding it and it still does not work its like the first checkpoint is a vortex and the Enemy can't follow the path after hitting the first checkpoint lol
here is what I added I also used your code without the If Statement and it still did not work and I also removed and replaced the "IS TRIGGER" on both the enemy and checkpoint trying to alternate them and neither worked
void OnTriggerEnter2D(Collider2D col)
{
if (col.CompareTag("Enemy"))
{
col.gameObject.GetComponent<Enemy$$anonymous$$ovement>().target ++;
}
}
I still have not managed to have any luck in making the Enemy move from checkpoint 1-21 like the video I have rewatched it many times checked the code line by line and everything and nothing is different except this code I added for 2 spawns ins$$anonymous$$d of 1
if (enemiesOnScreen < maxEnemiesOnScreen)
{
int spawnIndex = Random.Range(0, spawnPoint.Length);
GameObject newEnemy = Instantiate(enemies[0]) as GameObject;
newEnemy.transform.position = spawnPoint[spawnIndex].position;
enemiesOnScreen += 1;
}
I am clueless why this Trigger Event is not working... I just tried using the Debug.Log and Print to see if it would show in the console and the OnTriggerEnter2d is acting like it does not exist...
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D crashing editor 0 Answers
How to get a variable from a triggers script. 0 Answers
Problems with Gyroscope VR Car Game 0 Answers
Enabling multiple Monobehaviour Components in a game object 1 Answer
[SOLVED] According to "print" message boolean variable turns true but if statment doesn't do stuff., 1 Answer