Trigonometry Demo

Trigonometry Demo on GitHub

Trig Demo on YouTube

trig demo screen capThe is a simple little demo that I did for one of my programming classes which shows that I know how to use basic trigonometry in code. There were two main goals for this project.

  1. Have the hand on the clock follow the mouse cursor through code.
  2. Have the numbers of the clock be generated through code as well.

To have the mouse follow the cursor hand I used the following code:

void RotateToMouse()
{
// Gets the mouse location from the screen
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// Use the Mathf.Atan2 function to get the angle at which we need to rotate
float angleFromCenter = Mathf.Atan2(mouseWorldPos.y, mouseWorldPos.x);

// Since the angle input from Atan2 is returned in radians, we have to
// convert it to degrees in order to use the Quaternion.Euler method.
float degrees = angleFromCenter * Mathf.Rad2Deg + 180;

transform.rotation = Quaternion.Euler(0, 0, degrees);
}

As you can see from the YouTube video, both of my goals were achieved. How I generated the angle interval for between the numbers is like so:

angleInterval = (-1) Mathf.PI / ((numberOfObjects) / 2);

The reason that I multiplied by -1 in order to get the numbers to go in a clockwise direction when instantiating them, instead of a counter-clockwise direction.

I also have a float called startAngle, which is (PI/2), so that the generation starts at the top like a clock would. Obviously this is adjustable through the code.

Perlin Noise Terrain Generation

Terrain Gen Demo on GitHub

What this small project does is generate terrain patterns in Unity using 2D Perlin Noise. The script that I wrote for this demo is very reusable, but does have to be put on a terrain object for it to work, or at least something with a Terrain Data component. The main part of this script is the method where I loop through the TerrainData component’s height map, and set each value equal to a Mathf.PerlinNoise value.

perlinnoiseterrain
Perlin Noise Terrain

The fields for this script are simple:

A private TerrainData object

A public Vector3 value that is used to apply the size of the terrain that you want

A public int so that you can adjust the Terrain heightmap Resolution

2 public float that are limited to a range of between 0.0 and 1.0. I use tha [Range(0,1)] modifier here so that it is a nice slider in the Unity Editor, making it easier on me and others in the future.

Here is what the inspector looks like for this component

terraingeninspector
Component’s Inspector

This was a class assignment for my IGME 202.04 class(Interactive Media Development), with Professor Erin Casciolli, and I did get a 100% on it.