Skip to content
Snippets Groups Projects
Commit 36b3d845 authored by clg544's avatar clg544
Browse files

Added detailed comments to all scripts

parent 19e985b8
No related branches found
No related tags found
2 merge requests!8Develop,!3Feature physics
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputManagerScript : MonoBehaviour {
[SerializeField]
......@@ -10,16 +10,20 @@ public class InputManagerScript : MonoBehaviour {
private GameObject playerTwo;
private PlayerBehavior playerOneBehavior;
private PlayerBehavior playerTwoBehavior;
// Use this for initialization
private PlayerBehavior playerTwoBehavior;
// Use this for initialization
void Start () {
playerOneBehavior = playerOne.GetComponent<PlayerBehavior>();
playerTwoBehavior = playerTwo.GetComponent<PlayerBehavior>();
}
// Update is called once per frame
}
// Update is called once per frame
void Update () {
/**
* Player One Movement Controls
*/
if (Input.GetKey(KeyCode.W))
{
playerOneBehavior.MoveUp();
......@@ -43,6 +47,9 @@ public class InputManagerScript : MonoBehaviour {
}
/**
* Player Two Movement Controls
*/
if (Input.GetKey(KeyCode.UpArrow))
{
playerTwoBehavior.MoveUp();
......@@ -63,5 +70,5 @@ public class InputManagerScript : MonoBehaviour {
{
playerTwoBehavior.Brake();
}
}
}
}
}
......@@ -7,19 +7,27 @@ public class PlayerBehavior : MonoBehaviour {
/* Movement to apply this frame */
private Vector3 frameMovement;
/* How much force is applied by player movement */
[SerializeField]
private float acceleration;
/* How much the velocity is scaled down on brake */
[SerializeField]
private float brakeFraction;
/* The maximum speed a player is allowed to go */
[SerializeField]
private float maxSpeed;
/* Holds our plaayer and player body */
public GameObject player;
private Rigidbody2D playerBody;
/**
* Collection of basic movement functions that add to a movement vector, which is
* reset every frame.
*/
public void MoveLeft()
{
frameMovement.x -= acceleration;
......@@ -40,6 +48,9 @@ public class PlayerBehavior : MonoBehaviour {
frameMovement.y -= acceleration;
}
/**
* Scale the player's velocity down, if brakeFraction is < zero
*/
public void Brake()
{
playerBody.velocity *= brakeFraction;
......@@ -53,13 +64,17 @@ public class PlayerBehavior : MonoBehaviour {
// Update is called once per frame
void Update () {
playerBody.AddForce(Vector3.ClampMagnitude(frameMovement, acceleration));
/* Accelerate the player with a movement vector based on this frames input */
playerBody.AddForce(frameMovement.normalized * acceleration));
if(playerBody.velocity.magnitude > maxSpeed)
{
/* Cap player speed with a velocity check */
if (playerBody.velocity.magnitude > maxSpeed)
playerBody.velocity = (playerBody.velocity.normalized * maxSpeed);
}
/* Apply some simple friction */
playerBody.velocity *= .99f;
/* Reset any persistant variables */
frameMovement = Vector3.zero;
}
}
......@@ -7,9 +7,6 @@ public class PlayerTetherScript : MonoBehaviour {
/* How far the players can go before the tether pulls them in */
[SerializeField]
private float engageDistance;
/* The Absolute Max */
[SerializeField]
private float maxDistance;
/* Hooke's law labels this k, how much force the tether applies */
[SerializeField]
......@@ -30,10 +27,11 @@ public class PlayerTetherScript : MonoBehaviour {
private Rigidbody2D playerTwoBody;
/* My Components */
/* Resource to represent the tether as a line */
LineRenderer tetherVisual;
public void DrawTether()
/* Draw the tether as a line between the two players */
public void DrawTetherAsLine()
{
tetherVisual.SetPosition(0, playerOne.transform.position);
tetherVisual.SetPosition(1, playerTwo.transform.position);
......@@ -50,25 +48,32 @@ public class PlayerTetherScript : MonoBehaviour {
// Update is called once per frame
void Update () {
// Get current distance
Vector3 posOne = playerOne.transform.position;
Vector3 posTwo = playerTwo.transform.position;
// Distance vector of p1 to p2
// Calculate vector of p1 to p2
Vector3 difference = posOne - posTwo;
// Magnitide of difference
// curdistance = Magnitide of difference
curDistance = Vector3.Distance(posOne, posTwo);
/* if The players are far enough apart... */
if (curDistance > engageDistance)
{
/* Apply force of Distance, Scaled by elasticity, and divide among both players */
float tension = (elasticity * (curDistance - engageDistance)) / 2;
/* Vector difference gets magnitide = tension */
difference = Vector3.ClampMagnitude(difference, tension);
/* Apply said Vector to both players */
playerOneBody.AddForce(-difference);
playerTwoBody.AddForce(difference);
}
DrawTether();
/* Draw the tether */
DrawTetherAsLine();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment