Rigidbody and Collider Concepts in Unity

 

Hello everyone,

Today, I wanted to explain two concepts that should be learnt in the first place when starting Unity to new game development friends like me. After opening an empty project and adding that famous cube (in all the tutorials I saw, the teaching started by adding a cube :), the first thing to do is usually adding Rigidbody to the cube. So let’s start with this.

What is Rigidbody?

What is Rigidbody? Let’s examine this under this title. Rigidbody is a component used in Unity to give an object physical behaviour. Adding a Rigidbody to an object means allowing that object to be managed by the physics engine. So what is a physics engine? The physics engine simulates the movement and interaction of game objects in the physical world. This ensures that the object has physical properties such as mass, gravity, speed, force, and moves in accordance with the laws of physics. It also allows realistic motion and collision calculations. So it is very necessary for games.

What are the Basic Properties of Rigidbody?

Let’s examine the basic properties that appear in the inspector window when we add Rigidbody.

Rigidbody Inspector
  • Mass: Determines the mass of the object. Mass affects the resistance of the object against forces. Since the value we will write here means the mass of the object, objects with higher mass require more force.
  • Drag (Friction): It is the resistance force against the movement of the object. The higher the value we write here, the slower the object moves.
  • Angular Drag (Angular Friction): It is the resistance force against the rotational movement of the object.
  • Use Gravity: Enabling this option will cause the object to be under the influence of gravity, and if it is high up, it will fall downwards when the game is started. If there is no object to stop it when it falls, it will disappear from the game scene, but its presence will remain in the game system.
  • Is Kinematic: When this option is enabled, the object is not affected by the physics engine, but can move when it hits other objects. Kinematic objects are usually controlled programmatically.

What can we do using rigidbody?

Under this heading, let’s examine what we can do using the rigidbody feature.

  • Moving objects by applying force and torque. Thanks to this application, objects can fall faster, roll when falling or different reactions can occur.
  • Letting objects free fall. An object positioned higher than the ground can fall downwards when the game is started and stop when it hits the ground, or if the ground does not have a collider feature, it can pass through the ground and continue travelling in space.
  • Setting objects to react physically after collision. Post-collision disappearance function or any animation and effect can be added.

What is Collider?

Another feature that has been used in Unity since the beginning is collider. It is a component used to determine the presence of an object in the physical world. It can be called like a 3D frame placed around it. Adding Collider to an object allows that object to react to collisions. The Collider component can be of various types depending on the shape of the object. Now let’s look at which types it can be according to its shape.

What are Collider Types?

  • Box Collider: It is used for rectangular prism-shaped objects. It has a simple box shape.
  • Sphere Collider: Used for sphere-shaped objects.
  • Capsule Collider: Used for objects in the shape of a capsule (two hemispheres and a cylinder).
  • Mesh Collider: Used for complex shaped objects. It creates the collision area using the mesh structure of the object. Generally suitable for static (stationary) objects.
  • Terrain Collider: Used for terrain objects.

What are the Basic Properties of Collider?

Let’s examine the basic properties that appear in the inspector window when we add the Collider.

Collider Inspector
  • Is Trigger: When this option is enabled, Collider acts as a trigger instead of a collision. It initiates trigger events instead of physically interacting with other objects.
  • Material: A Physic Material is used to add physical properties to the Collider. This determines properties such as friction and flexibility.

What can we do using Collider?

Under this heading, let’s examine what we can do using the collider feature.

  • We can make objects collide with each other and give physical reactions. For example, when there is a collision, the objects go back separately or an animation like an explosion effect.
  • Enabling objects to initiate trigger events when they enter certain areas. For example, making an object disappear when it enters the collider area of another object.

Since we know both components separately, let’s finally examine how they can be used together.

Using Rigidbody and Collider Together

Adding both Rigidbody and Collider to an object allows us to fully manage the object’s interactions in the physical world. Since this management is a very important part of the game development process, learning these two components can make us a better game developer to begin with.

Rigidbody manages the physical movements and forces of the object, while Collider manages the collisions and trigger events of the object with other objects.

For example, let’s take a ball dropping and rolling on the ground in a game scene:

    • By adding a Rigidbody to the ball, we can ensure that it remains under the influence of gravity and moves with physical forces, i.e. rolls realistically.
  • At the same time, by adding a Sphere Collider to the ball, we can manage its collisions with other objects and add an effect or other triggered event.

