Transform类
2016-09-12 14:58
176 查看
Rotate 绕某一向量旋转
public void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self);public class ExampleClass : MonoBehaviour { void Update() { transform.Rotate(Vector3.right * Time.deltaTime); transform.Rotate(Vector3.up * Time.deltaTime, Space.World); } }
public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);
public class ExampleClass : MonoBehaviour { void Update() { transform.Rotate(Time.deltaTime, 0, 0); transform.Rotate(0, Time.deltaTime, 0, Space.World); } }
public void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);
public class ExampleClass : MonoBehaviour { void Update() { transform.Rotate(Vector3.right, Time.deltaTime); transform.Rotate(Vector3.up, Time.deltaTime, Space.World); } }其中第二个参数 Space.World表示绕世界坐标轴旋转,Space.Self表示绕自身坐标轴旋转
我们来看一下第二个参数的效果
创建一个Cube,随意旋转一下Cube,让Cube的坐标轴与世界坐标轴不同
创建脚本t_Rotate.cs
首先看一下Space.Self效果
代码
public class t_Rotate : MonoBehaviour { void Update() { transform.Rotate(Vector3.up * Time.deltaTime * 20, Space.Self); } }
效果
我们看到是围绕自身的Y坐标轴旋转的
下面看一下Space.World的效果
代码
public class t_Rotate : MonoBehaviour { void Update() { transform.Rotate(Vector3.up * Time.deltaTime * 20, Space.World); } }效果
我们看到是围绕世界坐标系中的Y轴旋转的。
RotateAround 绕某一点的某个轴旋转
public void RotateAround(Vector3 point, Vector3 axis, float angle);功能:绕point点的axis轴旋转
例子:
假如我们想使Cube绕Sphere物体的Vector3(0,1,1)轴旋转
代码:
public class t_RotateAround : MonoBehaviour { public Transform target; void Update () { transform.RotateAround(target.position, new Vector3(0, 1, 1), 20 * Time.deltaTime); } }效果:
LookAt 朝向某一物体或者是世界中的某一点
public void LookAt(Transform target, Vector3 worldUp = Vector3.up);public void LookAt(Vector3 worldPosition, Vector3 worldUp = Vector3.up);
例子:我们在RotateAround例子中加入一句话使Cube的前方始终朝向Sphere
代码:
public class t_RotateAround : MonoBehaviour { public Transform target; void Update () { transform.RotateAround(target.position, new Vector3(0, 1, 1), 20 * Time.deltaTime); //使Cube的前方(在这里是Z轴)始终朝向Sphere transform.LookAt(target); } }
效果
TransformDirection 将向量从局部坐标系转换到世界坐标系
public Vector3 TransformDirection(Vector3 direction);该转换不受物体缩放和位置的影响
TransformPoint 将点从局部坐标系转换到世界坐标系
public Vector3 TransformPoint(Vector3 position);返回点在世界坐标系下的位置,受到物体缩放的影响
InverseTransformPoint 将点从世界坐标系转换到局部坐标系
public Vector3 InverseTransformPoint(Vector3 position);返回点在局部坐标系下的位置,受到物体缩放的影响
Translate
public void Translate(Vector3 translation, Space relativeTo = Space.Self);public void Translate(float x, float y, float z, Space relativeTo = Space.Self);
相对坐标系移动
[b]其中第二个参数 Space.World表示相对世界坐标系移动,Space.Self表示相对自身坐标系移动
[/b]
[b]public void Translate(Vector3 translation, Transform relativeTo);
public void Translate(float x, float y, float z, Transform relativeTo);
[/b]
[b]相对物体[b]relativeTo的坐标系移动[/b][/b]
lossyScale 和 LocalScale
localscale 本地坐标系的大小,也就是检视面板显示的数值lossyscale 世界坐标系的大小,没有父物体时检视面板显示的数值,也就是相对于世界的实际大小
这两个的区别相当于localPosition和position的区别
相关文章推荐
- unity实现多点触控代码
- 在Unity中实现动画的正反播放代码
- Unity UGUI教程之实现滑页效果
- unity实现摄像头跟随
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- Unity3D-5.3.5发布VR项目到Android
- unity 新UI草稿
- [软件资讯]Unity已支持将3D游戏导出成Flash
- ShaderLab: Pass
- Unity 武器拖尾效果
- unity常见问题之20题
- Unity3D与JavaWeb服务器传递数据和文件 ---建立Java服务器
- unity动态加载资源并缓存到本地
- Unity3D动画存储插件
- unity 赛车相机跟随物体移动
- uLua中遇到的问题