How to infinitely generate a grid in unity? - c#

So I have this piece of code that generates a 7x50 grid that falls down.
public class FieldController : MonoBehaviour
{
[SerializeField] private GameObject gridPrefab;
[SerializeField] private int rows = 50;
[SerializeField] private int columns = 7;
[SerializeField] private float tileSize = 1;
private GameObject tile;
private GameObject parentObject;
private float gridW;
private float gridH;
private float posX;
private float posY;
private void Start()
{
parentObject = new GameObject("Parent");
GenerateGrid();
}
private void Update()
{
MoveGridDown();
}
void GenerateGrid()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
tile = Instantiate(gridPrefab, parentObject.transform);
posX = j * tileSize;
posY = i * tileSize;
tile.transform.position = new Vector2(posX, posY);
}
}
SetParentToStart(parentObject);
}
void SetParentToStart(GameObject parent)
{
parent.transform.position = new Vector3(-columns / 2 + tileSize, rows / 10, 0);
}
void MoveGridDown()
{
parentObject.transform.position -= new Vector3(0, FallingObjectBehaviour.FallingSpeed * Time.deltaTime);
}
}
This is how it looks:
https://imgur.com/a/nKtOuyV
What I want to do is to be able to generate the new grid every time the previous one reaches the end so it would look like the grid is infinite.
I've tried to use extra GameObject for a parent and switch between them but the results were buggy.
Is there any way to make it possible?

i believe you want to generate the next grid before it reaches the end, and i suggest a system where you have
GameObject gridPrefab;
GameObject grid;
Queue<GameObject> grids;
Transform camera;
Vector3 newPos = Vector3.zero;
Vector3 offset = new Vector3(0,0,50);
Quaternion forward = Quaternion.Euler(Vector3.forward);
int i = 0;
then you instantiate the new grid once camera is beyond "newPos", add it to the queue and destroy the last one.
if(newPos.z > camera.z)
{
newPos += offset;
grid = Instantiate(gridPrefab,newPos,forward);
grid.name = $"grid{++i}";
grids.Enqueue(grid);
Destroy(grids.Dequeue());
}
this is done without testing, let me know if makes sense to you, i can test it later if makes sense but is not working.

Related

How to change TrailRenderer's space To LocalSpace

How to make Local Space TrailRenderer
I want to make TrailRenderer use Local Space. Because I use TrailRenderer in my Player's Sword Effect like that Image.
When the Player stopped in place, there is no problem. But as the player moves, the TrailRender moves too. So the TrailRenderer'shape is strange like that Image.
I tried all of TrailRenderer's Z Position set 0 But that's failed.
How to solve this Problem??
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trail : MonoBehaviour
{
[SerializeField]
private Transform _trailPos;
TrailRenderer _renderer;
[SerializeField]
private Vector3[] _t;
private void Start()
{
_renderer = GetComponent<TrailRenderer>();
}
private void LateUpdate()
{
transform.position = _trailPos.position;
for (int i = 0; i < _renderer.positionCount; ++i)
{
Vector3[] v = new Vector3[_renderer.positionCount];
_renderer.GetPositions(v);
for (int j = 0; j < v.Length; ++j)
{
v[j] = (Vector2)v[j];
}
_renderer.SetPositions(v);
}
_t = new Vector3[_renderer.positionCount];
_renderer.GetPositions(_t);
}
}
You could probably try and track your player movement and apply the same delta to the trail positions.
Something like e.g.
[SerializeField] private TrailRenderer trailRenderer;
[SerializeField] private Transform player;
private Vector3 lastFramePosition;
private void Start()
{
lastFramePosition = player.position;
}
private void LateUpdate()
{
var delta = player.position - lastFramePosition;
lastFramePosition = player.position;
var positions = new Vector3[trailRenderer.positionCount];
trailRenderer.GetPositions(positions);
for (var i = 0; i < trailRenderer.positionCount; i++)
{
positions[i] += delta;
}
trailRenderer.SetPositions(positions);
}

Spawning and destroying random platforms in unity