In this way, the Rigidbody and Collider components work together to realistically simulate the physical behaviour and interactions of the object.

In this article, I tried to explain the importance of Rigidbody and Collider by talking about what it is. I hope it was useful.

Good coding to everyone.

Selin.

Unity’de Rigidbody ve Collider Kavramları

 

Herkese merhaba,

Bugün benim gibi oyun geliştirmeye yeni girişmiş arkadaşlara Unity’ye başlarken ilk sıralarda öğrenilmesi gereken iki kavramı olanca basitliğiyle anlatmak istedim. Boş bir proje açıp o meşhur küpü (gördüğüm tüm eğitimlerin genelinde öğretime küp eklenerek başlanıyordu :) ekledikten sonra yapılan ilk şey genelde küpe Rigidbody eklemek oluyor. Bu yüzden bununla başlayalım.

Rigidbody Nedir?

Rigidbody nedir? Bunu bu başlık altında inceleyelim. Rigidbody Unity’de bir nesneye fiziksel davranışlar kazandırmak için kullanılan bir bileşen. Bir nesneye Rigidbody eklemek, o nesnenin fizik motoru tarafından yönetilmesini sağlamak demek. Peki fizik motoru ne? Fizik motoru, oyun nesnelerinin fiziksel dünyadaki hareketlerini ve etkileşimlerini simüle ediyor. Bu da nesnenin kütle, yerçekimi, hız, kuvvet gibi fiziksel özelliklere sahip olmasını ve fizik kurallarına uygun hareket etmesini sağlar. Aynı zamanda gerçekçi hareket ve çarpışma hesaplamaları yapma olanağı sağlıyor. Yani oyunlar için çok lazım.

Rigidbody’nin Temel Özellikleri Neler?

Rigidbody’yi eklediğimizde inspector penceresinde görünen temel özelliklerini inceleyelim.

Rigidbody Inspector
  • Mass (Kütle): Nesnenin kütlesini belirler. Kütle, nesnenin kuvvetlere karşı gösterdiği direnci etkiler. Buraya yazacağımız değer nesnenin kütlesi demek olduğundan, daha yüksek kütleli nesneler, daha fazla kuvvet gerektirir.
  • Drag (Sürtünme): Nesnenin hareketine karşı olan direnç kuvvetidir. Buraya yazacağımız değer ne kadar yüksek olursa, nesne o kadar daha yavaş hareket eder.
  • Angular Drag (Açısal Sürtünme): Nesnenin dönüş hareketine karşı olan direnç kuvvetidir.
  • Use Gravity (Yerçekimi Kullan): Bu seçeneği etkinleştirmek, nesnenin yerçekimi etkisi altında kalmasını, eğer yüksekteyse oyun başlatıldığında aşağı doğru düşmesini sağlar. Düştüğünde durabileceği bir nesne bulunmuyorsa oyun sahnesinde gözden kaybolur ancak varlığı oyunun sisteminde bulunmaya devam eder.
  • Is Kinematic: Bu seçenek etkinleştirildiğinde, nesne fizik motoru tarafından etkilenmez, ancak diğer nesnelere çarptığında hareket edebilir. Kinematic nesneler genellikle programatik olarak kontrol edilir.

Rigidbody kullanarak neler yapabiliriz?

Bu başlık altında rigidbody özelliği kullanarak neler yapabiliriz, bunu inceleyelim.

  • Kuvvet ve tork uygulayarak nesneleri hareket ettirmek. Bu uygulama sayesinde nesneler daha hızlı düşebilir, düştüğünde yuvarlanma veya daha farklı tepkiler oluşabilir.
  • Nesneleri serbest düşüşe bırakmak. Zeminden daha yukarıda konumlandırılmış bir nesne oyun başlatıldığında aşağı doğru düşerek zemine çarptığında durabilir veya zeminin bir collider özelliği yok ise zemin içerisinden geçip boşlukta yolculuk etmeye devam edebilir.
  • Nesneleri çarpışma sonrası fiziksel tepkiler verecek şekilde ayarlamak. Çarpışma sonrası yok olma fonksiyonu veya herhangi bir animasyon ile efekt eklenmesi sağlanabilir.

