Community-based video game design and development.

Posts tagged ‘c sharp’

C# Script: POV Camera

Engine: Unity
Language: C#

Description:
This script is a very blunt and simple approach to have the camera always 
reset to the player's current position, giving you a POV feeling. 
It precedes that you want to keep the camera separate from the 
player's avatar in the hierarchy and that you attach the script 
to the camera.
Note that you will have to make adjustments depending on how tall 
your player avatar is, as the default position may vary 
from object to object.

//Script start
public class POVCamera : MonoBehaviour {
//Initialise members
Vector3 playerposition;
Transform self;
GameObject player;

//This happens when the scene loads
void Start() {
//Returns the Transform component of the camera
self = GetComponent<Transform>(); 
//Returns the GameObject that you called "player" 
//for your player's avatar
player = GameObject.Find("player");
}

//This happens once per frame
void Update() {
//Gets the player's position in (x, y, z)
playerposition = player.transform.position;
//Sets the camera's position to the position of the player
self.transform.position = playerposition;
}
}

Day 5-8: Achievements

Day 5

Physics

  1. Velocity
  2. Acceleration
  3. Kinetic Force

C#

  1. Acceleration programmed

Day 6

Mathematics

  1. Dimensionless Numbers

Geometry

  1. Vector addition
  2. Point addition resulting in vector

Physics

  1. Drag/Fluid-/Air Friction:
    1. Meaning
    2. Drag coefficient
  2. Air Density:
    1. Temperature & Pressure dependency
  3. Reynold’s Number
  4. Viscosity of fluids
  5. Dry Friction:
    1. Stiction
    2. Kinetic Friction:
      1. Coefficient
      2. Coulomb’s formula
      3. Geometric dependency
  6. Angular velocity
  7. Centrifugal force
  8. Graviation:
    1. Earth’s Standard Gravity (gravitational acceleration)
    2. Newton’s law of Universal Gravitation
    3. Gravitational constant G
  9. Normal force:
    1. Formula
    2. Angular aspects
  10. Collision:
    1. Deflection (1D, 2D)
    2. Elasticity (elastic, inelastic)
    3. Impact (deformation)
  11. Tension:
    1. Definition
    2. Relation towards Compression
    3. Relation towards Stress
  12. Stress
  13. Ductility & Malleability
    1. Definition
    2. Relation to Tension and Compression
  14. Pressure

C#

  1. Acceleration formula:
    1. Maximum speed
    2. Adjusting units to Unity’s engine,
    3. Drag considered
  2. Velocity formula:
    1. Considers input-based acceleration (keyboard)
    2. Considers stored acceleration without input (“sliding”)
  3. Getter/Setter

Unity

  1. Accessing Object > Component > Value
  2. Velocity calculation:
    1. Cube in air for T=15°C, p=1atm, g=9.80665m/s2
  3. Prefab created: ‘Gravitational Cube

Day 7

C#

  1. Regex:
    1. Repetition
    2. Word Mining algorithm
  2. Booleans: inversion
  3. Setter: successful application
  4. Named arguments
  5. Optional arguments
  6. Inheritance
  7. Polymorphism
  8. Encapsulation:
    1. Properties (get, set)
  9. Naming conventions
  10. Null vs Nil
  11. Pointers: overview
  12. ‘this’ keyword: repetition

Unity

  1. Vector.Angle:
    1. Definition
    2. Player’s direction calculated
  2. GUI:
    1. OnGUI basics
    2. Rect
    3. GUI.Button with working method
  3. Asset vs Object
  4. Instantiating:
    1. Cloning of an object successful
    2. Object vs GameObject
  5. Mouse:
    1. Position
    2. GetMouseButtonDown

Day 8

Mathematics

  1. Absolute value: repetition
  2. Mapping/Translation:
    1. Algebraic Functions
    2. Linear Map
    3. Matrix method

Geometry

  1. Cross product
  2. Norm
  3. Unit vector
  4. Dot product
  5. Mapping/Translation:
    1. Vector mapping
  6. Exterior algebra
    1. Brief insight
    2. Bivectors: brief insight
  7. Tensors
  8. Osculating circle: definition

