Random Position Generation - c#

I'm very new at coding (like two weeks), so forgive me for the silly question. But I'm trying to code a block sliding 3d game in unity, where the obstacles randomly generate, forever, the random generation is fine, but for my life, i cannot figure out how to get the obstacles to spawn at random Z positions. i"m currently stuck with a CS0117 error, so i cant test my latest attempt to fix this.
here's my current code:
using UnityEngine;
public class BlockSpawner : MonoBehaviour
{
public Transform[] SpawnPoints;
public GameObject blockPrefab;
public float timeBetweenWaves = 1f;
private float timeToSpawn = 2f;
public class Random { }
void Update()
{
if (Time.time >= timeToSpawn)
{
SpawnBlocks();
timeToSpawn = Time.time + timeBetweenWaves;
}
}
void SpawnBlocks()
{
//Spawning blocks in
int randomIndex = Random.Range(0, SpawnPoints.Length);
{
for (int i = 0; i < SpawnPoints.Length; i++)
if (randomIndex != i)
{
// generate random position
var viewRange = this.Size - SpawnPoints.Size;
Random random = new Random();
var left = random.Next(0, viewRange.Width);
var top = random.Next(0, viewRange.Height);
// set the random position
SpawnPoints.Location = new Point(left, top);
}
}
}
And Heres is my last working Code (note this has no random location code attempts):
using UnityEngine;
public class BlockSpawner : MonoBehaviour
{
public Transform[] SpawnPoints;
public GameObject blockPrefab;
public float timeBetweenWaves = 1f;
private float timeToSpawn = 2f;
void Update()
{
if (Time.time >= timeToSpawn)
{
SpawnBlocks();
timeToSpawn = Time.time + timeBetweenWaves;
}
}
void SpawnBlocks()
{
//Spawning blocks in
int randomIndex = Random.Range(0, SpawnPoints.Length);
{
for (int i = 0; i < SpawnPoints.Length; i++)
if (randomIndex != i)
{
Instantiate(blockPrefab, SpawnPoints[i].position, Quaternion.identity);
}
}
}
}
Please help!

I will guess at the error and say this is wrong
var viewRange = this.Size - SpawnPoints.Size;
var left = random.Next(0, viewRange.Width);
var top = random.Next(0, viewRange.Height);
// set the random position
SpawnPoints.Location = new Point(left, top);
Random random = new Random();
you are calling methods on 'random' before you have set it up. YOu need
var viewRange = this.Size - SpawnPoints.Size;
Random random = new Random();
var left = random.Next(0, viewRange.Width);
var top = random.Next(0, viewRange.Height);
// set the random position
SpawnPoints.Location = new Point(left, top);
But I will point out 2 things
unity has given you its random number generator, and you already used it (Random.Range) but for some reason you decide to use the c# standard one
creating a new Random each time round that loop will keep generating the same numbers over and over again if called in rapid succession better is to create the Random once at the start of the function (or use the Unity one)

To Generate Obstacles At Random Z position.
Am I keeping it much Simple!.
To Do this we have to keep these things in mind:
1: The X and Y Coordinates are not going to change.
2: The only Z coordinates are going to change.
Solution:
Generate a new Vector3 that will contains same X and Y coordinates but random Z coordinates.
Code:
// the game object (mean obstacle to generate)
public GameObject ObstaclePrefab;
// this will be the coordinates that will not going to change (X and Y coordinates as they will be same for all obstacles but only the Z coordinates will change.)
public Vector2 ConstantCords = new Vector2(0, 0);
// the max and minimum range for obstacles to be generated (X represents the negative [minimum] while y will represent the positive [maximum] position (Cordinates) )
public Vector2 MaxZCords = new Vector2(-50f, 50f);
// this method will generate the obstacle at random position
public void GenerateRandomObstacle()
{
// getting a random Z coordinates
// Random.Range will give random values
float Z = Random.Range(MaxZCords.x, MaxZCords.Y);
// getting position to generate the obstacle
// putting the constant and the random Z value
Vector3 pos = new Vector3(ConstantCords.x, ConstantCords.y, Z);
// generating the obstacle at the generated random position
// we are setting the rotation to rotation of the prefab
Instantiate(ObstaclePerfab, pos, ObstaclePrefab.transfrom.rotation);
}

Related

Unity: Finding Lowest Vertices on Mesh

I'm a very newbie to using unity and C#. I have a question about spawning an object on the lowest Y coordinate on the mesh.
How to get the lowest Y vertices with other coordinates to spawn an object at that point.
Thank you in advance everyone :)
public Mesh terrain;
public GameObject agents;
void Start()
Mesh terrain = GetComponent<MeshFilter>().mesh;
Vector3[] meshVertices = terrain.vertices;
float minY = float.MinValue;
int count = terrain.vertexCount;
List<Vector3> vertices = new List<Vector3>();
terrain.GetVertices(vertices);
for (int i = 0; i < vertices.Count; i++)
{
Vector3 pos = vertices[i];
minY = Mathf.Min(pos.y,-pos.y);
Vector3 position = transform.TransformPoint(vertices[i]);
if (position.y == minY)
{
Instantiate(agents, position, Quaternion.identity);
}
}
terrain.RecalculateBounds();
If you're looking for a minimum value, then you need to start with something that's so big that anything will be less than that. Something like float.MaxValue. Then you need to go through all the points and compare your current minimum to the running minimum and cache the current point if it's less. Once you're done with all the points then you can use your cached point as the instantiation location. Consider the following:
public Mesh terrain;
public GameObject agents;
void Start()
{
Mesh terrain = GetComponent<MeshFilter>().mesh;
Vector3[] meshVertices = terrain.vertices;
float minY = float.MaxValue; // <--- Change
Vector3 minimumVertex; // <--- Change
int count = terrain.vertexCount;
List<Vector3> vertices = new List<Vector3>();
terrain.GetVertices(vertices);
for (int i = 0; i < vertices.Count; i++)
{
// Vector3 pos = vertices[i]; // <--- Change
// minY = Mathf.Min(pos.y,-pos.y); // <--- Change
Vector3 position = transform.TransformPoint(vertices[i]);
// if (position.y == minY) // <--- Change
if(position.y < minY) // <--- Change
{
minY = position.y; // <--- Change
minimumVertex = position; // <--- Change
//Instantiate(agents, position, Quaternion.identity); // <--- Change
}
}
Instantiate(agents, minimumVertex, Quaternion.identity); // <--- Change
terrain.RecalculateBounds();
}
I was looking for an answer for you that summarizes a large part of the code, all this is possible thanks to system.linq, the following code sorts vertices by y coordinate and puts them in a position list, just enough Set list[0] as position. The other vertices are also arranged in order, which is an advantage.
using System.Linq;
public void Start()
{
var meshFilter = GetComponent<MeshFilter>();
var list = meshFilter.mesh.vertices.Select(transform.TransformPoint).OrderBy(v => v.y).ToList();
Debug.Log(list[0]); // lowest position
Debug.Log(list.Last()); // highest position
}

