머리 위에 체력바를 표시하고 체력 감소를 표현하는 데는 2D 방식이 더 적합할 수 있습니다. 이유와 구현 방법은 다음과 같습니다:
구현 방법:
jsx
Copy
const HealthBar = ({ health }) => (
<div style={{
width: '100px',
height: '10px',
backgroundColor: 'gray',
position: 'absolute',
left: '50%',
top: '10px',
transform: 'translateX(-50%)'
}}>
<div style={{
width: `${health}%`,
height: '100%',
backgroundColor: 'red',
transition: 'width 0.3s'
}} />
</div>
);
jsx
Copy
import { Html } from '@react-three/drei'
function Character({ health }) {
return (
<group>
<mesh>
{/* 캐릭터 메시 */}
</mesh>
<Html>
<HealthBar health={health} />
</Html>
</group>
)
}
jsx
Copy
<Html
style={{
width: '100px',
height: '20px',
position: 'absolute',
top: '-30px',
left: '-50px',
fontSize: '12px',
color: 'white',
textAlign: 'center'
}}
transform
occlude
>
<HealthBar health={health} />
</Html>
jsx
Copy
const [health, setHealth] = useState(100);
// 체력 감소 함수
const decreaseHealth = (amount) => {
setHealth(prev => Math.max(0, prev - amount));
};
css
Copy
.health-bar {
transition: width 0.3s ease-in-out;
}
이 방식을 사용하면 3D 환경에 잘 통합되면서도 가독성 높고 반응이 빠른 2D 체력바를 구현할 수 있습니다. 또한 HTML과 CSS를 사용하므로 스타일링과 애니메이션 추가가 쉽습니다.