| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
- ํฌ๋ฅด๋น์ฉ
- ์์์ธ
- ๊ทธ๋์คํธํผ
- ์๊ฐ
- ์ค๋ฌผ์
- ํฌ์
- ์๊ณ ๋ฆฌ์ฆ
- ๊ธ์์ธ
- ์๊ณ
- ๋ผ์ด๋ ธ
- ์ค๋ฒ๋ฐ
- ์ ๋๋ฉ์ด์
- ์ค๋ฌผ๊ธ
- ์ํฌ์
- ๊ธ๊ฐ
- ์ํดํฌ
- ์ ๋ํฐ
- ๊ธ
- ๋ ๊ณ
- ๊ธํฌ์
- ๋ ๋๋ง
- ๊ธฐ์ ์ ๋ถ์
- ์ฐจํธ
- 3Dํ๋ฆฐํ
- ์ค๊ณ
- ์งญ์ค์คํค
- ๊ตญ์ ๊ธ์์ธ
- ๋ผ์ด๋ ธ3D
- grasshopper
- ์ ๋ง
- Today
- Total
Creative Engineer Lab - Archive
[Unity]๊ตฌ์ฒด ๋๋กญ ์๋ฎฌ๋ ์ด์ (Slide_drop) ๋ณธ๋ฌธ

