Unity 常用脚本:Transform

Transform组件

Position, rotation and scale of an object.物体的位置、旋转、缩放。

场景中对象都有Transform组件(UGUI为RectTransform),此组件不可移除 。

游戏物体的父子关系是基于Transform组件建立起来的。

某一游戏物体没有父物体,则Transform显示其世界坐标中的属性;如果有父物体,则显示其相对于父物体的属性。

脚本API

脚本中直接写transform表示当前绑定此脚本的物体的Transform。

属性:

public Vector3 localPosition { get; set; }物体相对于父物体transform的位置。
public Vector3 eulerAngles { get; set; }物体在世界坐标系中以欧拉角为单位的旋转。
public Vector3 localEulerAngles { get; set; }物体相对于父物体坐标以欧拉角为单位的旋转。
public Vector3 right { get; set; }

物体在世界坐标系中的右方向(场景中红色轴正方向)。

public Vector3 up { get; set; }物体在世界坐标系中的上方向(场景中绿色轴正方向)。
public Vector3 forward { get; set; }物体在世界坐标系中的前方向(场景中蓝色轴正方向)。
public Quaternion rotation { get; set; }物体在世界坐标系中以四元数表示的旋转。
public Vector3 position { get; set; }物体在世界坐标系中的位置。
public Quaternion localRotation { get; set; }物体相对于父物体以四元数表示的旋转。
public Transform parent { get; set; }物体的父物体的Transform
public Matrix4x4 worldToLocalMatrix { get; }世界坐标系转换为局部坐标系的矩阵(只读)。
public Matrix4x4 localToWorldMatrix { get; }局部坐标系转换为世界坐标系的矩阵(只读)。
public Transform root { get; }层次结构中最顶层的根物体的Transform。
public int childCount { get; }物体的子物体的数量。
public Vector3 lossyScale { get; }物体的全局缩放比例。
public bool hasChanged { get; set; }物体Transform属性是否发生改变。
public Vector3 localScale { get; set; }物体相对于父物体的缩放。
public int hierarchyCapacity { get; set; }物体当前所在的树状层级结构的层次容量。即当前层级树最大可以容纳的节点数目,从整个树中的任意一个节点访问此属性,所获取的层次容量都是相同的。此属性是自动增长的,即当物体所在层级树发生层次变动(节点数增加)时,如果当前树的容量不足,会自动扩容。
public int hierarchyCount { get; }物体所处的相互关联的树状层级结构中,存在的层次(节点)的数目。

方法

public void DetachChildren();

摘要: Unparents all children.解除所有子物体。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
	void Start ()
    {
        transform.DetachChildren();
    }
}

  ---> 

public Transform Find(string name);

摘要:Finds a child by name and returns it. 按名字查找子元素并返回它。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
	void Start ()
    {
       Debug.Log( transform.Find("Sphere/Sphere (1)") );
    }
}

   --->  

public Transform GetChild(int index);

摘要:Returns a transform child by index.返回索引对应的子物体的Transform。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
	void Start ()
    {
       Debug.Log( transform.GetChild(1) );
    }
}

  --->  

public int GetSiblingIndex();

摘要: Gets the sibling index.获取同级索引。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
	void Start ()
    {
       Debug.Log( transform.GetSiblingIndex());
    }
}

public Vector3 InverseTransformDirection(Vector3 direction);

摘要:Transforms a direction from world space to local space. The opposite of Transform.TransformDirection.

将方向从世界坐标变换到自身坐标。和Transform.TransformDirection相反。 

public Vector3 InverseTransformDirection(float x, float y, float z);

摘要:Transforms the direction x, y, z from world space to local space. The opposite of Transform.TransformDirection.

将方向x, y, z从世界坐标变换到局部坐标。 

public Vector3 InverseTransformPoint(float x, float y, float z);

 摘要:Transforms the position x, y, z from world space to local space.

将位置x,y,z从世界坐标变换到局部坐标。

public Vector3 InverseTransformPoint(Vector3 position);

摘要: Transforms position from world space to local space.将位置从世界坐标转换为局部坐标。

public Vector3 InverseTransformVector(Vector3 vector);

摘要:  Transforms a vector from world space to local space. The opposite of Transform.TransformVector.

将向量从世界坐标变换到局部坐标。

public Vector3 InverseTransformVector(float x, float y, float z);

摘要:Transforms the vector x, y, z from world space to local space. The opposite of Transform.TransformVector.