I am trying to make a doodle jump game in unity for fun and I have a question.
How could I change my script to spawn other platforms on top and destroy any platform that is under main camera.I'll let down my code for spawning random platforms
public class GradinariuZAHAR : MonoBehaviour
{
public Transform Player;
public GameObject platformPrefab;
private float minimY = 0.2f;
private float maximY = 1.6f;
public int numberOfPlatforms = 200;
public float levelWidth = 3f;
// Use this for initialization
void Start()
{
GenerateChunk(.2f, 1.5f);
}
void GenerateChunk(float minY, float maxY)
{
Vector3 spawnPosition = new Vector3(0f, 0f, 0f);
for (int i = 0; i < numberOfPlatforms; i++)
{
spawnPosition.y += Random.Range(minY, maxY);
spawnPosition.x = Random.Range(-levelWidth, levelWidth);
Instantiate(platformPrefab, spawnPosition, Quaternion.identity);
}
}
void Update()
{
if (Player.position.y > 160) {
Debug.Log("acum");
minimY = maximY;
maximY = maximY * 2;
GenerateChunk(minimY, maximY);
}
}
}
Just attach the following script to your platform. It'll destroy the platform once its entire sprite is completely below the camera, exactly how you want it.
using UnityEngine;
public class Platform : MonoBehaviour
{
SpriteRenderer sprite;
float bottomOfScreen;
private void Start()
{
sprite = GetComponent<SpriteRenderer>();
var cam = Camera.main;
var screen = new Vector2(Screen.width, Screen.height);
var camWorldPos = cam.ScreenToWorldPoint(screen);
bottomOfScreen = camWorldPos.y - cam.orthographicSize * 2;
}
private void Update()
{
var height = sprite.bounds.size.y;
var topOfPlatform = transform.position.y + height / 2;
if (topOfPlatform < bottomOfScreen)
Destroy(gameObject);
}
}

