4.23

113 阅读1分钟

方向的描述,欧拉角和四元数

//旋转:欧拉角、四元数
//欧拉角也用Vector3表示。
//沿y轴旋转30°
Vector3 rotate = new Vector3(0,30,0);
//无旋转四元数
Quaternion quaternion = Quaternion.identity;
//通过欧拉角创建四元数
quaternion = Quaternion.Euler(rotate);
//四元数转换成欧拉角
rotate = quaternion.eulerAngles;
//看向一个物体
quaternion = Quaternion.LookRotation(new Vector3(0,0,0));

常用调试方法

//输出普通信息
Debug.Log();
//警告输出
Debug.LogWarning("test2");
//错误输出
Debug.LogError();

//绘制一条线(起点,终点)
Debug.DrawLine(Vector3.zero,Vector3.one[,Color.color]);
//绘制一条射线(起点,射线)
Debug.DrawRay(Vector3.zero,Vector3.up[,Color.color]);

游戏物体GameObject的使用方法

//声明一个立方体,这种方法需要在组件脚本中将立方体拖拽放入
public GameObject Cube;


//拿到当前脚本所挂载的游戏物体
GameObject go = this.gameObject;
Debug.Log(go.name);
或
Debug.Log(gameObject,name);
//tag
Debug.Log(gameObject.tag);
//layer
Debug.Log(gameObject.layer);

//当前真正激活状态
Debug.Log(Cube.activeInHierrachy);
//当前自认激活状态
Debug.Log(Cube.activeSelf);
//获取Transform组件
Debug.Log.(transform.position);
//获取其他组件(这是一个泛型)
BoxCollider bc = GetComponent<BoxCollider>();
//获取当前物体子物体身上的某个组件
GetComponentInChildren<CapsuleCollider>(bc);
//获取当前物体父物体上的某个组件
GetComponentInParent<BoxCollider>();
//添加一个组件
Cube.AddComponent<AudioSource>();
//通过游戏对象名称获取游戏物体
GameObject game = GameObject.Find("GameObjectName");
    Debug.Log(game.name);
//通过游戏对象标签来获取游戏物体
GameObject = GameObject.FindWithTag("Enemy");