Unity – Coding using C#

I have began delving into the programming language C# within the Unity game engine. Using our tutor Ant’s tutorials as a guide, holding our hand through the process.

 

The introduction familiarised ourselves with Unity’s user interface, teaching us how to navigate through the software. This drew attention to the project tab on the lower section of the screen, also the asset folder within it.  In this folder, we created another folder labelled “Script” where will begin to create and save our collection of code using C#.

Auto Rotation

AutoRotationCode.jpg

This code, when applied to an object,  will rotate the selection by the desired amount of degrees per frame on the axis of choice. As these are “public”, the user is able to manipulate the value easily without accessing the code itself.  This is displayed on the front screen as shown below:

AutoRot.jpg

 

Auto Bob (Up & Down)

Auto Bob.jpg

These lines of code result in the object, on which it is applied, to appear to bob up and down on the “Y” axis. It does this by storing the objects original position and raising its “Y” location based on a Sin wave by adding this to its original position. Lastly, returning the object to its original location to complete the up and down motion.

Auto Side to Side

This is an amendment on the “Auto Bob” code, not included in the tutorial. Using the existing code as a base, I edited it so it affected the “X” axis instead of the “Y”. This causes the object to move side to side, as above, corresponding to a Sin wave.

SidetoSide.jpg

I then multiplied the transformation by -1 to reverse the direction of movement to provide an alternative.

SidetoSide1.jpg

 

Rigid Bodies

Applying a rigid body to a static mesh applies physics to the selection, adding rules such as gravity, friction and collisions. Rigid bodies are said to be “awake” if moving, and “sleeping” if static. This can easily be incorporated into code to adjust the rigid body based on these conditions.

If statements based on Rigid Body condition

conditioncode.jpg

The above code initial sets the render of the colour of the selected mesh to green, until the object come to a complete stop and is static (“Is Sleeping”). At this point, the object will then turn black based on this condition. The final section then applies the colour red to the mesh, in relation to whether to object is colliding with any other objects in the scene.

Rigid Body (Spherical) and Player Movement

PlayerMovement.jpg

This section allows me to control a spherical mesh as a character, using the directional keys as input. This works by applying a force to the corresponding horizontal or vertical axis in order to roll the sphere.  The initial code made the movement speed too slow, in result I multiplied the force by 5 to rectify the issue.

Conclusion

In conclusion, we were left with a variety of code which had the potential, when used in conjunction with each other, to make a simple game within Unity. I aim to further my knowledge using the Unity official website for useful tutorials to help achieve more varied techniques to help make my own game more unique.

I would suggest the coding / programming section of this course has been neglected a bit by myself, due to the fact I have been caught up in  the 3D modelling and creativity side. I have made it my intention to divert more attention into this subject immediately.

 

There were a number of variations of code which I have not mentioned but will come in handy when creating our individual and group games. These can be accessed via the tutorials mentioned above.

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