Newer
Older
Clinton Galbraith
committed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerTetherScript : MonoBehaviour {
/* How far the players can go before the tether pulls them in */
[SerializeField]
private float engageDistance;
clg544
committed
[SerializeField]
private float distanceMultiplier;
private bool taught;
Clinton Galbraith
committed
/* Hooke's law labels this k, how much force the tether applies */
[SerializeField]
private float elasticity;
/* The current distance between the two players */
[SerializeField]
private float curDistance;
/* Our Players */
[SerializeField]
private GameObject playerOne;
[SerializeField]
private GameObject playerTwo;
private Rigidbody2D playerOneBody;
private Rigidbody2D playerTwoBody;
/* Resource to represent the tether as a line */
clg544
committed
private GameObject[] tetherLinks;
private LineRenderer tetherVisual;
[SerializeField]
private float linkSpeed;
Clinton Galbraith
committed
/* Draw the tether as a line between the two players */
public void DrawTetherAsLine()
Clinton Galbraith
committed
{
tetherVisual.SetPosition(0, playerOne.transform.position);
tetherVisual.SetPosition(1, playerTwo.transform.position);
}
public Vector3 getCentre()
{
return (playerOne.transform.position + playerTwo.transform.position) / 2;
}
public void distributeNodes()
{
Vector3 diff = (playerTwo.transform.position - playerOne.transform.position) / (tetherLinks.Length + 1);
int i = 1;
foreach (GameObject link in tetherLinks)
{
link.transform.position = playerOne.transform.position + (diff * i);
//link.GetComponent<Rigidbody2D>().velocity = Vector3.MoveTowards(link.transform.position, playerOne.transform.position + (diff * i), linkSpeed);
i++;
}
}
Clinton Galbraith
committed
// Use this for initialization
void Start () {
playerOneBody = playerOne.GetComponent<Rigidbody2D>();
playerTwoBody = playerTwo.GetComponent<Rigidbody2D>();
clg544
committed
tetherLinks = GameObject.FindGameObjectsWithTag("TetherLink");
GameObject[] temp = new GameObject[tetherLinks.Length];
/* Sort all of the links */
clg544
committed
for(int i = 0; i < tetherLinks.Length; i++)
{
// Sorted by GameObject Name! Important, as we adust positions with this array.
int cur = int.Parse(tetherLinks[i].name[4].ToString() + tetherLinks[i].name[5].ToString());
temp[cur] = tetherLinks[i];
}
tetherLinks = temp;
/* Set the links targets, with special cases for the end links */
tetherLinks[0].GetComponent<DistanceJoint2D>().connectedBody = playerOneBody;
tetherLinks[0].GetComponent<TetherLinks>().setConnection(playerOne);
for (int i = 1; i < tetherLinks.Length; i++)
{
tetherLinks[i].GetComponent<DistanceJoint2D>().connectedBody = tetherLinks[i - 1].GetComponent<Rigidbody2D>();
tetherLinks[i].GetComponent<TetherLinks>().setConnection(tetherLinks[i-1]);
}
playerTwo.GetComponent<DistanceJoint2D>().connectedBody = tetherLinks[tetherLinks.Length - 1].GetComponent<Rigidbody2D>();
playerTwo.GetComponent<TetherLinks>().setConnection(tetherLinks[tetherLinks.Length - 1]);
distributeNodes();
Clinton Galbraith
committed
tetherVisual = this.GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update () {
clg544
committed
bool curTaught = (curDistance > engageDistance);
Clinton Galbraith
committed
// Get current distance
Vector3 posOne = playerOne.transform.position;
Vector3 posTwo = playerTwo.transform.position;
Clinton Galbraith
committed
Vector3 difference = posOne - posTwo;
// curdistance = Magnitide of difference
Clinton Galbraith
committed
curDistance = Vector3.Distance(posOne, posTwo);
/* if The players are far enough apart... */
clg544
committed
if (curTaught)
Clinton Galbraith
committed
{
clg544
committed
float distance = curDistance - engageDistance;
/* Apply force of Distance squared, Scaled by elasticity, and divide among both players
*
* I used distance squared to make the tether more responsive, though less realistic. - Clint
*/
clg544
committed
float tension = (elasticity * (distance * distanceMultiplier)) / 2;
Clinton Galbraith
committed
/* Vector difference gets magnitide = tension */
Clinton Galbraith
committed
difference = Vector3.ClampMagnitude(difference, tension);
Clinton Galbraith
committed
playerOneBody.AddForce(-difference);
playerTwoBody.AddForce(difference);
}
if (curTaught && (!taught))
clg544
committed
{
distributeNodes();
clg544
committed
DrawTetherAsLine();
}
else if(taught && (!curTaught))
clg544
committed
{
BroadcastMessage("ConnectNodes");
playerTwo.SendMessage("ConnectNodes");
}
taught = curTaught;