Instantiate predefined number of object along a raycast in Unity

I have a raycast that's being rendered every frame based on 2 points, and those 2 points change position each frame.
What I need is a system that doesn't need a direction, or a number of objects, but instead takes in 2 points, and then instantiates or destroys as many objects as necessary to get the instantiated objects from one side to another minus spaceBetweenPoints. If you wanted you could think of it as an Angry Birds Style slingshot HUD, except without gravity, and based on raycasts.
My Script:
public int numOfPoints; // The number of points that are generated (This would need to chnage based one the distance in the end)
public float spaceBetweenPoints; // The spacing between the generated points
private GameObject[] predictionPoints; // The prefab to be gernerated
private Vector2 firstPathStart; // The starting point for the raycast (Changes each frame)
private Vector2 firstPathEnd; // The ending point for the raycast (Changes each frame)
void start()
{
predictionPoints = new GameObject[numOfPoints];
for (int i = 0; i < numOfPoints; i++)
{
predictionPoints[i] = Instantiate(predictionPointPrefab, firePoint.position,
Quaternion.identity);
}
}
void Update
{
Debug.DrawLine(firstPathStart, firstPathEnd, UnityEngine.Color.black);
DrawPredictionDisplay();
}
void DrawPredictionDisplay()
{
for (int i = 0; i < numOfPoints; i++)
{
predictionPoints[i].transform.position = predictionPointPosition(i * spaceBetweenPoints);
}
}
Vector2 predictionPointPosition(float time)
{
Vector2 position = (Vector2)firstPathStart + direction.normalized * 10f * time;
return position;
}
The current system simply takes in a starting position, a direction, and then moves a preset number of objects in that direction based on time. This way of doing it also causes problems because it's endess instead of only going till the end of the raycast: (Pardon my drawing skills)
Blue line = raycast
Black dots = instantiated prefab
Orange dot = raycast orange
Green dot = end of raycast
Notes:
direction is the momentum which I set in the editor, I needed it to put together what I currently have working, but it shouldn't be necessary when running based on points.
If you ask me I would say it is kinda easy if you know little bit of Math trickery. I'm not saying that I'm very good at Math, but once you get it it's kind of easy to pull off next time. Here if I try to explain everything, i won't be able to explain clearly. Take a look as the code below I've commented the whole code so that you can understand easily.
Basically I used a Method called Vector2.Lerp() Liner Interpolation, which means that this method will return value between point1, and point2 based on the value of 3rd argument t which goes from 0 to 1.
public class TestScript : MonoBehaviour
{
public Transform StartPoint;
public Transform EndPoint;
public float spaceBetweenPoints;
[Space]
public Vector2 startPosition;
public Vector2 endPosition;
[Space]
public List<Vector3> points;
private float distance;
private void Update()
{
startPosition = StartPoint.position; //Setting Starting point and Ending point.
endPosition = EndPoint.position;
//Finding the distance between point
distance = Vector2.Distance(startPosition, endPosition);
//Generating the points
GeneratePointsObjects();
Debug.DrawLine(StartPoint.position, EndPoint.position, Color.black);
}
private void OnDrawGizmos()
{
//Drawing the Dummy Gizmo Sphere to see the points
Gizmos.color = Color.black;
foreach (Vector3 p in points)
{
Gizmos.DrawSphere(p, spaceBetweenPoints / 2);
}
}
private void OnValidate()
{
//Validating that the space between two objects is not 0 because that would be Raise an exception "Devide by Zero"
if (spaceBetweenPoints <= 0)
{
spaceBetweenPoints = 0.01f;
}
}
private void GeneratePointsObjects()
{
//Vlearing the list so that we don't iterate over old points
points.Clear();
float numbersOfPoints = distance / spaceBetweenPoints; //Finding numbers of objects to be spawned by dividing "distance" by "spaceBetweenPoints"
float increnment = 1 / numbersOfPoints; //finding the increment for Lerp this will always be between 0 to 1, because Lerp() takes value between 0 to 1;
for (int i = 1; i < numbersOfPoints; i ++)
{
Vector3 v = Vector2.Lerp(startPosition, endPosition, increnment * i); //Find next position using Vector2.Lerp()
points.Add(v);//Add the newlly found position in List so that we can spwan the Object at that position.
}
}
}
Update: Added, How to set prefab on the positions
I just simply Destroyed old objects and Instantiated new Objects. But remember instantiating and Destroying object frequently in your game in unity will eat-up memory on your player's machine. Os I would suggest you to use Object-Pooling. For the reference I'll add a link to tutorial.
private void Update()
{
startPosition = StartPoint.position; //Setting Starting point and Ending point.
endPosition = EndPoint.position;
//Finding the distance between point
distance = Vector2.Distance(startPosition, endPosition);
//Generating the points
GeneratePointsObjects();
//Update: Generating points/dots on all to location;
InstenciatePrefabsOnPositions();
Debug.DrawLine(StartPoint.position, EndPoint.position, Color.black);
}
private void InstenciatePrefabsOnPositions()
{
//Remove all old prefabs/objects/points
for (int i = 0; i < pointParent.childCount; i++)
{
Destroy(pointParent.GetChild(i).gameObject);
}
//Instantiate new Object on the positions calculated in GeneratePointsObjects()
foreach (Vector3 v in points)
{
Transform t = Instantiate(pointPrefab);
t.SetParent(pointParent);
t.localScale = Vector3.one;
t.position = v;
t.gameObject.SetActive(true);
}
}
Hope this helps please see below links for more reference
OBJECT POOLING in Unity
Vector2.Lerp
I hope I understood you right.
First, compute the A to B line, so B minus A.
To get the number of needed objects, divide the line magnitude by the objects' spacing. You could also add the diameter of the prediction point object to avoid overlapping.
Then to get each object position, write (almost) the same for loop.
Here's what I came up with, didn't tested it, let me know if it helps!
public class CustomLineRenderer : MonoBehaviour
{
public float SpaceBetweenPoints;
public GameObject PredictionPointPrefab;
// remove transforms if you need to
public Transform start;
public Transform end;
private List<GameObject> _predictionPoints;
// these are your raycast start & end point, make them public or whatever
private Vector2 _firstPathStart;
private Vector2 _firstPathEnd;
private void Awake()
{
_firstPathStart = start.position;
_firstPathEnd = end.position;
_predictionPoints = new List<GameObject>();
}
private void Update()
{
_firstPathStart = start.position;
_firstPathEnd = end.position;
// using any small value to clamp everything and avoid division by zero
if (SpaceBetweenPoints < 0.001f) SpaceBetweenPoints = 0.001f;
var line = _firstPathEnd - _firstPathStart;
var objectsNumber = Mathf.FloorToInt(line.magnitude / SpaceBetweenPoints);
var direction = line.normalized;
// Update the collection so that the line isn't too short
for (var i = _predictionPoints.Count; i <= objectsNumber; ++i)
_predictionPoints.Add(Instantiate(PredictionPointPrefab));
for (var i = 0; i < objectsNumber; ++i)
{
_predictionPoints[i].SetActive(true);
_predictionPoints[i].transform.position = _firstPathStart + direction * (SpaceBetweenPoints * i);
}
// You could destroy objects, but it's better to add them to the pool since you'll use them quite often
for (var i = objectsNumber; i < _predictionPoints.Count; ++i)
_predictionPoints[i].SetActive(false);
}
}

