Experimenting with code

Today in class we were tasked with two tasks. The main purpose of this task was to get us to experiment with code to see what works and to have a further understanding of it. The first was to take a cube or any other object we desired and to make it interact in some way with the environment.

update.PNG

I started off with a simple rotate function that makes it so that the object rotates 60 degrees in the Y axis every second. I used this as a basis and just let my ideas snowball from there.

Variable for rotate speed.PNG

I then created a variable which holds the rotate speed so that I could control this within unity rather than having to go into the code to change it.update2.PNG

I then replaced the number in the update function with this variable to make it completely editable.

I then decided that I wanted to work with sliders which took some looking into as I haven’t really used a slider before so I needed to do some research before I could really use one.

using.PNG

The first thing I had to do was tell the code that I was using unities inbuilt UI manager since this slider would be something found on the UI rather than an object in the world.

variables2.PNG

I then created a new variable which would Identify the slider that I had to pick out within unity itself for this code to work. I also removed the basic value for the rotate speed as that will no longer be static

update3.PNG

I then told the code that the sliders value would be equal to the rotate speed and for this I had to specify that I was pulling out the value part of the slider otherwise it would give me an error as it didn’t know what part of the slider it wanted to use.

variables3.PNG

Lastly I wanted to make it so that the player could see just how fast the object was rotating so I decided to add a text box with this information in. To start off I had to identify the text box just like I had to with the slider using a variable.Update4.PNG

This is the final  update function for this script and it simply makes it so that the text will display what I want it to.

Spin.gif

The next thing we had to do was to create ball that starts off by bouncing and add to it to make it our own. The idea I had for mine was that it would get faster as it hits other objects and it would make more versions of itself every few seconds.

Variables.PNG

I started off by defining some variables for it. The first one of this variables just lets me identify the rigidbody of the object so that it can move using the code. The second one is a float that I use for power throughout the code. I also identify the game object and a spawn time that can be changed which helps it to create more versions of itself.

start function.PNG

In the start function I have it identify the rigidbody attached to itself so it always has the right one for all of the movement it does.

I then used a piece of code that was completely new to me. I used the invoke repeating command which creates a function that is called a specified amount of time after the start function and then consistently however often you want it to repeat. As you can see here I used the spawn time and made it repeat after that for the same amount of time so it would keep on repeating depending on how long the spawn time is.

I then made sure that the power was defined in the start function as I came across a problem where  the new ball spawned would spawn at the full speed rather than the original speed so this sets it right as it spawns.

collision.PNG

This function is called everytime the ball hits anything. The original purpose of this function is to make it so that the ball always has movement  by the first part of the function added force to the ball everytime it hits anything. I changed this by making it instead use the variable I defined earlier and making it so that it adds 2% of the power to itself anytime a ball hits anything as you can see by the second line of this.

Spawnball.PNG

Finally I used the function that I created earlier to make a new version of the ball spawn on the timer I made earlier using the instantiate command that I have worked with in the past.

Balls.gif

Overall I learned a lot from doing this such as the invoke repeating command that I think I will use again to make timers in the future and overall I think this has helped me to become a slightly better programmer. To improve on this in the future I need to make more complicated scripts and try to use more commands that I haven’t used before.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s