Unity Code

By | January 2020

Setting up Git

Installing Git Windows 10

  • Install Git – put in an short-named folder like C:\Y\G\ (G == Git)
  • Don’t need to install Git Bash or Git GUI, so just installing the Git Framework. Rest as default options.
  • Clone a project that’s made in GitHub or GitLab

Clone a Project

  • In GitLab, create a new Project without README to see instructions.
  • cd C:\Y\G\
  • git clone “url-link”
  • git add . OR git add Assets (goes directly into Assets Folder)
  • git commit -m “a comment
  • git push origin master

Command Prompt Window Properties Windows 10

  • Change colour, font, text-size
  • “Start In” default is – %HOMEDRIVE%%HOMEPATH% which starts up in C:\Users\(ComputerName)
  • Change change it to – %HOMEDRIVE%\Y
  • Then select dropdown for the folder “G”
  • This will start up Command Prompt automatically in C:\Y\G when searching “cmd” in the Windows 10 Menu Search
  • Opening Command Prompt via Win+R, “cmd” will open up an unmodified Command Prompt
  • Different cmd shortcuts can have different properties

Always pull before pushing

  • git pull origin master
  • git status
  • git add Assets
  • git commit -a -m “a comment
  • git push origin master

Move an object – Transform and Rigidbodies

Press Start – Unity Movement [Rigidbody vs Translate]
Using Transform is basically “Rigidbody.Is_Kinematic = true;“.
Translate is better used in moving specific objects that doesn’t require physics.

When using Rigidbodies, call in FixedUpdate instead of Update:
– “FixedUpdate” runs every physics step.
– “Update” runs once per frame, which is good for detecting player input.


Jason Weimann – Unity 101: 5 ways to move Unity3D Objects
Using Transform to push against a collider causes the object to “vibrate”.

// Transform SetPosition - Setting the object's position to a new position.
void Update()
{
   transform.position += transform.forward * Time.deltaTime;
}

// Transform Translate - Instead of giving the object a new position, give an amount to move.
void Update()
{
   transform.Translate(Vector3.forward * Time.deltaTime);
}

Rigidbody.Is_Kinematic = true;” means that the object doesn’t interact with the physics sytem, pass through colliders but can move by adding Force.

// Rigidbody AddForce - Adding force every single frame (e.g. acceleration).
Rigidbody RB;
float Force;

void FixedUpdate()
{
   RB.AddForce(transform.forward * Force * Time.deltaTime);
}

// Rigidbody MovePosition - Similar to Transform.translate. Detects physics and collisions.
// Set Rigidbody.Collision_Detection from "Discrete" to "Continuous".
Rigidbody RB;
float Force;

void FixedUpdate()
{
   Vector3 NewPosition = transform.position + (transform.forward * Time.deltaTime);
   RB.MovePosition(NewPosition);
}

// Rigidbody SetVelocity - Set a velocity value (e.g. shooting an object) in Awake method.
// Move the object at a constant rate over time.
Rigidbody RB;
float Force;

void Awake()
{
   RB = GetComponent<Rigidbody>();
   RB.velocity = transform.forward * Force * Time.deltaTime;
}
// Debug.Log(RB.velocity.magnitude);
// Returns the xyz Vector3 coordinate values to a single READONLY float.

Move an object – AddForce Modes

UnityDocs – AddForce and ForceMode

Rigidbody RB;
float xForce, yForce, zForce;
Vector3 TotalForce;

void Start()
{
   TotalForce = new Vector3(xForce, yForce, zForce);
}

void FixedUpdate()
{
   RB.AddForce(TotalForce, ForceMode.Acceleration);   // Acceleration.
   RB.AddForce(TotalForce, ForceMode.Force);          // Continuously applied force.
   RB.AddForce(TotalForce, ForceMode.Impulse);        // Applies an instant force.
   RB.AddForce(TotalForce, ForceMode.VelocityChange); // Applies an instant velocity change.
}

Better Jump

Board To Bits Games – Better Jumping in Unity with Four Lines of Code

Rigidbody RB;
float FallMultiplier = 2.5f;
float LowJumpMultiplier = 2f;

void Awake()
{
   RB = GetComponent<Rigidbody2D>();
}

void Update()
{
   if (RB.velocity.y < 0) 
   { 
      RB.velocity += Vector2.up * Physics2D.gravity.y * (FallMultiplier - 1) * Time.deltaTime; 
   }
   else if (RB.velocity.y > 0 && !Input.GetButton("Jump"))
   {
      RB.velocity += Vector2.up * Physics2D.gravity.y * (LowJumpMultiplier - 1) * Time.deltaTime;
   }
}

Direction Left or Right Only

int Direction;

void Start()
{  // Direction can be left(1) or right(-1) only.
   Direction = Random.Range(0, 2) * 2 - 1;
}

KeyCode

UnityDocs – KeyCode

void Update()
{
   if (Input.GetKey(KeyCode.Space))
   {
      Debug.Log("Space key was pressed.");
   }
}

Instantiate

UnityDocs – Instantiate

public GameObject Prefab;

void Start()
{
   Instantiate(Prefab, new Vector3(0, 0, 0), Quaternion.identity);
}

Leave a Reply