Skip to content
Snippets Groups Projects
Commit 47a8cd4f authored by clg544's avatar clg544
Browse files

Tether interacts with rigidBody's, but requires 200+ Phys2D position steps to...

Tether interacts with rigidBody's, but requires 200+ Phys2D position steps to do so.  Might be optimizable.
parent c0c107ba
No related branches found
No related tags found
1 merge request!14Feature teather
...@@ -56,7 +56,7 @@ Rigidbody2D: ...@@ -56,7 +56,7 @@ Rigidbody2D:
m_Simulated: 1 m_Simulated: 1
m_UseFullKinematicContacts: 0 m_UseFullKinematicContacts: 0
m_UseAutoMass: 0 m_UseAutoMass: 0
m_Mass: 0.0001 m_Mass: 0.001
m_LinearDrag: 0 m_LinearDrag: 0
m_AngularDrag: 0 m_AngularDrag: 0
m_GravityScale: 0 m_GravityScale: 0
...@@ -79,7 +79,7 @@ CircleCollider2D: ...@@ -79,7 +79,7 @@ CircleCollider2D:
m_UsedByComposite: 0 m_UsedByComposite: 0
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
serializedVersion: 2 serializedVersion: 2
m_Radius: 0.005 m_Radius: 0.001
--- !u!114 &114281913493297394 --- !u!114 &114281913493297394
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1
......
This diff is collapsed.
...@@ -32,6 +32,8 @@ public class PlayerTetherScript : MonoBehaviour { ...@@ -32,6 +32,8 @@ public class PlayerTetherScript : MonoBehaviour {
/* Resource to represent the tether as a line */ /* Resource to represent the tether as a line */
private GameObject[] tetherLinks; private GameObject[] tetherLinks;
private LineRenderer tetherVisual; private LineRenderer tetherVisual;
[SerializeField]
private float linkSpeed;
/* Draw the tether as a line between the two players */ /* Draw the tether as a line between the two players */
public void DrawTetherAsLine() public void DrawTetherAsLine()
...@@ -45,7 +47,19 @@ public class PlayerTetherScript : MonoBehaviour { ...@@ -45,7 +47,19 @@ public class PlayerTetherScript : MonoBehaviour {
return (playerOne.transform.position + playerTwo.transform.position) / 2; 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++;
}
}
// Use this for initialization // Use this for initialization
void Start () { void Start () {
playerOneBody = playerOne.GetComponent<Rigidbody2D>(); playerOneBody = playerOne.GetComponent<Rigidbody2D>();
...@@ -54,16 +68,29 @@ public class PlayerTetherScript : MonoBehaviour { ...@@ -54,16 +68,29 @@ public class PlayerTetherScript : MonoBehaviour {
tetherLinks = GameObject.FindGameObjectsWithTag("TetherLink"); tetherLinks = GameObject.FindGameObjectsWithTag("TetherLink");
GameObject[] temp = new GameObject[tetherLinks.Length]; GameObject[] temp = new GameObject[tetherLinks.Length];
/* Sort all of the links */
for(int i = 0; i < tetherLinks.Length; i++) for(int i = 0; i < tetherLinks.Length; i++)
{ {
// Sorted by GameObject Name! Important, as we adust positions with this array. // Sorted by GameObject Name! Important, as we adust positions with this array.
Debug.Log(tetherLinks[i].name + ":" + int.Parse(tetherLinks[i].name[4].ToString() + tetherLinks[i].name[5].ToString()));
int cur = int.Parse(tetherLinks[i].name[4].ToString() + tetherLinks[i].name[5].ToString()); int cur = int.Parse(tetherLinks[i].name[4].ToString() + tetherLinks[i].name[5].ToString());
temp[cur] = tetherLinks[i]; temp[cur] = tetherLinks[i];
} }
tetherLinks = temp; 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();
tetherVisual = this.GetComponent<LineRenderer>(); tetherVisual = this.GetComponent<LineRenderer>();
} }
...@@ -79,7 +106,7 @@ public class PlayerTetherScript : MonoBehaviour { ...@@ -79,7 +106,7 @@ public class PlayerTetherScript : MonoBehaviour {
Vector3 difference = posOne - posTwo; Vector3 difference = posOne - posTwo;
// curdistance = Magnitide of difference // curdistance = Magnitide of difference
curDistance = Vector3.Distance(posOne, posTwo); curDistance = Vector3.Distance(posOne, posTwo);
/* if The players are far enough apart... */ /* if The players are far enough apart... */
if (curTaught) if (curTaught)
{ {
...@@ -98,25 +125,14 @@ public class PlayerTetherScript : MonoBehaviour { ...@@ -98,25 +125,14 @@ public class PlayerTetherScript : MonoBehaviour {
playerTwoBody.AddForce(difference); playerTwoBody.AddForce(difference);
} }
// if we are re-entering engage distance
if(taught && (!curTaught))
{
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);
i++;
}
}
/* Draw the tether */ /* Draw the tether */
if (curTaught) if (curTaught && (!taught))
{ {
distributeNodes();
DrawTetherAsLine(); DrawTetherAsLine();
} }
else else if(taught && (!curTaught))
{ {
BroadcastMessage("ConnectNodes"); BroadcastMessage("ConnectNodes");
playerTwo.SendMessage("ConnectNodes"); playerTwo.SendMessage("ConnectNodes");
......
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class TetherLinks : MonoBehaviour { public class TetherLinks : MonoBehaviour {
public GameObject myConnection; public GameObject myConnection;
private LineRenderer tetherVisual; private Rigidbody2D myBody;
private LineRenderer tetherVisual;
public void ConnectNodes() public void ConnectNodes()
{ {
tetherVisual.SetPosition(0, this.transform.position); tetherVisual.SetPosition(0, this.transform.position);
tetherVisual.SetPosition(1, myConnection.transform.position); tetherVisual.SetPosition(1, myConnection.transform.position);
} }
// Use this for initialization
void OnCollisionStay2D(Collider2D coll)
{
Debug.Log("Hit!");
coll.GetComponent<Rigidbody2D>().velocity = myBody.velocity;
}
// Use this for initialization
void Start () { void Start () {
tetherVisual = this.GetComponent<LineRenderer>(); myBody = this.GetComponent<Rigidbody2D>();
} tetherVisual = this.GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update () { // Update is called once per frame
void Update () {
}
} }
public void setConnection(GameObject gameOb)
{
myConnection = gameOb;
}
}
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