๊ตฌ์ฒด์ Rigidbody๋ฅผ ๋ถ์ฌ, Collider๋ฅผ ๊ตฌ์ฒด์ ์ฌ๋ผ์ด๋์ ํ ๋นํ์ฌ ์๋ฎฌ๋ ์ด์ ์ ๊ตฌํํ์ต๋๋ค.
์ ๋ํฐ 3d ํ๋ก์ ํธ์์ ๋งค๊ฐ๋ณ์๋ ๋์ฒด๋ก ๊ธฐ๋ณธ๊ฐ์ผ๋ก๋ง ์ฌ์ฉํ๋๊ฒ์ด ๊ฐ์ฅ ์ข์ง๋ง
์ข ๋ ํ์ค๊ณผ ์ ์ฌํ๊ฒ ํ๊ณ ์ํ๋ค๋ฉด ์ธ๋ถ ์ปจํธ๋กค์ด ํ์ํ ์ ์๋ค.
rigidbody๋ ๊ธฐ๋ณธ์ ์ผ๋ก ํ ๋น ๋ ์ค๋ธ์ ํธ๊ฐ ๋ฌผ๋ฆฌ์์ง๊ณผ ์ํธ์์ฉ ํ๋๋ก ๋์ด์๋๋ฐ, Mass(์ง๋)๊ณผ Drag(๊ณต๊ธฐ ์ ํญ), Angular Drag(ํ์ ์ ํญ) ๊ฐ์ ๊ฐ์ ์กฐ์ ํ์ฌ ์ค์ ๋ฌผ๋ฆฌํ๊ฒฝ์ ๋ง์ถ ์ ์๋ค.
๋ฌผ์ฒด๊ฐ ์์ง์ด๋ ๋ฐฉ์์ ์ํฅ์ ๋ฏธ์น๊ธฐ ๋๋ฌธ์ ๋ฌผ๋ฆฌ ์์ง์ ์ ํ๋๋ฅผ ๋์ด๋ ค๋ฉด ์ง๋, ์๋, ์ค๋ ฅ ๋ฑ์ ์ธ๋ถ ์กฐ์ ์ด ํ์ํ๋ค.
Collider๋ ๊ธฐ๋ณธ์ ๋จ์ํ ํํ๋ก, ์ ํํ ์ถฉ๋์ ์ํด์๋ ์ค๋ธ์ ํธ ๋ชจ๋ธ๋ง์ ์ต์ ํํด์ผ ํ๋ค.
๋๋ฌด ๋ค๊ฐํ ๋ ๋ฉ์ฌ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ์ ๋ฐฉํด๋ฅผ ์ค ์๋ ์๊ณ , ๋๋ฌด ๋จ์ํ ๋ฉ์ฌ๋ ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์ง ๋ชปํ ์ ์๊ธฐ ๋๋ฌธ์ ์ ์ ํ ์ต์ ์กฐ์ ์ด ํ์ํ๋ค.
Box๋ Sphere, Cylinder ๊ฐ์ ๋จ์ํ ํํ์ ๊ฒฝ์ฐ Mesh Collider ๋ณด๋ค๋ Box Collider ๋ฑ์ ํ ๋นํ๋๊ฒ์ด ์ข์ ์ ์๋ค.
using UnityEngine;
public class SimulationController : MonoBehaviour
{
public GameObject ball; // ๊ตฌ์ฒด
private Rigidbody ballRigidbody;
private Vector3 initialPosition; // ๊ตฌ์ฒด์ ์ด๊ธฐ ์์น
private Quaternion initialRotation; // ๊ตฌ์ฒด์ ์ด๊ธฐ ํ์
private bool isPaused = false; // ์ผ์์ค์ง ์ํ
void Start()
{
if (ball != null)
{
ballRigidbody = ball.GetComponent<Rigidbody>();
// ์ด๊ธฐ ์์น์ ํ์ ์ ์ฅ
initialPosition = ball.transform.position;
initialRotation = ball.transform.rotation;
}
}
void Update()
{
// Spacebar๋ก Pause/Resume ํ ๊ธ
if (Input.GetKeyDown(KeyCode.Space))
{
TogglePause();
}
// Rํค๋ก Reset
if (Input.GetKeyDown(KeyCode.R))
{
ResetSimulation();
}
}
// Pause/Resume ๊ธฐ๋ฅ
public void TogglePause()
{
isPaused = !isPaused;
if (isPaused)
{
Time.timeScale = 0; // ์๊ฐ ํ๋ฆ ๋ฉ์ถค
if (ballRigidbody != null)
{
ballRigidbody.isKinematic = true; // ๋ฌผ๋ฆฌ ์ค์ง
}
}
else
{
Time.timeScale = 1; // ์๊ฐ ํ๋ฆ ์ฌ๊ฐ
if (ballRigidbody != null)
{
ballRigidbody.isKinematic = false; // ๋ฌผ๋ฆฌ ์ฌ๊ฐ
}
}
}
// Reset ๊ธฐ๋ฅ
public void ResetSimulation()
{
// ์๊ฐ ๋ฐ ๋ฌผ๋ฆฌ ์ด๊ธฐํ
Time.timeScale = 1;
isPaused = false;
if (ball != null)
{
// ๊ตฌ์ฒด๋ฅผ ์ด๊ธฐ ์์น์ ํ์ ์ผ๋ก ์ด๋
ball.transform.position = initialPosition;
ball.transform.rotation = initialRotation;
// Rigidbody ์๋ ์ด๊ธฐํ
if (ballRigidbody != null)
{
ballRigidbody.linearVelocity = Vector3.zero;
ballRigidbody.angularVelocity = Vector3.zero;
}
}
}
}
์ ์ ์๊ธ์
์คํ์ด์ค๋ฐ๋ก ์ผ์์ ์ง/์์ ์ ํ ์ ์๊ณ ,
R ๋ฒํผ์ผ๋ก Reset ์ ํ ์ ์๋ ์คํฌ๋ฆฝํธ์ด๋ค.

์คํฌ๋ฆฝํธ๋ฅผ ๋น ์ค๋ธ์ ํธ์ ํ ๋นํด์ฃผ๊ณ ๊ฐ๊ฐ Sphere๋ฅผ ์ฐ๊ฒฐํด์ฃผ๋ฉด ๋๋ค.
'Unity & C# ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ด๊ฐ ๊ฐ๋ฐํ ๊ฒ์ setup ํ์ผ. ์ ๋ฐ์ด๋ฌ์ค๋ก ๊ฐ์ง๋ ๊น? (1) | 2025.05.12 |
|---|---|
| ์ ๋ํฐ ๊ต๊ณผ์ ์ตํ๊ธฐ 1 (2) | 2024.11.25 |
| ChatGPT์ ํจ๊ป Unity๋ก FPS ๊ฒ์ ๊ฐ๋ฐํ๊ธฐ - ํ๋กค๋ก๊ทธ (0) | 2024.11.01 |