Transform.parent 父级

var parent : Transform

Description描述

The parent of the transform.

物体变换的父级。

Changing the parent will modify the parent-relative position, scale and rotation but keep the world space position, rotation and scale the same.

改变父级,将修改相对于父级的位置、缩放和旋转角度,但是保持和世界坐标的位置、旋转角度和缩放相同。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Transform cameraTransform = Camera.main.transform;
	public void Awake() {
		cameraTransform.parent = transform;
		cameraTransform.localPosition = -Vector3.forward * 5;
		cameraTransform.LookAt(transform);
	}
}
// Makes the camera follow this object by
// making it a child of this transform.
//使摄像机跟随物体,使它成为该变换的子物体
// Get the transform of the camera
//获取摄像机的变换
var cameraTransform = Camera.main.transform;

// make it a child of the current object
//使它成为当前物体的子物体
cameraTransform.parent = transform;

// place it behind the current object
//放它到当前物体的后面
cameraTransform.localPosition = -Vector3.forward * 5;

// make it point towards the object
//使它的指向物体。
cameraTransform.LookAt(transform);

另外一个例子:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public void Awake() {
		transform.parent = null;
	}
}
// Detaches the transform from its parent.
//从它的父级分类变换
transform.parent = null;
最后修改:2010年12月19日 Sunday 0:04

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。