模型的中心点及模型的旋转

模型中心点
说明
模型身上的坐标轴中心点,也就是我们模型的中心点。
模型的位置,旋转。缩放都是相对于模型的中心点来进行变化的。
如何改变模型中心点
创建一个空物体,创建父子关系,通过父物体来控制子物体。
也就间接的改变了模型的中心点。
实例
模型的中心点是无法改变的,要实现门的旋转需要创建一个空物体作为门的父级,再将空物体移动到合适的位置作为中心点。
通过空物体的旋转带动子物体门的旋转。
中心点工具
Center:当选中两个模型的时候,设置为“Center”模型组的中心点就在两个模型的中间中心位置。
Pivot:当选中两个模型的时候,设置为“Pivot”,模型组的中心点就在后选中的模型的中心点位置。
tip:在Unity中按Ctrl键进行物体的加选。


使用键盘按键实现开关门
使用Transform.Rotate(Vector3.float)旋转模型:
Vector3:沿某个轴向旋转
Float:旋转的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| using System.Collections; using System.Collections.Generic; using UnityEngine; public class door : MonoBehaviour { private Transform m_transform; private bool isclose; // Use this for initialization void Start () { //获取变换组件 m_transform = gameObject.GetComponent<Transform>(); isclose = true; } // Update is called once per frame void Update () { } //角色在门的触发器范围内 private void OnTriggerStay(Collider other) { //按下z键 if (Input.GetKeyDown(KeyCode.Z)) { //如果门是关着的就打开 if (isclose) { Opendoor(); isclose = false; } //如果门是开着的就关闭 else { Closedoor();isclose = true; } } } //开门方法 void Opendoor() { m_transform.Rotate(Vector3.up, 90); Debug.Log("kai"); } //关门方法 void Closedoor() { m_transform.Rotate(Vector3.up, -90); Debug.Log("guan"); } }
|
通过名称查找模型
Gameobject静态类的静态方法Find(String)
String:需要查找的模型名称
返回值:gameobject对象