RequireComponent需要组件与Quaternion 四元数

来源:互联网 发布:4399淘宝旗舰店 编辑:程序博客网 时间:2024/06/06 16:36

1.第一人称人物身上应该有Rigidbody刚体组件和CapsuleCollider胶囊体组件,要判断有没有,如果没有要进行添加,

这里就用到了RequireComponent组件,[RequireComponent(typeof(Rigidbody))]

[RequireComponent(typeof(CapsuleCollider))]  

Inherits from Attribute

The RequireComponent attribute lets automatically add required component as a dependency.

RequireComponent 属性可以允许自动添加需要的组件作为一个附属。

个人理解为如果游戏对象身上没有这个组件 ,就会加上这个组件。

When you add a script which uses RequireComponent, the required component will automatically be added to the game object. This is useful to avoid setup errors. For example a script might require that a rigid body is always added to the same game object. Using  RequireComponen  this will be done automatically, thus you can never get the setup wrong.

当你添加的一个用了RequireComponent 组件的脚本,需要的组件将会自动被添加到game object(游戏物体)。这个可以有效的避免组装错误。举个例子一个脚本可能需要刚体总是被添加在相同的game object(游戏物体)上。用RequireComponent 属性的话,这个过程将被自动完成,因此你可以永远不会犯组装错误

2.Quaternion四元数

用于表示旋转。不会出现万向节锁并且能够很容易被插值。Unity中使用Quaternion表示所有旋转。

使用方法如下(部分代码):

private Transform  m_chaTrans;

private Quaternion quaternion;

quaternion = m_chaTrans.rotation;

quaternion *= Quaternion.Euler(0f,1f,0f);


欧拉角:

m_chaRotate = m_chaTrans.eulerAngles;

m_chaRotate += new Vector3(0f,1f,0f);







0 0