Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question I need help with a tower defense game.

Discussion in 'Scripting' started by eanderso, Aug 26, 2023.

  1. eanderso

    eanderso

    Joined:
    May 5, 2023
    Posts:
    4
    I am making a toweled defense game and I made it so when you press space a tower appears but I want to make it so if you place a tower on a path I makes a land mine. paths are tiles using the tile mapping feature

    here is my code for spawning towers:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class zFix : MonoBehaviour
    6. {
    7.     private Rigidbody rb;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         rb = GetComponent<Rigidbody>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         rb.position = new Vector3(transform.position.x,transform.position.y,-1);
    18.     }
    19. }
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    You must have pasted the wrong thing, that code is only brutally setting the rigidbody position every frame.
     
  3. eanderso

    eanderso

    Joined:
    May 5, 2023
    Posts:
    4
    yup sorry here's the real code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class spawner : MonoBehaviour
    7. {
    8.     float countdown;
    9.     float levelnum;
    10.    public GameObject Prefab;
    11.  
    12.     // Update is called once per frame
    13.     void Start()
    14.     {
    15.        
    16.         levelnum = 1;
    17.     }
    18.    
    19.     void SpawnObject()
    20.     {
    21.         Instantiate(Prefab,transform.position,Quaternion.identity);
    22.     }
    23.     public void start_level()
    24.     {   if(countdown<levelnum)
    25.         {
    26.             Instantiate(Prefab,transform.position,Quaternion.identity);
    27.            
    28.             countdown ++;
    29.         }
    30.         else
    31.         {
    32.             levelnum = levelnum + 1;
    33.             countdown = 0;
    34.         }
    35.     }
     
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Classes, which are Components, along with method/function calls should all start with capital letters. Any made variable or declaration should start with lower case letters. That way just looking at it, you, or anyone else can easily see what's what.
    Code (CSharp):
    1. public class Spawner : MonoBehaviour
    2. {
    3.     float countdown;
    4.     float levelnum = 1;
    5.     public GameObject myPrefab;
    6.    
    7.     void SpawnObject()
    8.     {
    9.         Instantiate(myPrefab, transform.position, Quaternion.identity);
    10.     }
    11.    
    12.     public void Start_Level()
    13.     {  
    14.         if (countdown < levelnum)
    15.         {
    16.             SpawnObject();
    17.             countdown++;
    18.         }
    19.         else
    20.         {
    21.             levelnum++;
    22.             countdown = 0;
    23.         }
    24.     }
    25. }
    Ok, now that it's readable, I'm not exactly sure what's going on here. So I assume another manager class is calling this classes "Start_Level()" every frame? If this class is just one spawner, and you're not too worried about severe micro-performance, this class should have no problems running it's own Update() method. Unless you have checks on the manager class, that control spawning at different intervals, then I see, all good.

    But your question originally was:
    This would all have to do with your raycast method, and determining layer group to if on path or not, and then from that check only call the land mine Instantiate instead of a normal tower Instantiate.
     
  5. eanderso

    eanderso

    Joined:
    May 5, 2023
    Posts:
    4
    um I don't have a clue how to do ray-cast
     
  6. eanderso

    eanderso

    Joined:
    May 5, 2023
    Posts:
    4
    and I'm still working on the start level thing
     
  7. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Ohh ok, so you just started playing with code, you're much better off doing some tutorials(code for code), and get some working examples you can always pull and mix code from later. Tutorials normally give you all the info you need, start to finish. :)
     
  8. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I suggest "Code Monkey" from YouTube, he's very good at what he does, and he has a lot of tower defense examples.
     
    Chubzdoomer likes this.