Memory overload on for loop

I am making an object spawn script in which on the start of the script the spawning function is called and in it is a for loop which creates an object each iteration. It first picks a random X position for it and then checks if it is in a range of another prefabs coordinates so they don't spawn too close or worse, one in each other. If it is in the same coordinates as another prefab it will return 0 and this same goes out for the Z axis too. It also picks a random Y axis rotation so it doesn't all face the same direction. After this it spawns the prefab and sets it's coordinates and rotation after which it check if the coordinates in the X or Z axis are 0, and if any of those two are 0 it goes back one iteration and the last object to be spawned is destroyed so it doesn't flood. This works perfectly but when you want to set it to spawn too much objects it floods the RAM because there is nowhere to spawn more objects. I tried finding the highest X position and highest Z position and multiplying them, and setting them both to positive, and then dividing them by the space between the prefabs but this doesn't work as it sets it to a really really high number. How would you fix this?
Script:
using UnityEngine;
using System.Collections;
public class PrefabSpawner : MonoBehaviour {
public int amountOfPrefabs;
public int maxAmountOfPrefabs;
private int currentSpawnedPrefab;
public float spaceBetweenPrefabs;
private float positionX;
private float positionZ;
private float maxPositionX;
private float maxPositionZ;
private float multipliedPosXZ;
private bool previousSpawnHadZero;
public GameObject prefab;
private GameObject point1;
private GameObject point2;
private GameObject currentSpawn;
private Vector2[] positions;
void Start () {
currentSpawnedPrefab = 0;
previousSpawnHadZero = false;
point1 = gameObject.transform.GetChild (0).gameObject;
point2 = gameObject.transform.GetChild (1).gameObject;
if (point1.transform.position.x > point2.transform.position.x)
maxPositionX = point1.transform.position.x;
else
maxPositionX = point2.transform.position.x;
if (point1.transform.position.z > point2.transform.position.z)
maxPositionZ = point1.transform.position.z;
else
maxPositionZ = point2.transform.position.z;
multipliedPosXZ = maxPositionX * maxPositionZ;
if (multipliedPosXZ < 0)
multipliedPosXZ += multipliedPosXZ + multipliedPosXZ;
maxAmountOfPrefabs = Mathf.FloorToInt (multipliedPosXZ / spaceBetweenPrefabs);
if (amountOfPrefabs > maxAmountOfPrefabs)
amountOfPrefabs = maxAmountOfPrefabs;
point1.GetComponent<MeshRenderer> ().enabled = false;
point2.GetComponent<MeshRenderer> ().enabled = false;
gameObject.GetComponent<MeshRenderer> ().enabled = false;
positions = new Vector2[amountOfPrefabs];
SpawnPrefabs (amountOfPrefabs);
}
void SpawnPrefabs (int amount) {
for (int i = 0; i < amount; i++) {
if(previousSpawnHadZero)
i -= 1;
currentSpawn = (GameObject)Instantiate (prefab);
positionX = GetRandomPositionX ();
positionZ = GetRandomPositionZ ();
currentSpawn.transform.position = new Vector3 (positionX, this.transform.position.y + currentSpawn.transform.localScale.y, positionZ);
currentSpawnedPrefab += 1;
if (positionX == 0 || positionZ == 0) {
previousSpawnHadZero = true;
currentSpawnedPrefab -= 1;
Destroy (currentSpawn);
}
if (positionX != 0 && positionZ != 0) {
previousSpawnHadZero = false;
positionX = 0;
positionZ = 0;
}
}
}
IEnumerator Pause () {
yield return null;
}
float GetRandomPositionX () {
//Finds a random position for the X axis and then checks it and returns either 0 if the position is taken or the position if not
}
float GetRandomPositionZ () {
//Finds a random position for the Z axis and then checks it and returns either 0 if the position is taken or the position if not
}
bool CheckPositionAvailable (float pos, int axis) {
//Checks if the position is available.
}
}
Code is really long to debug but the problem is clearly visible and is from the SpawnPrefabs function. Currently, when you instantiate a prefab, you check if the generated position is 0. If 0, you subtract 1 from the i in the for loop then destroy the instantiated object and then start the for loop again from the current loop-1.
So the combination of Instantiate, Destroy and repeating it over again in the for loop is causing the memory issue.
What to do:
You have to re-write the whole function and this will require modification in your whole code too. Do not instantiate and destroy object in that loop unless when needed.
1.In the Start() function, create one prefab.
2.Make it to be invisible in the scene by disabling its mesh/sprite renderer.
3.Use that prefab in the for loop to check if the generated position is valid. If it is valid, you can now create/instantiate an object in the loop.
This prevents instantiating and destroying objects in the loop when you only create objects when if (positionX != 0 && positionZ != 0) .

