Create random falling objects in unity C# - c#

i am starting to learn unity3d and am developing a 2d game but need some help at the main menu. I have a coin texture which i would like to spawn multiple times above the screen and fall down to create a falling coin texture. Spawn points should be random and entities should be destroyed after falling off the screen, but i have no idea how to do it. Any help will be greatly appreciated. Thank you
Image Upload: http://imgur.com/ol4vkr2

I will give you some starting points to done your task. You can read about`
RigidBody(RigidBody2D)
Collider(Collider2D)
MonoBehaviour Callbacks for example OnTriggerEnter(OnTriggerEnter2D) or OnCollisionEnter(OnCollisionEnter2D)
For randomness you can read about Random class and his methods like Random.randomInsideUnitCircle.

If I were you I'd start by reading up on Unity's particle system. By setting up some parameters in the editor it can exactly do what you want and very efficiently as well.
Some important parameters to consider:
Emitter shape: You want this to be a rectangle in your case, positioned at the top of the scene.
Initial velocity: You want this to be something like (0,-2,0) for downwards falling coins.
Emission rate: Determines how fast new coins will be produced.
Particle shape: Either a coin texture or a coin mesh or whatever you want.
Starting lifetime: The duration of each coin. The coin will be properly destroyed after this time.
The particle system will automatically choose random points within the emitter shape.

Related

How to change a particle system's rotation without changing the direction in which emitted particles are heading?

Alright, so I have a 2d rocket with a particle system shooting squares and triangles as thrust. The issue I am having is that, the particles that are emitted rotate with the rocket when the rocket and the particle system turn. The particle system is a child to the rocket. So if the rocket is going up, the particles emit downwards, but say I turn the rocket to be going down, those already emitted particles change their position to be above the rocket and start going up. How can I fix this?
Thank you!
One way to go about it is just to change simulation space to world.
With mode set to local, any changes to your parent game object, or the particle system itself, propagates down to all the living particles.
With world, new particles will match the position and rotation of your rocket, but the ones already emitted will retain their direction, as local changes to transform of ps/any other parent, will no longer affect them.

Unity. "Stretching" of an object instantiated from prefab

I am trying to spawn obstacles on the road. To do so, I generate a road by spawning its parts, and each part itself spawns several obstacles in bounds of themselves. But some prefabs after being instantiated got strange "stretching" effects. I don't know how to explain it, so I recorded a small video link. Also, if I spawn that same object by "drag n drop" to the scene, this bug never appears.
This how I spawn obstacles:
Vector3 size = GetComponent<Renderer>().bounds.size;
Vector3 pos = new Vector3(Random.Range(-0.3f*size.x,0.3f*size.x), 30, Random.Range(-0.3f*size.z,0.3f*size.z));
Debug.Log(pos + transform.position +"");
GameObject newBottle = Instantiate(minus_prefabs[Random.Range(0, minus_prefabs.Length)], new Vector3(transform.position.x+pos.x,transform.position.y + pos.y,transform.position.z+pos.z), Quaternion.Euler(0, 0, 0));
newBottle.transform.SetParent(transform);
This happens because the parent GameObject of the model has diffrent sizes on X, Y and Z. This results in some weird stretching. Try going in your prefab and set the scale of your main GameObject to X:1 Y:1 Z:1 for example.
Also see: https://www.unity3dtips.com/how-to-fix-objects-stretching-when-rotated/#:~:text=Cause%20of%20the%20object%20stretching,0.01%2C%200.002%2C%200.004%E2%80%9D.
This also happens to me all the Time. Thankfully its easy to fix!
EDIT: When you instantiate the prefab, also make sure it has the same sizes on every axis.
As a general rule, whenever possible, don't use any Scale other than 1,1,1 unless you need to and it should be on a leaf node of the hierarchy or prefab not a parent node. It will make things go much smoother. If you need to change the size of a mesh (because the noob modeler didn't know how to follow the life-sized scaling metrics in their modeling program, or you just want it smaller or larger), you can do that in the Import settings on the FBX.

Instantiate prefabs on endless runner top of previous one

I am developing 2D Endless racer game, insted of constant background; i will use prefabs as background, i have 3 different prefabs as background: (Please Check Image)
Straight Road
Right Curved Road
Left Curved Road
I want to instantiate endless prefabs, according to instantiate point of each prefab,
I would appreciate for your support
Goal Image and Prefabs
One simple solution come straight from the example image you posted. Each road piece has its own "exit point", symbolized by that yellow circle. Since each road piece is its own prefab, you could give each of them an empty child object, acting like your yellow circle.
Each object would then have a reference to that object, and would use that to spawn the next object.
public class RoadPiece : Monobehaviour
{
public Transform nextInstantiatePoint;
To make the next road piece line up correctly, you could take advantage of the Sprite's Import Settings. Set the Pivot to "Bottom", or try the others for different effects.
The algorithm that you use to chose which piece to instantiate each time is up to you, but to start you could have an array of all the road pieces and pick one from there.
Ideally you want each object to wait some time before it instantiates the next, otherwise it will quickly cause the scene to be overcrowded. The easiest way to do it is like this:
void Start()
{
Invoke(nameof(SpawnNext), waitTime);
}
void SpawnNext()
{
Instantiate(roadPiece, nextInstantiatePoint.position, nextInstantiatePoint.rotation);
}
}

Monogame - creating a player

after some fine-tuning on my game's GUI I am finally ready to begin with the gameplay. But that's a tall order. My game will be something like a 2D platformer with RPG elements like collecting armor, helmets, weapons etc. So with that in mind I begun thinking about a way to create my player. First, I thought a single Player class would do the job for me but since I want to equip the armor/helmet I've acquired, I quickly abandoned this concept.
Next I got another idea. I could have the player's Head, Arms and Legs to be different classes and each of them drawing its own texture, respectively. So I can swap between different armor/helmet sprites for each of the body parts. But that would seem pretty complex to implement... or not?
Could I have a code example on how you would do this? Which path you would take if you are in my stead? Single Player class or different body parts classes? If the latter, how would you hook them so that it all looks like a single sprite?
Armor/helmet should be part of the character status as it could be Health points. If you get hit your HP goes down and if your character collides with a helmet item then you should flag that status into your player object and render your character accordingly.
You may want to use the game component pattern (link is a very good read).
You probably want the player class still be the main base of the character, and have armor/clothing components that draw on top of the character.
The player class can then "have" the components and draw accordingly, add HP or other logic you like etc. A component could "break" and disappear etc.

Particle system without OpenGL and other libraries

I have to make a program on particle systems in C# and I'm using Visual studio 2010. I must not use any libraries like OpenGl, ... I can use just Graphisc library from C#. I tried to read some tutorials and lectures but I still don't know how to implement it.
Can anybody help me understand how I should decompose my problem? Or direct me maybe on some useful tutorials? It´s new for me and I'm kinda stuck.
My task:
Program a simple generator luminous particles in the shape of a geyser from a point generator, view the particles as different colored dots moving across a single track on a black background in parallel projection.
Look up BackgroundWorker and RenderTargetBitmap it is probably best to do this in WPF
Psuedo code
backgroundWorker()
{
while(isRunning)
{
update()
draw()
}
}
update()
{
for each all particles
{
update gravity and/or relativity to other particles
}
}
draw()
{
clear bitmap
for each all particles
{
draw your particle
}
set it to your container
}
This is based upon a game loop
Though not a complete answer, here is how I would break down the problem if it were my task:
Find a way to render a single particle (perhaps as a dot or a ball). I recommend starting with WPF.
Find a way to "move" that particle programmatically (i.e. by rendering it in different places over time).
Create a Particle class that can store its current state (position, velocity, acceleration) and can be moved (i.e. SetPosition, SetVelocity...)
Create a World class the represents a 3-dimensional space and keeps track of all the particles in it. The World will know the laws of physics and will be responsible for moving particles around given their current state.
Create a bunch of Particles that have the same initial starting position but random initial acceleration vectors. Add those Particles to the World. The World will automatically update their positions from then on.
Apply what you learned in steps 1 and 2 to have your World graphically represent all of its Particles.
Edit: Step 7 would be to "run step 6 in a loop". The loop code provided by noHDD is a very good basic framework: continuously update the state of the world and then draw the results.

Categories

Resources