whiteviperx
whiteviperx
CC#
Created by whiteviperx on 3/4/2023 in #help
❔ Creating a grid
Trying to create a 5 x 5 grid, but because object is 3d it shows up sideways
using UnityEngine;

public class LevelManager : MonoBehaviour
{
// V = vertical grid qty
private const int V = 5;

// H = horizontal grid qty
private const int H = 5;

[SerializeField]
private GameObject tile;

// Start is called before the first frame update
private void Start()
{
CreateLevel();
}

// Update is called once per frame
private void Update()
{
}

private void CreateLevel()
{
float tileSize = tile.GetComponent<MeshRenderer>().bounds.size.x;

{
for(int x = 0; x < H; x++)
{
for(int z = 0; z < V; z++)
{
GameObject newTile = Instantiate(tile);
newTile.transform.position = new Vector3(tileSize * x, 0, tileSize * z);
}
}
}
}
}

using UnityEngine;

public class LevelManager : MonoBehaviour
{
// V = vertical grid qty
private const int V = 5;

// H = horizontal grid qty
private const int H = 5;

[SerializeField]
private GameObject tile;

// Start is called before the first frame update
private void Start()
{
CreateLevel();
}

// Update is called once per frame
private void Update()
{
}

private void CreateLevel()
{
float tileSize = tile.GetComponent<MeshRenderer>().bounds.size.x;

{
for(int x = 0; x < H; x++)
{
for(int z = 0; z < V; z++)
{
GameObject newTile = Instantiate(tile);
newTile.transform.position = new Vector3(tileSize * x, 0, tileSize * z);
}
}
}
}
}

4 replies
CC#
Created by whiteviperx on 3/1/2023 in #help
❔ Tower Defense Enemy Movement
Having a problem with line 37....
1
2 using UnityEngine;
3
4 [RequireComponent(typeof(Enemies))]
5 public class EnemyMovement : MonoBehaviour
6 {
7 //public static Transform [] waypoints;
8 private Transform target;
9
10 private int wavepointIndex = 0;
11
12 private Enemies enemy;
13 private object waypoints;
14
15 private void Start()
16 {
17 enemy = GetComponent<Enemies>();
18
19 target = (Transform)waypoints;
20 }
21
22 private void Update()
23 {
24 Vector3 dir = target.position - transform.position;
25 transform.Translate(enemy.speed * Time.deltaTime * dir.normalized, Space.World);
26
27 if(Vector3.Distance(transform.position, target.position) <= 0.4f)
28 {
29 GetNextWaypoint();
30 }
31
32 enemy.speed = enemy.startSpeed;
33 }
34
35 private void GetNextWaypoint()
36 {
37 if(wavepointIndex >= Waypoints.- 1)
38 {
39 EndPath();
40 return;
41 }
42 else
43 {
44 wavepointIndex++;
45 target = Waypoints.points [wavepointIndex];
46 }
47 }
48
49 private void EndPath()
50 {
51 PlayerStats.Lives--;
52 WaveSpawner.EnemiesAlive--;
53 Destroy(gameObject);
54 }
55 }
56
1
2 using UnityEngine;
3
4 [RequireComponent(typeof(Enemies))]
5 public class EnemyMovement : MonoBehaviour
6 {
7 //public static Transform [] waypoints;
8 private Transform target;
9
10 private int wavepointIndex = 0;
11
12 private Enemies enemy;
13 private object waypoints;
14
15 private void Start()
16 {
17 enemy = GetComponent<Enemies>();
18
19 target = (Transform)waypoints;
20 }
21
22 private void Update()
23 {
24 Vector3 dir = target.position - transform.position;
25 transform.Translate(enemy.speed * Time.deltaTime * dir.normalized, Space.World);
26
27 if(Vector3.Distance(transform.position, target.position) <= 0.4f)
28 {
29 GetNextWaypoint();
30 }
31
32 enemy.speed = enemy.startSpeed;
33 }
34
35 private void GetNextWaypoint()
36 {
37 if(wavepointIndex >= Waypoints.- 1)
38 {
39 EndPath();
40 return;
41 }
42 else
43 {
44 wavepointIndex++;
45 target = Waypoints.points [wavepointIndex];
46 }
47 }
48
49 private void EndPath()
50 {
51 PlayerStats.Lives--;
52 WaveSpawner.EnemiesAlive--;
53 Destroy(gameObject);
54 }
55 }
56
12 replies