TrueSync
TSBoxCollider2D.cs
1 using UnityEngine;
2 using UnityEngine.Serialization;
3 
4 namespace TrueSync {
8  [AddComponentMenu("TrueSync/Physics/BoxCollider2D", 0)]
9  public class TSBoxCollider2D : TSCollider2D {
10 
11  [FormerlySerializedAs("size")]
12  [SerializeField]
13  private TSVector2 _size = TSVector2.one;
14 
18  public TSVector2 size {
19  get {
20  if (_body != null) {
21  TSVector2 halfVector = ((Physics2D.PolygonShape)_body.FixtureList[0].Shape).Vertices[0] * 2;
22  halfVector.x /= lossyScale.x;
23  halfVector.y /= -lossyScale.y;
24 
25  return halfVector;
26  }
27 
28  return _size;
29  }
30  set {
31  _size = value;
32 
33  if (_body != null) {
34  TSVector size3 = new TSVector(_size.x, _size.y, 1);
35  TSVector sizeScaled = TSVector.Scale(size3, lossyScale);
36 
37  ((Physics2D.PolygonShape)_body.FixtureList[0].Shape).Vertices = TrueSync.Physics2D.PolygonTools.CreateRectangle(sizeScaled.x * FP.Half, sizeScaled.y * FP.Half);
38  }
39 
40  }
41  }
42 
46  public void Reset() {
47  if (GetComponent<BoxCollider2D>() != null) {
48  BoxCollider2D boxCollider2D = GetComponent<BoxCollider2D>();
49 
50  size = new TSVector2(boxCollider2D.size.x, boxCollider2D.size.y);
51  Center = new TSVector2(boxCollider2D.offset.x, boxCollider2D.offset.y);
52  isTrigger = boxCollider2D.isTrigger;
53  } else if (GetComponent<BoxCollider>() != null) {
54  BoxCollider boxCollider = GetComponent<BoxCollider>();
55 
56  size = boxCollider.size.ToTSVector2();
57  Center = boxCollider.center.ToTSVector2();
58  isTrigger = boxCollider.isTrigger;
59  }
60  }
61 
65  public override TrueSync.Physics2D.Shape CreateShape() {
66  TSVector size3 = new TSVector(size.x, size.y, 1);
67  TSVector sizeScaled = TSVector.Scale(size3, lossyScale);
68  return new TrueSync.Physics2D.PolygonShape(TrueSync.Physics2D.PolygonTools.CreateRectangle(sizeScaled.x * FP.Half, sizeScaled.y * FP.Half), 1);
69  }
70 
71  protected override void DrawGizmos() {
72  Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
73  }
74 
75  protected override Vector3 GetGizmosSize() {
76  TSVector size3 = new TSVector(size.x, size.y, 1);
77  return TSVector.Scale(size3, lossyScale).ToVector();
78  }
79 
80  }
81 
82 }
FP y
The Y component of the vector.
Definition: TSVector.cs:39
bool isTrigger
If it is only a trigger and doesn&#39;t interfere on collisions.
Definition: TSCollider2D.cs:30
override Vector3 GetGizmosSize()
Returns the gizmos size.
override void DrawGizmos()
Draws the specific gizmos of concrete collider (for example "Gizmos.DrawWireCube" for a TSBoxCollider...
void Reset()
Sets initial values to size based on a pre-existing BoxCollider or BoxCollider2D. ...
A vector structure.
Definition: TSVector.cs:29
void Scale(TSVector other)
Multiplies each component of the vector by the same components of the provided vector.
Definition: TSVector.cs:168
TSVector2 Center
Center of the collider shape.
Definition: TSCollider2D.cs:60
override TrueSync.Physics2D.Shape CreateShape()
Create the internal shape used to represent a TSBoxCollider.
Collider with a box 2D shape.
TSVector lossyScale
Holds an first value of the GameObject&#39;s lossy scale.
FP x
The X component of the vector.
Definition: TSVector.cs:37
TSVector2 size
Size of the box.
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:11