Collider Nedir?

Unity’de ilk başlardan itibaren en çok kullanılan bir diğer özellikse collider. O da bir nesnenin fiziksel dünyadaki varlığını belirlemek için kullanılan bir bileşen. Etrafına konan 3 boyutlu çerçeve gibi denebilir. Bir nesneye Collider eklemek, o nesnenin çarpışmalara tepki verebilmesini sağlar. Collider bileşeni, nesnenin şekline bağlı olarak çeşitli türlerde olabilir. Şimdi şekillerine göre hangi türlerde olabilir, buna bakalım.

Collider Türleri Nelerdir?

  1. Box Collider: Dikdörtgen prizma şeklindeki nesneler için kullanılır. Basit kutu şekline sahiptir.
  2. Sphere Collider: Küre şeklindeki nesneler için kullanılır.
  3. Capsule Collider: Kapsül (iki yarım küre ve silindir) şeklindeki nesneler için kullanılır.
  4. Mesh Collider: Karmaşık şekilli nesneler için kullanılır. Nesnenin mesh (ağ) yapısını kullanarak çarpışma alanını oluşturur. Genellikle statik (hareketsiz) nesneler için uygundur.
  5. Terrain Collider: Arazi nesneleri için kullanılır.

Collider’ın Temel Özellikleri Nelerdir?

Collider’ı eklediğimizde inspector penceresinde görünen temel özelliklerini inceleyelim.

Collider Inspector
  • Is Trigger: Bu seçenek etkinleştirildiğinde, Collider çarpışma yerine tetikleyici olarak davranır. Diğer nesnelerle fiziksel etkileşime girmek yerine tetikleme olaylarını başlatır.
  • Material: Collider’a fiziksel özellikler eklemek için bir Physic Material kullanılır. Bu, sürtünme ve esneklik gibi özellikleri belirler.

Collider kullanarak neler yapabiliriz?

Bu başlık altında collider özelliği kullanarak neler yapabiliriz, bunu inceleyelim.

  • Nesnelerin birbirine çarpmasını ve fiziksel tepkiler vermesini sağlayabiliriz. Örneğin çarpışma olduğunda nesnelerin ayrı ayrı geri gitmesi ya da patlama efekti gibi bir animasyon çalışması.
  • Nesnelerin belirli alanlara girdiğinde tetikleyici olaylar başlatmasını sağlamak. Örneğin bir nesne diğer bir nesnenin collider alanına girdiğinde yok olmasını sağlamak gibi.

Her iki bileşeni de ayrı ayrı tanıdığımıza göre birlikte kullanımları nasıl olur, son olarak bunu inceleyelim.

Rigidbody ve Collider’ın Birlikte Kullanımı

Bir nesneye hem Rigidbody hem de Collider eklemek, nesnenin fiziksel dünyadaki etkileşimlerini tam anlamıyla yönetmemizi sağlar. Bu yönetim oyun geliştirme sürecinin çok önemli bir parçası olduğu için bu iki bileşeni öğrenmek bizi başlangıç için daha iyi bir oyun geliştiricisi yapabilir.

Rigidbody, nesnenin fiziksel hareketlerini ve kuvvetlerini yönetirken, Collider, nesnenin diğer nesnelerle çarpışmalarını ve tetikleyici olaylarını yönetir.

Örneğin, bir oyun sahnesinde bir topun yere düşmesini ve yuvarlanmasını ele alalım:

  • Topa bir Rigidbody ekleyerek onun yerçekimi etkisi altında kalmasını ve fiziksel kuvvetlerle hareket etmesini yani gerçekçi bir şekilde yuvarlanmasını sağlayabiliriz.
  • Aynı zamanda topa bir Sphere Collider ekleyerek onun diğer nesnelerle çarpışmalarını yönetir, varsa efekt veya tetiklenen başka bir olay ekleyebiliriz.

Bu şekilde, Rigidbody ve Collider bileşenleri birlikte çalışarak nesnenin fiziksel davranışlarını ve etkileşimlerini gerçekçi bir şekilde simüle eder.

Bu yazımda sizlere Rigidbody ve Collider’ın ne olduğundan bahsederek önemini anlatmaya çalıştım. Umarım faydalı olmuştur.

Herkese iyi kodlamalar.

Selin.