使用预制体创建实例对象后添加父节点,预制体坐标会自动转换为与未添加父节点前的相对坐标,此时添加父节点后应当更改其局部坐标
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public GameObject tempObject;
public GameObject CubeObject;
// Start is called before the first frame update
void Start()
{
GameObject temp = (GameObject)GameObject.Instantiate(tempObject);
temp.transform.SetParent(CubeObject.transform);
temp.transform.localPosition = Vector3.zero;
//temp.transform.position = Vector3.zero;//错误
}
// Update is called once per frame
void Update()
{
}
}
预制体坐标为(0,0,0)
未更改局部坐标时:
更改局部坐标后: