我的Unity3D学习日记-04(Transform,Time,Mathf类以及动态在场景中生成Prefab)

来源:互联网 发布:多开分身软件 编辑:程序博客网 时间:2024/06/10 20:57

//直接小写transform表示获取当前脚本所挂载的游戏对象身上的Transform组件

  Vector3 p= transform.position;

       //获取当前对象的位置存储到p

       //transform.localPosition; 获取局部坐标系位置

       print(p);

       //获取当前游戏对象的rotation

       //transform.rotation;旋转属性

       //transform.localRotation;局部坐标系中的旋转属性

       //rotation直接返回的数为四元数,而非vector3

       //transform.localScale; 获取对象的缩放属性

 

//关于改变游戏的对象的transform属性

//transform.Translate(new Vector3(1,0,0));

//改变当前游戏对象的位置

//transform.Rotate(Vector3.up, 10f);

//旋转当前游戏对象

//transform.eulerAngles = new Vector3(0f,10f,0f);

//使用欧拉角进行旋转,赋予一个状态,重复调用无法再次旋转

 

//关于获取对象的父对象与子对象的Transform属性

       transform.parent; //获取对象的父对象的transform,能够更改

       transform.root; //获取对象的根对象(最外层的父对象)的transform,不能更改

       transform.Find("Cube");//通过参数来查找当前对象的子对象

 

//关于Time

t = Time.time; //从游戏开始到当前帧所用的时间(秒)

        dt = Time.deltaTime;//从上一帧到当前帧所用的时间

        ts = Time.timeScale;//表示时间流逝的快慢,默认为1

        //改为2表示时间加快为两倍,改为0表示时间停止,游戏暂停

        transform.Rotate(Vector3.up, Time.deltaTime * 30f);//每秒钟转30

 

//关于数学类Mathf

Mathf.Min();//求最小值

        Mathf.Max();//求最大值

        Mathf.Abs();//绝对值

        Mathf.Sin();//sin函数

        Mathf.PI;//pi

        Mathf.Sqrt();//求平方根


以下这段代码是在按下P键时能在场景中随机位置生成一个Prefab

public class NewPrefab : MonoBehaviour {    public GameObject prefab0;//通过公共字段获得一个预设体void Update () {        if (Input.GetKeyDown("p"))        {            Vector3 pos = new Vector3();            pos.y = 0.5f;            pos.x = Random.Range(-5f, 5f);            pos.z = Random.Range(-5f, 5f);            Instantiate(prefab0,pos,Quaternion.identity);//Quaternion.identity表示为空//也可以使用Quaternion.AngleAxis(,);来指定一个欧拉角//要获取到添加的游戏对象可使用以下方法:            //GameObject p= Instantiate(prefab0,pos,Quaternion.identity) as GameObject;            //使用Instantiate方法在场景中添加游戏对象,返回值类型为Object            //使用as关键字将返回值转换为GameObject类型        }}}

以下为实现效果:


0 0
原创粉丝点击