Unity-3d Day04

  东西比较多  比较杂 还有数学知识  我日~~~

MonoBehaviour类:MonoBehaviour 表⽰⼀个单⼀的⾏为。Unity中⽤户对游戏对象的操作被分割成若干个
    单⼀⾏为,每个单⼀⾏为都作为⼀MonoBehaviour类来封装。继承⾃MonoBehaviour的类,不需要⾃⼰创建它
    的实例,也不能⾃⼰创建(如 new 类名)。因为所有从MonoBehaviour继承过来的类,unity都会⾃动创建实例,并且调⽤被重载的⽅
    法,如我们经常⽤到的Awake,Start, Update等。⽽普通类,可以⽤new来创建实例了。

MonoBehaviour类继承关系:

MonoBehavior类常⽤⽅法:

  Awake() 当⼀个脚本实例被载⼊时Awake被调⽤。
  Start() Start仅在Update函数第⼀次被调⽤前调⽤。
  OnEnable() 当对象变为可⽤或激活状态时此函数被调⽤。
  Update() 当MonoBehaviour启⽤时,其Update在每⼀帧被调⽤。
  FixedUpdate() 每⼀帧都在调⽤此⽅法。当处理Rigdibody时
  FixedUpdate应该代替Update⽅法
  LateUpdate() 每⼀帧都在调⽤此⽅法。当每⼀帧的Update⽅法全部执⾏完后,此⽅法开始被调⽤

  OnGUI() 渲染和处理GUI事件时调⽤。
  OnDisable() 当对象变为不可⽤或⾮激活状态时此函数被调⽤。
  OnDestroy() 当MonoBehaviour将被销毁时,这个函数被调⽤。

Transform类:

Transform组件作⽤:
  1. 负责游戏对象的变换
  2. 维持⽗⼦关系

Transform组件变量:
  position 在世界坐标系中,transform的位置
  localPosition 相对于⽗级的变换的位置
  eulerAngles 旋转作为欧拉⾓度
  localEulerAngles 相对于⽗级的变换的旋转欧拉⾓度
  rotation 在世界坐标系中物体变换的旋转⾓度作为
  Quaternion储存
  parent 返回物体变换的⽗级
  root 返回最⾼层次的

Transform组件⽅法:
  Translate 移动⼀个transform
  Rotate  eulerAngles.z度围绕z轴
      eulerAngles.x度围绕x轴
      eulerAngles.y度围绕y轴
  RotateAround 按照多少⾓度在世界坐标系中的某位置轴旋转物体
  LookAt 向前向量指向⺫标的当前位置,旋转物体
  Find 通过名字查找物体并返回,返回值类型为transform

Quaternion(四元数):

  operator * 四元数的叉乘,可⽤来连接两个旋转属性
  identity 该四元数没有旋转
  eulerAngles 返回表⽰旋转的欧拉⾓度⽅法
  Euler 返回⼀个旋转⾓度,绕x轴旋转x度,绕y轴旋转y度,绕z轴旋转z度

Vector3 (三维向量):
  变量:  normalized  magnitude  sqrtMagnitude
  ⽅法:Normalize  Cross  Dot  Project  Distance  Lerp  Slerp


Time类:

time 从游戏开始到现在的所⽤时间,是以秒计算的

deltaTime 获取上⼀次Update()⽅法执⾏的时间到当次执⾏
Update()⽅法时间的差值fixedDeltaTime 在物理和其他固定帧速率进⾏更新上⼀帧所消耗的时间,以秒计算
timeScale⽤于减缓运动效果。当timeScale传递时间为1.0时和实时⼀样;当timeScale传递时间0.5时⽐实时时间慢⼀半;当timeScale传递时间为0时游戏基本暂停

Mathf:

  Lerp 两个浮点数之间进⾏插值
  Clamp 返回⼀个限制值
  Sin 计算并返回浮点型的正弦值
  Cos 计算并返回浮点型的余弦值
  Abs 计算并返回指定参数的绝对值
  Max 返回两个值之中最⼤的值
  Min 返回两个值之中最⼩的值
  Sqr 返回平⽅根
  PI 圆周率

数学方面的就不写了  全都是各种向量啊什么的。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class QuatScript : MonoBehaviour {
    //public int x;
    //public int y;
    //public int z;
    //public Transform cylinder;
    //public Transform sphere;

    public Transform time;
    public Transform deltaTime;
    public Transform fixedDeltaTime;
    public Transform timeScale;
    float lerp = 0;

    // Use this for initialization
    void Start () {
        //sphere = transform.Find("Sphere");
    }
    
    // Update is called once per frame
    void Update () {

        //float rad = Mathf.PI / 2;
        //float xh = Mathf.Sin(rad / 2) * x;
        //float yh = Mathf.Sin(rad / 2) * y;
        //float zh = Mathf.Sin(rad / 2) * z;
        //float wh = Mathf.Cos(rad / 2);

        //Quaternion q = new Quaternion(xh, yh, zh, wh);
        ////欧拉角转化为四元数
        //Quaternion q = Quaternion.Euler(90f,0f,0f);
        ////四元数转化欧拉角
        //Quaternion q = Quaternion.Euler(90f, 0f, 0f);
        ////输出四元数
        //transform.rotation = q;
        ////转                    中心点         沿x轴转,rignt,up,foward    速度
        //transform.RotateAround(new Vector3(1,1,1),Vector3.up,80*Time.deltaTime);

        //float hor = Input.GetAxis("Horizontal");
        //float ver = Input.GetAxis("Vertical");
        //transform.position += Vector3.forward * ver;
        //transform.position += Vector3.right * hor;

        //transform.LookAt(cylinder);
        //sphere.transform.RotateAround(cylinder.position, Vector3.up, 80 * Time.deltaTime);


        time.GetComponent<Text>().text = Time.time.ToString();
        deltaTime.GetComponent<Text>().text = Time.deltaTime.ToString();
        fixedDeltaTime.GetComponent<Text>().text = Time.fixedDeltaTime.ToString();
        timeScale.GetComponent<Text>().text = Time.timeScale.ToString();

        
        //lerp = Mathf.Lerp(lerp, 100f, 0.5f);


    }

}

今天的代码没怎么整理 不过全都在上面了

原文地址:https://www.cnblogs.com/little-sun/p/4370336.html