hey guys, I am making a wave system for an array of blocks; for example, If one block is at 2, and the origin point is 0, it will calculate how many blocks it will take to slowly decline to the origin point, sort of like stairs.
Here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class MobMentality : MonoBehaviour {
public float moveIncrement = .2f;
public float MoveSize = 1f;
[Space]
private float offset;
private Grid highlightedGrid;
private Vector3 startPos = new Vector3 (1, 0, 1);
public GameObject cubePrefab;
[Space]
public float size;
public float spacing;
[Serializable]
public sealed class Grid{
public GameObject Object;
public int column;
public int height;
}
[Space]
[HideInInspector]
public List<Grid> Grids = new List<Grid>();
public int width;
public int height;
void Start(){
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
createCube (i, j);
}
}
}
void createCube(int width, int height){
Vector3 pos;
offset = size + spacing;
GameObject Cube = Instantiate (cubePrefab, new Vector3 (0, 0, 0), Quaternion.identity);
float x = offset * width;
float z = offset * height;
pos = new Vector3(x,0,z) + startPos;
Cube.transform.position = pos;
Cube.GetComponent<CubeUpdate> ().number = Grids.Count + 1;
Cube.GetComponent<CubeUpdate> ().GameManager = this.gameObject;
Grid _CubeGrid = new Grid ();
_CubeGrid.Object = Cube;
_CubeGrid.height = height;
_CubeGrid.column = width;
Grids.Add (_CubeGrid);
}
public void MovementUpdate(GameObject _object){
if (_object.transform.position.y > 0f)
getGrid (_object.transform.position.x, _object.transform.position.z);
}
// void Update(){
// foreach (Grid _grid in Grids) {
// if (_grid.Object.transform.position.y != 0f) {
// getGrid (_grid.Object.transform.position.x, _grid.Object.transform.position.z);
// }
// }
// }
void getGrid(float posZ, float posX){
int column = (int)((posX / offset) + .1f);
int _height = (int)((posZ / offset) + .1f);
int number = column + (width * (_height-1));
highlightedGrid = Grids [number - 1];
moveNeighbour (highlightedGrid, number);
Debug.Log (number + "");
}
void moveNeighbour(Grid _grid, int blockNumber){
GameObject gridObject = _grid.Object;
float blocksToAffect = (gridObject.transform.position.y / moveIncrement);
//MoveAll Left
for (int i = 1; i < blocksToAffect; i++) {
Grids [blockNumber - i].Object.transform.position = new Vector3(Grids[blockNumber - i].Object.transform.position.x,(_grid.Object.transform.position.y + 1 - (i * moveIncrement)),Grids[blockNumber - i].Object.transform.position.z);
Debug.Log ("Move Neighbour: " + (blockNumber - i) + " From Origin Point: " + blockNumber + " To Point: " + Grids[blockNumber - i].Object.transform.position.y);
Debug.Log ("Origin Point: " + blockNumber);
Debug.Log ("Blocks to affect: " + blocksToAffect);
}
}
}
Focus is on moveNeighbour. So in the function, blocks to affect is the calculation that determines how many blocks need to be affected, if the block is at 2, it should be 15 since the move increment is 0.2; but no matter how high it is, it only shows 5.
I did the math and it should work no matter what. E.G 5 / .2 = 25; 2 / .2 = 10
and also; in movementUpdate, it only runs when the block is > 2. I want it to include 1 aswell. with the for loop, it also affects the middle block which i call origin point. which I tried to fix by starting i at 1 but it still doens’t work.
Thanks in advance!
–Edit
For reference, as you can see the origin point is the block that says (2.8); this should be 3 but the loop affects it for some reason, and it only goes down to 1.6, when it should have (1.4, 1.2, 1, .8 etc)