How can I change position of instantiate objects (clones)?

I created a series of sphere clones in my game. After that I adapted the scale so that they appear smaller. However, now there is a gap between these spheres ... and I would have to change the position of this instatiate game objects. I changed my code already exactly at this position but nothing happens. So please I need your help! How can I do this? I would have very small spheres which are located near together.
Here the code:
using UnityEngine;
using System.Collections;
public class SineWave : MonoBehaviour {
private GameObject plotPointObject;
private int numberOfPoints= 100;
private float animSpeed =1.0f;
private float scaleInputRange = 8*Mathf.PI; // scale number from [0 to 99] to [0 to 2Pi] //Zahl vor Mathf, Anzahl Bön
private float scaleResult = 2.5f; // Y Achse Range
public bool animate = true;
GameObject[] plotPoints;
// Use this for initialization
void Start () {
if (plotPointObject == null) //if user did not fill in a game object to use for the plot points
plotPointObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); //create a sphere
//add Material to the spheres , load material in the folder Resources/Materials
Material myMaterial = Resources.Load("Materials/green", typeof(Material)) as Material;
plotPointObject.GetComponent<MeshRenderer> ().material = myMaterial;
//change the scale of the spheres
//plotPointObject.transform.localScale = Vector3.one * 0.5f ;
plotPointObject.transform.localScale -= new Vector3(0.5f,0.5f,0.5f);
plotPoints = new GameObject[numberOfPoints]; //creat an array of 100 points.
//plotPointObject.GetComponent<MeshRenderer> ().material =Material.Load("blue") as Material
//plotPointObject.transform.localScale -= new Vector3 (0.5F, 0.5F, 0.5F); //neu: change the scale of the spheres
for (int i = 0; i < numberOfPoints; i++)
{
plotPoints[i] = (GameObject)GameObject.Instantiate(plotPointObject, new Vector3(i -
(numberOfPoints/2), 0, 0), Quaternion.identity); //this specifies
what object to create, where to place it and how to orient it
}
//we now have an array of 100 points- your should see them in the hierarchy when you hit play
plotPointObject.SetActive(false); //hide the original
}
Thank you already in advance!
Edit:
As I said in the comment I achieved now to place my spheres without a gap in between. However, as soon as I animate my spheres (with a sine wave) there is still that gap between the spheres. How can I adapt this? Should I copy the code of the Start function in the Update function?
I would be very happy to get some help. Thank you very much!
enter code here void Update()
{
for (int i = 0; i < numberOfPoints; i++)
{
float functionXvalue = i * scaleInputRange / numberOfPoints; // scale number from [0 to 99] to [0 to 2Pi]
if (animate)
{
functionXvalue += Time.time * animSpeed;
}
plotPoints[i].transform.position = new Vector3(i - (numberOfPoints/2), ComputeFunction(functionXvalue) * scaleResult, 0);
//print (plotPointObject.GetComponent<MeshRenderer> ().bounds.size.x);
// put the position information of sphere clone 50 in a vector3 named posSphere
posSphere = plotPoints [50].transform.position;
}
//print position of sphere 50 in console
//print (posSphere);
}
float ComputeFunction(float x)
{
return Mathf.Sin(x);
}
}
I think you could make the Barış solution.
For each new object that you are instantiating, you will set his position to the lasted instantiated position adding the size of the object itself, or whatever distance that you want they have from each other.
var initialPosition = 0;
var distanceFromEachOther = 20;
for (int i = 0; i < numberOfPoints; i++) {
var newPos = new Vector3(initialPosition + (i * distanceFromEachOther), 0, 0);
plotPoints[i] = (GameObject)GameObject.Instantiate(plotPointObject, newPos, Quaternion.identity);
}
That will make a gap between the spheres at X pivot, depending on their size. Change the distanceFromEachOther var, adjusting for your needs.
You could also get the object distance with plotPointObject.GetComponent<MeshRenderer>().bounds.size, so distanceFromEachOther could be, for example distanceFromEachOther = plotPointObject.GetComponent<MeshRenderer>().bounds.size.x + 5. So then you will have the objects with a perfectly distance of 5 from each other.
give this a try:
Transform objectToSpawn;
for (int i = 0; i < numberOfPoints; i++)
{
float someX = 200;
float someY = 200;
Transform t = Instantiate(objectToSpawn, new Vector3(i -(numberOfPoints/2), 0, 0), Quaternion.identity) as Transform;
plotPoints[i] = t.gameObject;
t.position = new Vector(someX, someY);
}