Physics

  1. Stress:
    1. Simple uniaxial stress
    2. Shear stress
    3. Isotropic stress (tension) & Hydrostatic pressure (compression)
  2. Net force
  3. Moment of Inertia
  4. Angular/Rotational Motion:
    1. Angular Velocity
    2. Angular Acceleration
    3. Angular Momentum
  5. Torque
  6. Force, Energy, Work, Power: differences and definition
  7. Kinetic energy:
    1. Formulae
    2. Rotational energy
  8. Kinematics:
    1. Uniform Acceleration: partially (motion)
  9. Centripetal force: introduction

C#

  1. Externalisation: passing only attributes to a public method instead of repeating it inside the script (linked to Unity, 3.)
  2. Structure:
    1. Optimising by combining instructions into as few methods as possible.
    2. Optimising by properly separating and prioritising methods within parent handlers.

Unity

  1. Materials:
    1. Created simple texture.
    2. Created simple bumpmap from texture.
  2. Heightmap: basic knowledge
  3. Script Management: empty GameObjects serving as libraries (linked to C#, 1.)
  4. POV Camera: attached to player avatar’s ‘head’
  5. Mouse: axis translates Camera’s angle to rotate (“POV mouse camera”)

Day 4: Achievements

Graphics & Engine

  1. What is Raycasting and how do you apply it?
  2. What are Collision Masks and how do you apply them?
  3. How do you access objects’ components and their values?

Physics

  1. Velocity (v = Δx/Δt)
  2. Acceleration (a = v/t)
  3. Momentum (p = m * v)
  4. Kinetic Energy (E = m * v2)
  5. Weight (W = m * g)
  6. Mass (m = F / a)
  7. Density (ρ = m / V)
  8. Volume (V = x * y * z) (for a cube)
  9. Grativation (g = 9.80665 m/s2) & (F = G * m1 * m2 / r2) & (G = 6.67384(80) * 10−11 * N (m/kg)2)

Material Science

  1. Iron: physical properties

Programming

  1. 2D collision detection.
  2. Primitive acceleration.

Day 1: Player Control Input in C#

The first script I’ve written and helped me find the entrance into C# and Unity did one simple, yet powerful thing: It allows the player to move a square freely in two dimensions by using the default keyboard input.

using UnityEngine;

using System.Collections;

[RequireComponent(typeof(PlayerPhysics))]

public class PlayerControl : MonoBehaviour {

//Handler Variables

public float h;

public float v;

private Vector2 amount;

private PlayerPhysics playerPhysics;

// Use this for initialization

void Start () {

playerPhysics = GetComponent<PlayerPhysics>();

}

// Update is called once per frame

void Update () {

h = Input.GetAxis (“Horizontal”);

v = Input.GetAxis (“Vertical”);

amount = new Vector2(h, v);

playerPhysics.Move(amount);

}

}

//=== SECOND SCRIPT STARTS HERE ===

using UnityEngine;

using System.Collections;

[RequireComponent(typeof(BoxCollider))]

public class PlayerPhysics : MonoBehaviour {

public void Move(Vector2 amount) {

transform.Translate(amount);

}

}

The code consists of two scripts that I had to merge here for facility: PlayerControl.cs and PlayerPhysics.cs. Where the first translates the horizontal (h) and vertical (v) arrow-key inputs into a vector (amount), the second shifts the position of the player (i.e. the cube) by just that amount into those directions (Move()).

Through that, simple two-dimensional games can be created that don’t require a lot of special attention. It matters not whether the “player” is a cube, a 2D sprite or a 3D animated model – it will always move with that script and alterations will smoothen it out, give it a more realistic and fluid, stable feeling.

It was very refreshing for me to see many familiar terms and syntax elements again after a “long” time, but I had to rely on some sources to update my memory and teach me this or that, especially when it came to working with physical objects on a plane/in a space and the Unity engine’s terms, usage and architecture. I greatly enjoyed it, though, and feel like I’ve opened the gate to a long, arduous journey of learning, training and perfecting C# programming and video game development.