What are properties?
What is a delegate?
What is a coroutine?
They are the objects that we will be applying physics on. You can think of them as sets of triangles (or vertices, edges and faces). In unity we use meshes with the Mesh Filter and Mesh Renderer component.
Quick blender demo
Simple shapes that completely contain an object. Used for optimizing collision detection. The most popular bounding volumes are bounding boxes, spheres and capsules.
bool Intersect(Bounds a, Bounds b) {
return (a.min.x <= b.max.x && a.max.x >= b.min.x)
&& (a.min.y <= b.max.y && a.max.y >= b.min.y)
&& (a.min.z <= b.max.z && a.max.z >= b.min.z);
}
bool Intersect(Vector3 aCenter, float aRadius,
Vector3 bCenter, float bRadius) {
float distance = Mathf.Sqrt(
(aCenter.x - bCenter.x) * (aCenter.x - bCenter.x) +
(aCenter.y - bCenter.y) * (aCenter.y - bCenter.y) +
(aCenter.z - bCenter.z) * (aCenter.z - bCenter.z));
return distance < (aRadius + bRadius);
}
A tree structure of bounding volumes. Can make the time complexity of the collision detection algorithm logarithmic in the number of objects.
If even compound colliders don't offer enough precission then we can use Mesh Colliders to match the shape of the GameObject’s mesh exactly.
Mesh Colliders don't collide with other Mesh Colliders. For that to happen we need to make them into convex hulls.
By default colliders work for physics. To make them work just for collision we have to make them triggers. In scripts we work with triggers differently.
RigidBody body = GetComponent<RigidBody>();
// All forces are applied for the period of fixedDeltaTime
// Apply Force
body.AddForce(Vector3 force, ForceMode mode = ForceMode.Force);
// Apply Torque
body.AddTorque(Vector3 torque, ForceMode mode = ForceMode.Force);
// Move the RigidBody towards a position
body.MovePosition(Vector3 position);
When a GameObject has a collider but doesn't have a RigidBody
Controls whether physics affects the rigidbody
// XXX can be:
// Enter - Called the first frame when this collider
// touches another collider
// Stay - Called once per frame for every collider
// that is touching this collider
// Exit - Called when this collider stops touching
// other colliders
// For 2D collisions:
// OnCollisionXXX2D(Collision2D collisionInfo) {...}
void OnCollisionXXX(Collision collisionInfo) {
if (collisionInfo.gameObject.CompareTag("Enemy")) {
Destroy(collisionInfo.gameObject);
}
}
// XXX is the same as for collisions:
// Enter
// Stay
// Exit
// ----------------------------------
// For 2D triggers:
// OnTriggerXXX2D(Collider2D collision) {...}
private void OnTriggerXXX(Collider other) {
if (other.CompareTag("MyTag")) {
Destroy(other.gameObject);
}
}
Cast a ray from some point in some direction and return information on what was hit
void Foo() {
int mask = LayerMask.GetMask("MyCustomLayer", "Other");
float rayLength = 200;
Vector3 origin = ...;
Vector3 direction = ...;
if (Physics.Raycast(origin,
direction,
out RaycastHit hit,
rayLength,
mask,
QueryTriggerInteraction.Collide)) {
//...
}
}
void Foo() {
int mask = LayerMask.GetMask("MyCustomLayer", "Other");
float rayLength = 200;
Vector3 origin = ...;
Vector3 direction = ...;
RaycastHit2D hitInfo = Physics2D.Raycast(origin,
direction,
rayLength,
mask);
if (hitInfo.collider != null) {
//...
}
}
// Draw a line from start to start + dir in world coordinates
Debug.DrawRay(Vector3 start, Vector3 dir);
// Draw a line between specified start and end points
Debug.DrawLine(Vector3 start, Vector3 end);