How can I prevent from adding more and more new objects when pressing F to change formation?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SquadFormation : MonoBehaviour
{
enum Formation
{
Square, Circle, Triangle
}
[Header("Main Settings")]
[Space(5)]
public Transform squadMemeberPrefab;
[Range(4, 100)]
public int numberOfSquadMembers = 20;
[Range(1, 20)]
public int numberOfSquads = 1;
[Range(0, 4)]
public int columns = 4;
public int gaps = 10;
public int circleRadius = 10;
public float yOffset = 0;
[Range(3, 50)]
public float moveSpeed = 3;
[Range(3, 50)]
public float rotateSpeed = 1;
public float threshold = 0.1f;
public bool randomSpeed = false;
[Range(1, 100)]
public int randSpeedMin = 1;
[Range(1, 100)]
public int randSpeedMax = 1;
public bool startRandomFormation = false;
public string currentFormation;
private Formation formation;
private List<Quaternion> quaternions = new List<Quaternion>();
private List<Vector3> newpositions = new List<Vector3>();
private bool move = false;
private bool squareFormation = false;
private List<GameObject> squadMembers = new List<GameObject>();
private float[] step;
private int[] randomSpeeds;
private int index = 0;
private int numofobjects = 0;
// Use this for initialization
void Start()
{
numofobjects = numberOfSquadMembers;
if (startRandomFormation)
{
formation = (Formation)UnityEngine.Random.Range(0, Enum.GetNames(typeof(Formation)).Length);
}
else
{
formation = Formation.Square;
}
currentFormation = formation.ToString();
ChangeFormation();
foreach (Transform child in gameObject.transform)
{
if (child.tag == "Squad Member")
squadMembers.Add(child.gameObject);
}
randomSpeeds = RandomNumbers(randSpeedMin, randSpeedMax, squadMembers.Count);
step = new float[squadMembers.Count];
}
// Update is called once per frame
void Update()
{
if (numofobjects != numberOfSquadMembers)
{
numofobjects = 0;
numofobjects = numberOfSquadMembers;
squadMembers = new List<GameObject>();
FormationSquare();
}
if (Input.GetKeyDown(KeyCode.F))
{
randomSpeeds = RandomNumbers(randSpeedMin, randSpeedMax, squadMembers.Count);
foreach (int speedV in randomSpeeds)
{
if (index == randomSpeeds.Length)
index = 0;
step[index] = speedV * Time.deltaTime;
index++;
}
ChangeFormation();
}
if (move == true)
{
MoveToNextFormation();
}
}
private void ChangeFormation()
{
switch (formation)
{
case Formation.Square:
FormationSquare();
break;
case Formation.Circle:
FormationCircle();
break;
}
}
private Vector3 FormationSquarePositionCalculation(int index) // call this func for all your objects
{
float posX = (index % columns) * gaps;
float posY = (index / columns) * gaps;
return new Vector3(posX, posY);
}
private void FormationSquare()
{
newpositions = new List<Vector3>();
quaternions = new List<Quaternion>();
Transform go = squadMemeberPrefab;
for (int i = 0; i < numofobjects; i++)
{
if (squadMembers.Count == 0)
go = Instantiate(squadMemeberPrefab);
Vector3 pos = FormationSquarePositionCalculation(i);
go.position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
go.Rotate(new Vector3(0, -90, 0));
go.tag = "Squad Member";
go.transform.parent = gameObject.transform;
newpositions.Add(go.transform.position);
}
move = true;
squareFormation = true;
formation = Formation.Circle;
}
private Vector3 FormationCirclePositionCalculation(Vector3 center, float radius, int index, float angleIncrement)
{
float ang = index * angleIncrement;
Vector3 pos;
pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
pos.z = center.z + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
pos.y = center.y;
return pos;
}
private void FormationCircle()
{
newpositions = new List<Vector3>();
quaternions = new List<Quaternion>();
Vector3 center = transform.position;
float radius = (float)circleRadius / 2;
float angleIncrement = 360 / (float)numberOfSquadMembers;
for (int i = 0; i < numberOfSquadMembers; i++)
{
Vector3 pos = FormationCirclePositionCalculation(center, radius, i, angleIncrement);
var rot = Quaternion.LookRotation(center - pos);
pos.y = Terrain.activeTerrain.SampleHeight(pos);
pos.y = pos.y + yOffset;
newpositions.Add(pos);
quaternions.Add(rot);
}
move = true;
squareFormation = false;
formation = Formation.Square;
}
private void MoveToNextFormation()
{
if (randomSpeed == false)
{
if (step.Length > 0)
step[0] = moveSpeed * Time.deltaTime;
}
for (int i = 0; i < squadMembers.Count; i++)
{
squadMembers[i].transform.LookAt(newpositions[i]);
if (randomSpeed == true)
{
squadMembers[i].transform.position =
Vector3.MoveTowards(squadMembers[i].transform.position, newpositions[i], step[i]);
}
else
{
squadMembers[i].transform.position =
Vector3.MoveTowards(squadMembers[i].transform.position, newpositions[i], step[0]);
}
if (Vector3.Distance(squadMembers[i].transform.position, newpositions[i]) < threshold)
{
if (squareFormation == true)
{
Vector3 degrees = new Vector3(0, 0, 0);
Quaternion quaternion = Quaternion.Euler(degrees);
squadMembers[i].transform.rotation = Quaternion.Slerp(squadMembers[i].transform.rotation, quaternion, rotateSpeed * Time.deltaTime);
}
else
{
squadMembers[i].transform.rotation = Quaternion.Slerp(squadMembers[i].transform.rotation, quaternions[i], rotateSpeed * Time.deltaTime);
}
}
}
}
private static int[] RandomNumbers(int min, int max, int howMany)
{
int[] myNumbers = new int[howMany];
for (int i = 0; i < howMany; i++)
{
myNumbers[i] = UnityEngine.Random.Range(min, max);
}
return myNumbers;
}
}
In the constructor I'm searching for childs with the tag Squad Member.
But the List squadMembers will be empty since the script is attached to a new empty GameObject without any childs.
Then also the variable step will be empty.
Then inside the method MoveToNextFormation I'm checking if step is empty or not:
if (step.Length > 0)
step[0] = moveSpeed * Time.deltaTime;
If not checking the it will throw exception since there is nothing at index 0 it's null. But then if step is empty there will be no speed/s at all for the objects movements.
That's one problem.
I'm not sure even why in the constructor I did the part with the children and the "Squad Member" tag. I'm not creating yet any children with this tag so I'm confused about what I tried to do in the constructor.
The second problem is in this lines in the FormationSquare method:
if (squadMembers.Count == 0)
go = Instantiate(squadMemeberPrefab);
But if squadMembers is empty then it will throw exception somewhere else in other places in the code. And I'm creating new objects inside the FormationSquare method that's since I'm starting by default with the FormationSquare but what if I want to start by default with the FormationCircle method ?
The idea is to start with minimum (1) number of squads and with minimum (4) number of members in the squad when starting the program. Or to start with any range between min and max. But it's all messed up.
In your case, I would separate the squad member prefab instantiation from the squad shape formatting, doing this will help you identify your bug.
For example add the following methods and use them during 'Start':
void AddSquadMember()
{
// Use this to instantiate/spawn a new game object prefab.
}
void AddSquadMember(GameObject object)
{
// Use this for game object already in the scene. (.eg the children with your tag)
}
Then on the formation methods remove the intantiate calls and just use whatever game object you have in the list.
Finally, I would toss the 'numofobjects' variable. Then use 'squadMembers.Count' instead of both 'numofobjects' and 'numberOfSquadMembers' assuming that during 'Start' you have taken care of instantiating all game objects in order to 'numberOfSquadMembers == squadMembers.Count'. That is because you might need to raise the squad with a few more members during gameplay.

