하이어라키에서 한 오브젝트를 다른 오브젝트의 자식으로 옮기는 코드
반드시 오브젝트의 transform을 사용해서 옮겨야함.
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] GameObject collisionVFX;
[SerializeField] Transform parent2;
void OnParticleCollision(GameObject other)
{
GameObject gg = Instantiate(collisionVFX, transform.position, Quaternion.identity);
gg.transform.parent = parent2;
}
}
예시 : 파티클에 충돌했을때 오브젝트의 계층을 옮김.
다른방법
using UnityEngine;
public class MakeChild : MonoBehaviour
{
// 자식으로 만들 오브젝트
public GameObject childObject;
// 부모 오브젝트
public GameObject parentObject;
void Start()
{
// 자식 오브젝트의 부모를 설정
childObject.transform.SetParent(parentObject.transform);
// 자식 오브젝트의 위치를 부모 오브젝트의 로컬 좌표계로 초기화하고 싶다면 이렇게 할 수 있습니다:
childObject.transform.localPosition = Vector3.zero;
childObject.transform.localRotation = Quaternion.identity;
childObject.transform.localScale = Vector3.one;
}
}
유니티에서는 gameObject.transform.SetParent()를 사용하는것을 추천함.
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] GameObject collisionVFX;
[SerializeField] GameObject parent;
[SerializeField] Transform parent2;
void OnParticleCollision(GameObject other)
{
GameObject gg = Instantiate(collisionVFX, transform.position, Quaternion.identity);
// 방법1
gg.transform.parent = parent2;
// 방법2
gg.transform.SetParent(parent.transform);
}
}
유니티 편집모드에서 오브젝트 이름 바꾸기 (0) | 2024.09.04 |
---|---|
유니티 싱글톤 (1) | 2024.09.02 |
콜라이더, 트리거 충돌표 (0) | 2024.08.30 |
유니티 팁 여러개 오브젝트 정렬하기 (0) | 2024.08.29 |
유니티 기본 C# 스크립트 내용 수정하기(start,update) (0) | 2024.08.29 |