XNA: Need help spawning enemies on any side, on a timer tick, then maving them move randomly

Ok so I am making a 2d shooting game in XNA and have hit a bit of a snag. I am trying to make an enemy class that will allow me to spawn an enemy just off screen every 10 seconds, then have it move randomly in either up, right left or down. After spending the better part of 4 hours scouring the internet all I have found is examples making the enemy move one direction, spawn from one point and so on.
Any help would be much appreciated, my enemy class is a bit of a mess at the minute from dragging together so much code to try and make it work. Here it is as it stands:
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Shark_Wars_2
{
class Enemy
{
List<Vector2> sharkPosition = new List<Vector2>(); //is there a better way for having more than one of the same enemy?
public Vector2 sharkDirection;
public int sharkWidth;
public int sharkHeight;
public int sharkSpeed;
float sharkSpawnProbability;
public Random randomSharkPosition;
public Random direction;
double timer = 0;
float interval = 1000;
int speed = 1;
public Enemy()
{
sharkPosition = new Vector2(0, 0); //brings up error - see above
sharkWidth = 64;
sharkHeight = 44;
sharkSpeed = 3;
sharkSpawnProbability = 1 / 10000f;
randomSharkPosition = new Random(3); //left, right, up, down
direction = new Random(3);
}
public void update(GameTime gameTime)
{
//enemy movement
for (int i = 0; i < sharkPosition.Count; i++)
{
//enemy position increases by speed
sharkPosition[i] = new Vector2(sharkPosition[i].X + sharkDirection.X, sharkPosition[i].Y + sharkDirection.Y);
}
timer += gameTime.ElapsedGameTime.TotalMilliseconds;
//aniamtion
if (timer >= interval / speed)
{
timer = 0;
}
//spawn new enemy
if (randomSharkPosition.NextDouble() < sharkSpawnProbability)
{
float spawn = (float)randomSharkPosition.NextDouble() * (1280 - sharkHeight); sharkPosition.Add(new Vector2(sharkDirection.X, sharkDirection.Y));
}
}
// ill worry about drawing after spawning and movement, should be no problem
public void draw()//(SpriteBatch spritebatch, Texture2D wave)
{
for (int i = 0; i < sharkPosition.Count; i++)
{
//spritebatch.Draw(shark, new Rectangle((int)sharkPosition[i].X, (int)sharkPosition[i].Y, sharkWidth, sharkHeight);
}
}
}
}
my head is really blocked, I spent a lot of time working out how to make moving background objects and a list of bullets.
I would really like to simplify this down too so if you know of an easier way of doing things I am all ears!
Thanks in advance!
Your basic problem is that you haven't separated your managment/logic class from your instance objects. You want more separation, like this:
public class EnemyManager
{
List<Shark> activeSharks = new List<Shark>();
Timer spawnTimer;
//Just one random instance per class, this is considered best practice to improve
//the randomness of the generated numbers
Random rng = new Random(); //Don't use a specific seed, that reduces randomness!
public EnemyManager()
{
//Min/max time for the next enemy to appear, in milliseconds
spawnTimer = new Timer(rng.Next(500, 2000));
spawnTimer.Elapsed += SpawnShark;
spawnTimer.Start();
}
public void SpawnShark(object sender, ElapasedEventArgs e)
{
Vector2 newSharkPosition = Vector2.Zero;
newSharkPosition.X = rng.Next(0, 1280); //Whatever parameters you want
newSharkPosition.Y = rng.Next(0, 1280); //Whatever parameters you want
Vector2 newSharkDirection.... //Repeat above for direction
Shark spawnedShark = new Shark(newSharkPosition, newSharkDirection);
activeSharks.Add(spawnedShark);
//Min/max time for the next enemy to appear, in milliseconds
spawnTimer = new Timer(rng.Next(500, 2000));
}
public void update(GameTime gameTime)
{
foreach(Shark s in activeSharks)
s.Update(gameTime);
}
public void draw(SpriteBatch spritebatch, Texture2D wave)
{
foreach(Shark s in activeSharks)
s.Draw(spritebatch, wave)
}
}
public class Shark
{
Vector2 position;
Vector2 direction;
const float speed = 3;
const int height = 44;
const int width = 64;
public Shark(Vector3 initPosition, Vector3 direction)
{
position = initPosition;
this.direction = direction;
}
//Draw/Update using the local variables.
}
Obviously I didn't write all the code, but that should get you started. First I took all the "instance" specific data and put it into its own object, then the "manager" just holds and manages a collection of these objects. I also refactored the RNG to only have one instance (best practice) and to use the default time-based seed. Seeding it yourself will cause the numbers to come out the same each time you run the program (probably not what you were going for :) ).
Let me know if you don't understand something or I can provide any further assistance!

Categories

Resources