Spawn image at random position in canvas

I have a canvas and inside it a panel. I want to spawn different image UI object at random position inside my panel (on screen). I took x and y min and max limits of the panel in order to generate a random position (withing these limts) but the problem is object are not instantiating at desired position. This is my code.
public class ImageSpawnerScreen : MonoBehaviour {
public float waitTime = 2;
public float cubeSpawnTotal = 10;
public List<GameObject> cubePrefabList;
float xPosMinLimit = -347;
float xPosMaxLimit = 340;
float yPosMinLimit = -458f;
float yPosMaxLimit = 450f;
public GameObject panel;
void Start()
{
StartCoroutine(SpawnCube());
}
IEnumerator SpawnCube()
{
for (int i = 0; i < cubeSpawnTotal; i++)
{
GameObject prefabToSpawn = cubePrefabList[Random.Range(0, cubePrefabList.Count - 1)];
//Vector3 spawnPosition = Camera.main.ScreenToViewportPoint(new Vector3(Random.Range(0,Screen.width),0,Random.Range(0,Screen.height))); //Random.Range(xPosMinLimit, xPosMaxLimit);
float xPos = Random.Range(xPosMinLimit, xPosMaxLimit);
float yPos = Random.Range(yPosMinLimit, yPosMaxLimit);
Vector3 spawnPosition = new Vector3(xPos, yPos, 0f);
GameObject spwanObj = Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity) as GameObject;
spwanObj.transform.parent = panel.transform;
spwanObj.transform.position = spawnPosition;
yield return new WaitForSeconds(waitTime);
}
}
}
I checked instantiated object positions are far away from the given random range position. What I am doing wrong? I think it is Rect Transform so i have to set its position differently.
Indeed, you need to use RectTransform inside a Canvas.
You also don't need the -1 for you List.Count. Check the link in the comment below.
I think the following script will do what you are looking for:
public class ImageSpawnerScreen : MonoBehaviour
{
public float waitTime = 2;
public float cubeSpawnTotal = 10;
public List<GameObject> imagesList;
public RectTransform panel;
void Start()
{
StartCoroutine(SpawnImage());
}
IEnumerator SpawnImage()
{
for (int i = 0; i < cubeSpawnTotal; i++)
{
GameObject imageToSpawn = imagesList[Random.Range(0, imagesList.Count)]; // Remove -1 after count since is exclusive for int (https://docs.unity3d.com/ScriptReference/Random.Range.html)
Vector3 spawnPosition = GetBottomLeftCorner(panel) - new Vector3(Random.Range(0, panel.rect.x), Random.Range(0, panel.rect.y), 0);
print("Spawn image at position: " + spawnPosition);
GameObject spwanObj = Instantiate(imageToSpawn, spawnPosition, Quaternion.identity, panel);
yield return new WaitForSeconds(waitTime);
}
}
Vector3 GetBottomLeftCorner(RectTransform rt)
{
Vector3[] v = new Vector3[4];
rt.GetWorldCorners(v);
return v[0];
}
}
Let me know if you need more explanations.
To manage Canvas object's position you should definitely use RectTransform instead of regular transform. In your case you have to do something like :
m_RectTransform = GetComponent<RectTransform>();
As for setting position - coordinates you see on the object inside Canvas is not .position but RectTransform.anchoredPosition, so you should use it in your code. Here is an official documentation on anchoredPosition.
public class ImageSpawnerScreen : MonoBehaviour {
public float waitTime = 2;
public float cubeSpawnTotal = 10;
public List<GameObject> cubePrefabList;
public GameObject panel;
void Start()
{
StartCoroutine(SpawnCube());
}
IEnumerator SpawnCube()
{
for (int i = 0; i < cubeSpawnTotal; i++)
{
GameObject prefabToSpawn = cubePrefabList[Random.Range(0, cubePrefabList.Count - 1)];
//Vector3 spawnPosition = Camera.main.ScreenToViewportPoint(new Vector3(Random.Range(0,Screen.width),0,Random.Range(0,Screen.height))); //Random.Range(xPosMinLimit, xPosMaxLimit);
float xPos = Random.Range(0, Screen.width);
float yPos = Random.Range(0, Screen.height);
Vector3 spawnPosition = new Vector3(xPos, yPos, 0f);
GameObject spwanObj = Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity) as GameObject;
spwanObj.transform.parent = panel.transform;
spwanObj.transform.position = spawnPosition;
yield return new WaitForSeconds(waitTime);
}
}
}
This should help u out.

