Thursday, February 23, 2017

Day 14

Today I continued the tutorial.

I made an asteroid object that has a random rotation, has a continuous forward movement and spawns somewhere randomly on the top of the screen.

I started off by importing the object from the asset pack and placing it about 8 units across the Z axis. I then made an empty game object, named it "Astroid" and put the model inside the game object.

Now what I had to do was program a sort of rotation to the asteroid so it gives it a realistic effect.
So I made a new script and named it "RandomRotator" and opened it up in Visual Studio.

What I did program the rotation was I made the Rigidbody's angular velocity = to a random point inside a sphere with a radius a 1. This code was rb.angularVelocity = random.insideUnitSphere but that makes the rotation too slow. So I made a public variable called "tumble" set it 5 and multiplied the rotation code by tumble.

This is how it looks so far.

Now I had to add a constant forward movement as soon as it spawns so to do that I made a public variable called speed. I then made the Rigidbody's velocity equal to transform.forward which adds a forward movement. I then multiplied that by speed so it's not too slow. 

Now when I place the object in the game it has a forward movement.

Now the last thing I had to do was add a game object that randomly places the asteroid randomly randomly on the top of the screen.
This code was a bit harder to do but i'll try to explain it as simple as possible. What I did was I made a public Vector3 variable named spawnValues. I then made a method called SpawnWaves() and inside it I typed the code. I made a Vector3 called spawnPosition and made it equal to a new Vector3 with the values of a Random range of spawnValues.x and -spawnValues.x, spawnValues.y and spawnValues.z. I then made it have no spawn rotation and aligned with the world axis by using the code Quaternion spawnRotation = Quaternion.Identity. And lastly I had to actually Instantiate it, so I made a public gameObject variable and named it hazard and then I used the code Instantiate(hazard, spawnPosition, spawnRotation). Now I gave the spawnValues some values to make it randomly appear on the top of the screen and it works :D

Tomorrow I will be continuing the tutorial.

No comments:

Post a Comment