hey guys. i’m trying to make a duplicate character of my main player character and i want to make that duplicate an enemy against the main character player. a duplicate character that moves in opposite direction of main character while shooting at the main player at the same time moving in the opposite direction. i want to make multiple duplicates like this and make them enemies against the main player shooting at the main player. how do i do it? i know how to make a simple duplicate of the main character player. but then how do i turn that duplicate into an enemy against the main player?
This is a huge area of study. Look up how to make AI… and when I use the term “AI” it is the broadest sense of making an autonomous agent that does things independent or dependent on the player action.
Here are some small steps towards success:
-
make a guy who hangs out and scans for the player, if he sees the player he shoots at the player
-
make that guy patrol a given set path (tons of tutorials on patrolling) and if he sees the player he shoots at him
-
once the above is working, make something that listens for the player’s footsteps or gunshots and notifies all the enemies where he is, so they can run over to him.
Start small, iterate. It’s a VERY complex topic.
okay. i’ll check this stuff out. but do you know how to do it step by step? detail by detail?
Yes but I don’t make tutorials. Plenty of others do however.
i need you to walk me through it. i need you to be my tutorial since it is complicated and complex to do in unity
hey guys. who out there knows how to do this step by step? detail by detail? there must be somebody with the experience who can help me
You will want to go through some tutorials first as suggested, perhaps try https://learn.unity.com/ . You likely won’t find exactly the answer to your question, but you’ll learn how to do it yourself. Without seeing your project and your existing code, we would not be able to make specific suggestions.
hey jeff. okay the game right now is called LASER. it features a main player that is like a robot character and it moves around in a yellow trim border structure and dodges and kills enemy animated snakes and enemy animated spiders with with purple color laser fire. if the animated snakes and animated spiders just hit you once then it is game over. and with the new changes i want to make i will then call it LASER DROIDS. the new changes would be to create duplicates of the main player character robot and turn them into enemies into droids that would move in the opposite direction of the player while shooting at the player at the same time with laser fire. one enemy droid would appear at a time. one would be yellow. one would be blue. one would be red. and one would be purple. the goal of LASER is to get a billion points on one hit death. that would be the points goal of LASER DROIDS too. on LASER there are coins that appear each time you kill an animated snake and an animated spider. coins collected add points to your score in LASER. and i want this feature to be in LASER DROIDS too each time you kill a laser droid for coins to appear. if you kill a droid 5 coins would appear and you have to collect the coins to activate the power ups like laser spread, laser bombs, and laser storm. in LASER the power ups activate after you collect 5 coins too. and each change would be endless until you reach the billion. once you do that one scaled up in size red droid, purple droid, yellow droid, and blue droid would appear at the same time. and the player would have to kill all 4 super sized droids with 5 kill hits each a piece. and then the word VICTORY appears in the middle of the screen for 5 seconds and then you would get looped back into the main menu screen. this is what i’d like to do for LASER DROIDS. now i will copy and paste the code that was used for LASER. i’d please need you to help me how to modify this code so i can make the changes from LASER to LASER DROIDS.
here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class levelManager : MonoBehaviour
{
public GameObject gameOverScreen;
public GameObject pauseScreen;
public GameObject restartButton;
public GameObject continueButton;
public GameObject player2;
public AudioSource music;
public AudioSource gameOverSound;
//Shooting controls for both players
public Shooting shooting;
public Shooting shooting2;
public int points = 0;
public int level = 1;
public int levelCounter = 0;
public int toBossCounter = 1000;
public bool pause = false;
public int coinsCollected; //Count of how many coins have been collected
public int scoreToWin = 1000000000; //The score needed to win the game
//UI elements
public Text levelIndicator;
public Text pointText;
public GameObject victoryText;
//Prefabs
public GameObject coinPrefab;
public GameObject spiderBossPrefab;
//List Roots
public GameObject coinRoot;
public GameObject enemyRoot;
public GameObject[ ] Bosses;
//GameObject Lists
public List enemies;
public EnemySpawner[ ] spawners;
//Audio
public AudioSource audioSource;
public AudioClip[ ] audioClipArray;
AudioClip lastClip;
//Powerup enums
PowerupType currentPowerup;
PowerupType lastPowerup;
//Are we at the final bosses?
bool bossLevel = false;
private void Awake()
{
currentPowerup = PowerupType.POWER_UP_NONE;
}
public void playerHit()
{
gameOver();
}
//Callback for when an enemy has been killed
public void onEnemyKilled(GameObject enemy)
{
SpawnCoin(enemy.transform.position); //Spawn a coin in their location
points += enemy.GetComponent().points;
enemies.Remove(enemy); //Destroy the enemy
Destroy(enemy);
OnPointsAdded();
}
//Callback for when points have been added to the player’s score
public void OnPointsAdded()
{
Debug.Log(“In points to win " + points);
pointText.text = points + " POINTS”;
Debug.Log(scoreToWin);
if (points >= scoreToWin && !bossLevel)
{
Debug.Log(“Should have bosses”);
bossLevel = true;
DisableSpawners();
ClearEnemies();
for (int i = 0; i < 4; i++)
{
SpawnSpiderBoss();
}
}
if (points >= scoreToWin && enemies.Count <= 0)
{
OnVictory();
}
}
//Remove all enemies
public void ClearEnemies()
{
foreach(GameObject g in enemies)
{
Destroy(g);
}
enemies.Clear();
}
//Enable spawners
public void EnableSpawners()
{
foreach (EnemySpawner e in spawners)
{
e.enable();
}
}
//Disable spawners
public void DisableSpawners()
{
foreach(EnemySpawner e in spawners)
{
e.disable();
}
}
//Spawn a spider boss
public void SpawnSpiderBoss()
{
GameObject g = Instantiate(spiderBossPrefab, new Vector3(0, 0, 0), Quaternion.identity, enemyRoot.transform);
onEnemySpawned(g);
}
//Callback for when the player has won
public void OnVictory()
{
victoryText.SetActive(true);
StartCoroutine(ResetVictory());
}
//Wait 5 seconds and switch scenes to the main menu
public IEnumerator ResetVictory()
{
yield return new WaitForSeconds(5);
victoryText.SetActive(false);
SceneManager.LoadScene(“MainMenu”);
}
//Callback for when a coin has been collected
public void onCoinCollected()
{
coinsCollected++;
points += 1000000;
if(coinsCollected >= 1 && currentPowerup == PowerupType.POWER_UP_NONE)
{
if(lastPowerup == PowerupType.POWER_UP_SPREAD)
{
currentPowerup = PowerupType.POWER_UP_BOMB;
shooting.enableBombPowerup();
shooting2.enableBombPowerup();
StartCoroutine(ResetBomb());
}
else if (lastPowerup == PowerupType.POWER_UP_BOMB)
{
currentPowerup = PowerupType.POWER_UP_STORM;
shooting.enableStormPowerup();
shooting2.enableStormPowerup();
StartCoroutine(ResetStorm());
}
else if (lastPowerup == PowerupType.POWER_UP_STORM)
{
currentPowerup = PowerupType.POWER_UP_SPREAD;
shooting.enableSpreadPowerup();
shooting2.enableSpreadPowerup();
StartCoroutine(ResetSpread());
}
else
{
currentPowerup = PowerupType.POWER_UP_SPREAD;
shooting.enableSpreadPowerup();
shooting2.enableSpreadPowerup();
StartCoroutine(ResetSpread());
}
}
OnPointsAdded();
}
//Wait 5 seconds and then disable the spread powerup
public IEnumerator ResetSpread()
{
yield return new WaitForSeconds(5);
lastPowerup = PowerupType.POWER_UP_SPREAD;
currentPowerup = PowerupType.POWER_UP_NONE;
shooting.disableSpreadPowerup();
shooting2.disableSpreadPowerup();
coinsCollected = 0;
}
//Wait 60 seconds and then disable the bomb powerup
public IEnumerator ResetBomb()
{
yield return new WaitForSeconds(60);
lastPowerup = PowerupType.POWER_UP_BOMB;
currentPowerup = PowerupType.POWER_UP_NONE;
shooting.disableBombPowerup();
shooting2.disableBombPowerup();
coinsCollected = 0;
}
public IEnumerator ResetStorm()
{
Debug.Log(“Start reset storm”);
yield return new WaitForSeconds(60);
Debug.Log(“Finish reset storm”);
lastPowerup = PowerupType.POWER_UP_STORM;
currentPowerup = PowerupType.POWER_UP_NONE;
shooting.disableStormPowerup();
shooting2.disableStormPowerup();
coinsCollected = 0;
}
//Spawn a coin at the specified location
private void SpawnCoin(Vector3 pos)
{
GameObject.Instantiate(coinPrefab, pos, Quaternion.identity, coinRoot.transform);
}
//Callback for when an enemy has been spawned
public void onEnemySpawned(GameObject enemy)
{
enemy.transform.SetParent(enemyRoot.transform);
enemies.Add(enemy);
}
//Destroy all enemies within radius range of point
public void DestroyEnemiesWithinRange(Vector3 point, float radius)
{
List toRemove = new List();
foreach(GameObject e in enemies)
{
if(e != null)
{
if (Vector3.Distance(e.transform.position, point) <= radius)
{
if (e.GetComponent())
{
if(e.GetComponent().health - 1 <= 0)
{
toRemove.Add(e);
}
else
{
e.GetComponent().health -= 1;
}
}
else
{
toRemove.Add(e);
}
}
}
}
foreach(GameObject g in toRemove)
{
onEnemyKilled(g);
}
toRemove.Clear();
}
public void gameOver()
{
pause = true;
Time.timeScale = 0;
music.Stop();
gameOverSound.Play();
gameOverScreen.SetActive(true);
restartButton.GetComponent().Select();
}
public void pausing()
{
pause = true;
Time.timeScale = 0;
//music.Stop();
//gameOverSound.Play();
pauseScreen.SetActive(true);
continueButton.GetComponent().Select();
}
public void continuing()
{
pause = false;
Time.timeScale = 1;
//music.Play();
//gameOverSound.Play();
pauseScreen.SetActive(false);
//continueButton.GetComponent().Select();
}
public void reload()
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
Time.timeScale = 1;
}
public void Update()
{
if (toBossCounter <= 0)
{
for(int i = 0; i< Bosses.Length; i++)
{
Bosses*.SetActive(true);*
}
}
if (!audioSource.isPlaying && !pause)
{
AudioClip newClip = RandomClip();
audioSource.PlayOneShot(newClip);
}
if ((Input.GetButton(“Menu”)))
{
pausing();
}
if (points >= levelCounter + 10000)
{
levelCounter = levelCounter + 10000;
level = level + 1;
levelIndicator.text = "Level " + level;
}
}
void Start()
{
if (ButtonControl.Player2)
{
player2.SetActive(true);
}
pause = false;
AudioClip newClip = RandomClip();
audioSource.PlayOneShot(newClip);
}
AudioClip RandomClip()
{
int attempts = 3;
AudioClip newClip = audioClipArray[Random.Range(0, audioClipArray.Length)];
while (newClip == lastClip && attempts > 0)
{
newClip = audioClipArray[Random.Range(0, audioClipArray.Length)];
attempts–;
}
lastClip = newClip;
return newClip;
}
}
this is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class levelManager : MonoBehaviour
{
public GameObject gameOverScreen;
public GameObject pauseScreen;
public GameObject restartButton;
public GameObject continueButton;
public GameObject player2;
public AudioSource music;
public AudioSource gameOverSound;
//Shooting controls for both players
public Shooting shooting;
public Shooting shooting2;
public int points = 0;
public int level = 1;
public int levelCounter = 0;
public int toBossCounter = 1000;
public bool pause = false;
public int coinsCollected; //Count of how many coins have been collected
public int scoreToWin = 1000000000; //The score needed to win the game
//UI elements
public Text levelIndicator;
public Text pointText;
public GameObject victoryText;
//Prefabs
public GameObject coinPrefab;
public GameObject spiderBossPrefab;
//List Roots
public GameObject coinRoot;
public GameObject enemyRoot;
public GameObject[ ] Bosses;
//GameObject Lists
public List enemies;
public EnemySpawner[ ] spawners;
//Audio
public AudioSource audioSource;
public AudioClip[ ] audioClipArray;
AudioClip lastClip;
//Powerup enums
PowerupType currentPowerup;
PowerupType lastPowerup;
//Are we at the final bosses?
bool bossLevel = false;
private void Awake()
{
currentPowerup = PowerupType.POWER_UP_NONE;
}
public void playerHit()
{
gameOver();
}
//Callback for when an enemy has been killed
public void onEnemyKilled(GameObject enemy)
{
SpawnCoin(enemy.transform.position); //Spawn a coin in their location
points += enemy.GetComponent().points;
enemies.Remove(enemy); //Destroy the enemy
Destroy(enemy);
OnPointsAdded();
}
//Callback for when points have been added to the player’s score
public void OnPointsAdded()
{
Debug.Log(“In points to win " + points);
pointText.text = points + " POINTS”;
Debug.Log(scoreToWin);
if (points >= scoreToWin && !bossLevel)
{
Debug.Log(“Should have bosses”);
bossLevel = true;
DisableSpawners();
ClearEnemies();
for (int i = 0; i < 4; i++)
{
SpawnSpiderBoss();
}
}
if (points >= scoreToWin && enemies.Count <= 0)
{
OnVictory();
}
}
//Remove all enemies
public void ClearEnemies()
{
foreach(GameObject g in enemies)
{
Destroy(g);
}
enemies.Clear();
}
//Enable spawners
public void EnableSpawners()
{
foreach (EnemySpawner e in spawners)
{
e.enable();
}
}
//Disable spawners
public void DisableSpawners()
{
foreach(EnemySpawner e in spawners)
{
e.disable();
}
}
//Spawn a spider boss
public void SpawnSpiderBoss()
{
GameObject g = Instantiate(spiderBossPrefab, new Vector3(0, 0, 0), Quaternion.identity, enemyRoot.transform);
onEnemySpawned(g);
}
//Callback for when the player has won
public void OnVictory()
{
victoryText.SetActive(true);
StartCoroutine(ResetVictory());
}
//Wait 5 seconds and switch scenes to the main menu
public IEnumerator ResetVictory()
{
yield return new WaitForSeconds(5);
victoryText.SetActive(false);
SceneManager.LoadScene(“MainMenu”);
}
//Callback for when a coin has been collected
public void onCoinCollected()
{
coinsCollected++;
points += 1000000;
if(coinsCollected >= 1 && currentPowerup == PowerupType.POWER_UP_NONE)
{
if(lastPowerup == PowerupType.POWER_UP_SPREAD)
{
currentPowerup = PowerupType.POWER_UP_BOMB;
shooting.enableBombPowerup();
shooting2.enableBombPowerup();
StartCoroutine(ResetBomb());
}
else if (lastPowerup == PowerupType.POWER_UP_BOMB)
{
currentPowerup = PowerupType.POWER_UP_STORM;
shooting.enableStormPowerup();
shooting2.enableStormPowerup();
StartCoroutine(ResetStorm());
}
else if (lastPowerup == PowerupType.POWER_UP_STORM)
{
currentPowerup = PowerupType.POWER_UP_SPREAD;
shooting.enableSpreadPowerup();
shooting2.enableSpreadPowerup();
StartCoroutine(ResetSpread());
}
else
{
currentPowerup = PowerupType.POWER_UP_SPREAD;
shooting.enableSpreadPowerup();
shooting2.enableSpreadPowerup();
StartCoroutine(ResetSpread());
}
}
OnPointsAdded();
}
//Wait 5 seconds and then disable the spread powerup
public IEnumerator ResetSpread()
{
yield return new WaitForSeconds(5);
lastPowerup = PowerupType.POWER_UP_SPREAD;
currentPowerup = PowerupType.POWER_UP_NONE;
shooting.disableSpreadPowerup();
shooting2.disableSpreadPowerup();
coinsCollected = 0;
}
//Wait 60 seconds and then disable the bomb powerup
public IEnumerator ResetBomb()
{
yield return new WaitForSeconds(60);
lastPowerup = PowerupType.POWER_UP_BOMB;
currentPowerup = PowerupType.POWER_UP_NONE;
shooting.disableBombPowerup();
shooting2.disableBombPowerup();
coinsCollected = 0;
}
public IEnumerator ResetStorm()
{
Debug.Log(“Start reset storm”);
yield return new WaitForSeconds(60);
Debug.Log(“Finish reset storm”);
lastPowerup = PowerupType.POWER_UP_STORM;
currentPowerup = PowerupType.POWER_UP_NONE;
shooting.disableStormPowerup();
shooting2.disableStormPowerup();
coinsCollected = 0;
}
//Spawn a coin at the specified location
private void SpawnCoin(Vector3 pos)
{
GameObject.Instantiate(coinPrefab, pos, Quaternion.identity, coinRoot.transform);
}
//Callback for when an enemy has been spawned
public void onEnemySpawned(GameObject enemy)
{
enemy.transform.SetParent(enemyRoot.transform);
enemies.Add(enemy);
}
//Destroy all enemies within radius range of point
public void DestroyEnemiesWithinRange(Vector3 point, float radius)
{
List toRemove = new List();
foreach(GameObject e in enemies)
{
if(e != null)
{
if (Vector3.Distance(e.transform.position, point) <= radius)
{
if (e.GetComponent())
{
if(e.GetComponent().health - 1 <= 0)
{
toRemove.Add(e);
}
else
{
e.GetComponent().health -= 1;
}
}
else
{
toRemove.Add(e);
}
}
}
}
foreach(GameObject g in toRemove)
{
onEnemyKilled(g);
}
toRemove.Clear();
}
public void gameOver()
{
pause = true;
Time.timeScale = 0;
music.Stop();
gameOverSound.Play();
gameOverScreen.SetActive(true);
restartButton.GetComponent().Select();
}
public void pausing()
{
pause = true;
Time.timeScale = 0;
//music.Stop();
//gameOverSound.Play();
pauseScreen.SetActive(true);
continueButton.GetComponent().Select();
}
public void continuing()
{
pause = false;
Time.timeScale = 1;
//music.Play();
//gameOverSound.Play();
pauseScreen.SetActive(false);
//continueButton.GetComponent().Select();
}
public void reload()
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
Time.timeScale = 1;
}
public void Update()
{
if (toBossCounter <= 0)
{
for(int i = 0; i< Bosses.Length; i++)
{
Bosses*.SetActive(true);*
}
}
if (!audioSource.isPlaying && !pause)
{
AudioClip newClip = RandomClip();
audioSource.PlayOneShot(newClip);
}
if ((Input.GetButton(“Menu”)))
{
pausing();
}
if (points >= levelCounter + 10000)
{
levelCounter = levelCounter + 10000;
level = level + 1;
levelIndicator.text = "Level " + level;
}
}
void Start()
{
if (ButtonControl.Player2)
{
player2.SetActive(true);
}
pause = false;
AudioClip newClip = RandomClip();
audioSource.PlayOneShot(newClip);
}
AudioClip RandomClip()
{
int attempts = 3;
AudioClip newClip = audioClipArray[Random.Range(0, audioClipArray.Length)];
while (newClip == lastClip && attempts > 0)
{
newClip = audioClipArray[Random.Range(0, audioClipArray.Length)];
attempts–;
}
lastClip = newClip;
return newClip;
}
}
Well Jeff, you asked for that!
I did indeed. So @macha77 here’s the deal. I don’t know your skill level, but I’m guessing that you may have copied that code without fully understanding how it works. On the forum, we typically won’t write the code for you, but will happily help you along. My recommendation would be for you to take about an hour and review other posts on this forum, and see how they format the code in their posts using Code Tags, you’ll hear that a lot. It makes the code much easier for us to read. And then take about 2 weeks to review a few tutorials on https://learn.unity.com . If you don’t fully understand the existing code, I might suggest that adding to it is not a good idea. Apologies if I misjudged the situation. One note, you might have better luck right away if you edited your posts and used Code Tags .