将向量x,y,z从世界坐标变换到局部坐标。

 public bool IsChildOf(Transform parent);

 摘要:Is this transform a child of parent?判断物体是否为指定物体的子物体。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform cube;
    Transform sphere;
    Transform capsule;
    void Start ()
    {
        cube = GameObject.Find("Cube").transform;
        sphere = GameObject.Find("Sphere").transform;
        capsule = GameObject.Find("Capsule").transform;

        Debug.Log(sphere.IsChildOf(cube));
        Debug.Log(cube.IsChildOf(sphere));
        Debug.Log(capsule.IsChildOf(cube));
        Debug.Log(capsule.IsChildOf(sphere));
    }
}

---> 

public void LookAt(Transform target);

摘要:Rotates the transform so the forward vector points at target's current position.

旋转变换,使正向向量指向目标的当前位置。使物体朝向指定物体。

(重载暂略)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform cube;
    Transform sphere;
    void Start ()
    {
        cube = GameObject.Find("Cube").transform;
        sphere = GameObject.Find("Sphere").transform;
        cube.LookAt(sphere);
    }
}

    --->   

public void Rotate(Vector3 eulerAngles);

 摘要:使物体旋转到指定角度。

(重载暂略)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform cube;
    void Start ()
    {
        cube = GameObject.Find("Cube").transform;
        cube.Rotate(new Vector3(0,0,30));          
    }
}

  ---> 

public void RotateAround(Vector3 point, Vector3 axis, float angle); 

 摘要:使物体围绕另一物体公转。

(重载暂略)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform cube;
    void Start ()
    {
        cube = GameObject.Find("Cube").transform;       
    }

    void Update()
    {
        cube.RotateAround(Vector3.zero, Vector3.up, 5);//参数依次代表:围绕的坐标位置,围绕旋转的轴,旋转的角度
    }
}

public void SetAsFirstSibling();

 摘要:Move the transform to the start of the local transform list.将物体作为同层开始物体。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform sphere3;
    void Start ()
    {
        sphere3 = GameObject.Find("Sphere (3)").transform;
        sphere3.SetAsFirstSibling();
    }
}

  --->  

public void SetAsLastSibling();

摘要:Move the transform to the end of the local transform list.将物体作为同层结尾物体。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform sphere;
    void Start ()
    {
        sphere = GameObject.Find("Sphere").transform;
        sphere.SetAsLastSibling();
    }
}

  --->  

public void SetParent(Transform parent);

摘要: Set the parent of the transform. 为物体设置父物体。

(重载暂略)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform cube;
    Transform sphere;
    void Start ()
    {
        cube = GameObject.Find("Cube").transform;
        sphere = GameObject.Find("Sphere").transform;
        sphere.SetParent(cube);
    }
}

  --->  

public void SetPositionAndRotation(Vector3 position, Quaternion rotation);

摘要: Sets the world space position and rotation of the Transform component.同时设置物体世界坐标位置与旋转。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform cube;
    void Start ()
    {
        cube = GameObject.Find("Cube").transform;
        cube.SetPositionAndRotation(new Vector3(0,0,10), Quaternion.Euler(new Vector3(30,0,0)));
    }
}

 ---> 

public void SetSiblingIndex(int index);

摘要:Sets the sibling index. 设置同层索引。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform cube;
    Transform sphere;
    void Start ()
    {
        cube = GameObject.Find("Cube").transform;
        sphere = GameObject.Find("Sphere").transform;
        sphere.SetSiblingIndex(2);
    }
}

---> 

public Vector3 TransformDirection(float x, float y, float z);

摘要:Transforms direction x, y, z from local space to world space.

将方向x, y, z从局部坐标变换到世界坐标。 

public Vector3 TransformDirection(Vector3 direction);

 摘要:Transforms direction from local space to world space.

将方向从局部坐标变换到世界坐标。 

public Vector3 TransformPoint(float x, float y, float z);

 摘要:Transforms the position x, y, z from local space to world space.

将位置x, y, z从局部坐标变换到世界坐标。 

public Vector3 TransformPoint(Vector3 position);

 摘要:Transforms position from local space to world space.

将位置从局部坐标变换到世界坐标。

public Vector3 TransformVector(float x, float y, float z);

 摘要:Transforms vector x, y, z from local space to world space.

将向量x, y, z从局部坐标变换到世界坐标。 

public Vector3 TransformVector(Vector3 vector);

摘要:Transforms vector from local space to world space.

将向量从局部坐标变换到世界坐标。 

 public void Translate(Vector3 translation);

摘要:Moves the transform in the direction and distance of translation.移动物体

(重载暂略)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    Transform cube;
    void Start ()
    {
        cube = GameObject.Find("Cube").transform;
        cube.Translate(new Vector3(10, 10, 10));
    }
}

  --->  

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值