I’ve got a lot of script going on with this task; I really wanted to try a few different things to see what I liked best and what I thought worked well. I’ve done more Unity documentation rummaging than I thought possible, which is excellent because (while I may not use most of it) I feel like I’ve learned a lot of things for future projects.
There were a couple of ideas I had in the pipeline, which I’m sure will annoy most other people who may make a more offensive script for their spheres! I wanted my sphere to survive and multiply, while the clones will end up having separate functions to act on – likely nothing destructive, since I don’t think that’s the point of the exercise. Instead, they’re going to change the colour of any other Player spheres they hit as well as shrink them by 10%. My clones will be independently created outside of any collisions, so as long as my sphere survives they will continue to spawn every 10 seconds (I may change this so I don’t overload the demo we’ll be doing in class).
The video is merely an example of a number of player spheres (including my own) starting the scene and my clones (coloured in aqua) appearing every 10 seconds. Whenever they hit a sphere tagged with “Player”, they change the colour and reduce their size by 10%. I’ve done a number of play tests and it seems like the player spheres can be reduced to a very small size given enough play time. I didn’t really want to make a video example THAT long, though.
I started thinking about what variables I would need as well as what should be in my Start function – I was going to make up a mixture of “defensive” and “offensive” scripting. I wanted to protect my Collider in case it was turned off by someone else’s script and attach my Rigidbody to my sphere. I also wanted to make an array which would contain all the other player spheres as GameObjects – I could sneakily have my original sphere ignore any collisions with other spheres tagged as Player. Even if my ball happens to throw itself out of the “arena” (which it does… because Unity physics) it will still continue to generate clones unless it’s destroyed.
I then had a few floats for the length of time between each clone spawn and how fast my spheres would move around. This all felt good for now and I could move on to what I wanted in my Update.
Here, I make sure that if my collider becomes disabled (either on my original sphere or on my clones) that it would automatically be enabled. The clone timer would also be controlled via here, as per usual for my scripts. I could count down every ten seconds until a new clone would appear – in my own tests, this seems like a good amount of time, but I might have to consider whether it will be too taxing on the school computer during demo time.
I then added in something that only my original sphere would do: generate clones. This was important, because if I didn’t restrict this part of the script just to the original sphere, the game would quickly overload with clones and crash. I would call my CloningFunction, but also tell my sphere to ignore any collisions it makes with objects tagged as “Player,” hopefully protecting me somewhat from anyone who comes up with something mean.
The above is my OnCollisionEnter script, which is probably the most involved part of my code. It dictates the sporadic behaviour of my spheres, which was an accident but I think it still works out and is quite funny! This is done by plugging the velocity variables into the AddForce calculation, but randomising the numbers.
After that, the two if statements are wholly dependent on whether it’s the clones (tagged as “Respawn”) reading the script or my original sphere. If it’s the clones and they hit something else with the tag “Player,” then they’re going to change the opponents material colour to green and reduce their size by 10%. I thought these two things would be interesting if I get enough clones onto the field and they hit other player spheres more than once.
The second IF statement is… actually a placeholder at the moment. It doesn’t really do anything but help me test whether the IgnoreCollision script is working the way it should. It also means I could add something extra for when my original sphere hits another player or I could write in something that happens when it hits one of the clones.
I like the CloningFunction – it contains a lot of experimental stuff that I had to look up for the first time. I also learned that I didn’t need to “instantiate” my clones, but instead Unity has a script for creating primitive objects; after that, it was just a matter of making the clones a child of my original sphere (for neatness’ sake), adding a Rigidbody to it for the collisions, adding the script, and making the colour an outstanding cyan hue! It would also change the clone’s tag to “Respawn” rather than use the “Player” one everyone else would be planning for. This might be cheeky, but hey, other people can look this up, too! I had to use one of the default tags already built into Unity, since there’s no way of making a new tag via script. I just picked one at random from the handful available and it’s stuck.
I’ve learned a lot along the way to get to this iteration of my script – I tested A LOT of other things that just didn’t fit with the other stuff I had, such as resizing my own spheres or changing their locations sporadically (which oddly made me think of Nightcrawler). Overall, I really like my script in its current form, although I might add more at the very last minute if something pops into my mind!