How to spawn enemy at random intervals?

I am using EnemyScript to move the enemy towards the player and killing the player, but I'm unable to spawn it randomly in code. I am currently spawning it directly through screen by placing the prefab on the scene.
Here is my EnemyScript
using UnityEngine;
using System.Collections;
public class EnemyScript : MonoBehaviour {
public Transform target;
public float speed = 2f;
void Update ()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D otherCollider)
{
PlayerControl shot = otherCollider.gameObject.GetComponent<PlayerControl>();
if (shot != null)
{
Destroy(shot.gameObject);
}
}
}
you could use something similar to this:
public GameObject myObj;
void Start ()
{
enemy = GameObject.Find("enemy");
InvokeRepeating("SpawnEnemy", 1.6F, 1F);
}
public void SpawnEnemy()
{
Vector3 position = new Vector3(Random.Range(35.0F, 40.0F), Random.Range(-4F, 2F), 0);
Instantiate(myObj, position, Quaternion.identity);
}
in the InvokeRepeating call you could possibly add the random range there also instead of a timed instantiate. This example is just a snippet of some prototype code i did a while ago, it may not suit your needs directly but hopefully will give you a general idea on what to do.
EDIT: to make sense, put this into a blank object somewhere in your scene, dont attach this to the actual enemy.
i programmed it like this.
public GameObject enemyPrefab;
public float numEnemies;
public float xMin = 20F;
public float xMax = 85F;
public float yMin = 3.5F;
public float yMax = -4.5F;
void Start () {
for (int i=0; i< numEnemies; i++) {
Vector3 newPos = new Vector3(Random.Range(xMin,xMax),Random.Range(yMin,yMax),0);
GameObject enemy = Instantiate(enemyPrefab,newPos,Quaternion.identity) as GameObject;
}
}
This will spawn a random number of enemies, at random locations, after random periods of time, all adjustable in the Inspector.
public float minTime = 1;
public float maxTime = 3;
public int minSpawn = 1;
public int maxSpawn = 4;
public Bounds spawnArea; //set these bounds in the inspector
public GameObject enemyPrefab;
private spawnTimer = 0;
Vector3 randomWithinBounds(Bounds r) {
return new Vector3(
Random.Range(r.min.x, r.max.x),
Random.Range(r.min.y, r.max.y),
Random.Range(r.min.z, r.max.z));
}
void Update() {
spawnTimer -= Time.deltaTime;
if(spawnTimer <= 0) {
spawnTimer += Random.Range(minTime, maxTime);
int randomSpawnCount = Random.Range(minSpawn, maxSpawn);
for(int i = 0; i < randomSpawnCount; i++) {
Instantiate(transform.transformPoint(enemyPrefab), randomWithinBounds(spawnArea), Quaternion.identity);
}
}
}
//bonus: this will show you the spawn area in the editor
void OnDrawGizmos() {
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.color = Color.yellow;
Gizmos.drawWireCube(spawnArea.center, spawnArea.size);
}
People on here seem to like using coroutines for time delays, but I personally prefer to track my own timers in Update() to maximize control and predictability.

Categories

Resources