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