Primitive Understanding & Battle Balls

This week took a different turn in Programming, beginning with a simple concept that would help us review correct syntax and then an exercise to keep us busy over the week involving an arena of bouncing balls (more on this later).

Our simple task was to create a small script that may be useful as a portfolio entry, it would rotate and show off 3D models. The suggestions given to us were:

  1. Rotate around the Y axis each frame.
  2. Use a variable for the rotation speed.
  3. Change rotation direction when clicked.
  4. Rotate in X axis if space is being held.
  5. Scale up and down with arrow keys.
  6. Bob up and down with a sine wave.

Along with these suggestions the only bit of base code provided was:

A familiar sight I’ve used many times before.

Thankfully this seems to be a common exercise as there are plenty of helpful hints and tips across the internet for setting up similar scenes. Here are a breakdown of my variables:


I know I’ve commented a lot of it in the script so I’ll try not to repeat myself too much. The float originalY will be used later to determine the starting Y position of the cube, this is needed for the MathF.Sin which will calculate our bobbing motion. The two rotate floats will be used to determine rotation speed and the floatStrength will determine the intensity of the bobbing.


On the scene starting our originalY float will determine its value by looking at where the object is on the Y axis. This will be used later to determine the home value of the bob, for lack of a better term.


Here is the meat of the script driving the scene. On the first line you can see the good old rotate script being used to spin the object in the Y axis, nothing new here.

The transform.position is our sine wave creating the bob. Within the vector3 x and z are set to get current position but Y takes the originalY value and adds a MathF.Sin over time * the float strength variable. The strength variable determines the height of the wave and therefore the top and bottom positions of the bob.

The final GetKey function spins the object on its X axis when space is pressed, mostly the same as the previous rotate code.


For the last little bit, when our mouse is clicked it changes our rotateY value to a minus value, making the object spin in the opposite direction. Tada! One model spinning bit of showcase code! The only thing I didn’t squeeze into this exercise was the scaling up and down with the arrow keys. I’ll have to return at a later date and figure this out.


Battle Balls

It’s class deathmatch time, last man standing! We have been given an exercise to code an ability onto a basic sphere. These will all be dropped into an arena next class and we get to watch what carnage happens (I’m betting on Unity breaking personally). Everybody has to take the player tag for their sphere and we aren’t allowed to delete the floor, these are the only rules.

I’m not finished the exercise yet but I can bring you up to speed to where I currently am. Initially I didn’t know where to begin, all I knew was I wanted a clone mechanic on collision but first wanted to tackle the balls movement. A standard bounce with a physics material would slow down pretty fast and lay dormant in the arena, minimising the chance of further collisions. To solve this I setup float variables for x, y and z velocity but had the value set to a random range which would trigger on collision with any surface, theoretically keeping them alive forever and unpredictable.


The rest of this script adds force to collisions if that happens to be anything with the player tag. Currently I don’t know exactly what ForceMode is doing, as I pulled it straight off the Unity database and tested it. However its creating some interesting results, akin to the movements you’d expect from a deflating balloon. I may leave it in purely for comic value. The end of the script calls the CloningFunction twice which I’ll explain in a moment.


So the CloningFunction isn’t calling a simple prefab, since we have no control over the final scene in Unity everything needs to be contained within one script. To get around this I create a brand new primitive game object sphere, assign this as a child of the ball that created it and add a rigidbody, the whole script, a renderer material colour and a tag without need of the inspector. Therefore making the new child ball a duplicate of its parent.


This is my progress so far, I’ll come back later in the week to try and add a few more features.


As an added bit of fun I added a change of mass to objects being collided with, reducing their mass to 0.09, which when paired with the ForceMode.Impulse creates some rather hilarious physics practically shooting other balls out of the arena at high speed. This lives within the OnCollisionEnter function:

collision.rigidbody.mass = 0.08f;

The only other change that has been made is clone spawns of the original ball now don’t parent to the original. Turns out if the parent gets destroyed (if another player decides to get cheeky) it’ll destroy all the children too.



Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s