🔥 How to Make a Game Like Lost War in Unity 6 | Full Beginner Tutorial (Step-by-Step) - Part 14
Автор: Tasty Cherry Games
Загружено: 2025-11-18
Просмотров: 10
Описание:
🎮 Unity Tutorial: Mastering OnTriggerEnter Without Physics! | Full Guide Explained | Unity C# Game Dev 🚀
⚙️ Step 1: Setting Up the Scene
We start with a simple scene — a cube that acts as the player character and two wall cubes (left and right). All objects have Box Colliders attached. The player also has a Rigidbody component, but we turn on the “Is Kinematic” property. This means Unity won’t apply gravity or physics forces to it.
When kinematic is on, the object won’t fall, won’t bounce, and won’t collide in the usual physics sense — it simply exists for us to move via code.
🧱 Step 2: Turning on “Is Trigger”
Next, we enable “Is Trigger” for both wall colliders. By doing this, instead of the objects physically pushing each other away, Unity will now detect overlap events through functions like OnTriggerEnter(), OnTriggerStay(), and OnTriggerExit().
This is super important because if you forget to enable trigger on at least one of the colliders, the OnTriggerEnter method won’t be called at all!
So rule of thumb:
At least one object must have “Is Trigger” enabled.
If both objects are just colliders without trigger, they’ll block each other only if physics is active.
🎮 Step 3: Testing Without Physics
We play the scene, move our cube character left and right, and notice something interesting. Even though physics is turned off, the OnTriggerEnter method still gets called — because the triggers are working at the collider level.
That means even without gravity or bouncing, we can still detect when the player overlaps a boundary, enemy, or any object.
🧠 Step 4: Adding Game Logic to Stop Movement
In real games, when the player hits a wall, we don’t want them to keep moving infinitely. So we introduce a boolean variable, something like:
public bool collidingWithObjects = false;
When the player hits a wall, OnTriggerEnter sets this bool to true. When the player leaves, OnTriggerExit will set it back to false.
Then in our Update() or movement script, we add a simple condition:
if (!collidingWithObjects)
{
// allow movement
}
else
{
// stop movement
}
This ensures smooth, manual control without Unity’s physics interfering.
💡 PRO TIPS
✅ Always remember that OnTriggerEnter needs at least one collider with “Is Trigger” checked.
✅ Kinematic Rigidbodies ignore normal physics, so you must control movement manually through script.
✅ You can still detect collisions via triggers even if no Rigidbody physics are used.
✅ Keep your movement script clean — use boolean flags to manage wall contact or overlapping logic.
✅ For debugging, use Debug.Log() inside OnTriggerEnter to print which collider was touched.
📜 SAMPLE CODE SNIPPET
using UnityEngine;
public class TriggerMovementControl : MonoBehaviour
{
public float moveSpeed = 5f;
public bool collidingWithObjects = false;
void Update()
{
if (!collidingWithObjects)
{
float move = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
transform.Translate(move, 0, 0);
}
}
void OnTriggerEnter(Collider other)
{
Debug.Log("Collided with: " + other.name);
collidingWithObjects = true;
}
void OnTriggerExit(Collider other)
{
Debug.Log("Left collision with: " + other.name);
collidingWithObjects = false;
}
}
This script moves your player left or right and freezes movement when colliding with trigger walls.
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
🌍 WHY DISABLE PHYSICS SOMETIMES?
Many beginner Unity devs wonder: Why not just keep physics on?
Well, physics can sometimes make your game behave unpredictably. For example:
Characters sliding or jittering against walls.
Floating or clipping issues when colliders overlap.
Performance drops in large worlds with many rigidbodies.
By controlling movement directly, you keep performance high and behavior consistent — especially in mobile or lightweight games.
🧠 KEY TAKEAWAYS
OnTriggerEnter works without physics, as long as one collider has “Is Trigger” enabled.
Kinematic Rigidbody disables physics forces, letting you move objects by script only.
Boolean flags help manage game logic like stopping movement or resuming it.
Manual movement control ensures precision and avoids physics-based randomness.
Unity’s built-in physics is optional, not mandatory — perfect for optimized gameplay.
📢 TAGS (Use these to boost discoverability!)
#UnityTutorial #Unity3D #GameDevelopment #UnityGameDev #UnityPhysics #OnTriggerEnter #UnityCSharp #UnityBeginner #GameDevTutorial #UnityTips #TastyCherryGames #UnityCoding #IndieGameDev #LearnUnity #UnityScripting #NoPhysicsUnity #ColliderTutorial #TriggerCollider #UnityOnTriggerEnter #UnityOnTriggerExit #UnityKinematic #UnityRigidbody #CSharpInUnity #GameDevCommunity #UnityBeginnerTutorial #MadeWithUnity #UnityLearning #UnityBasics #Unity2025 #UnityProgramming #UnityGameTutorial
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: