【U3D入门小白教程——代码篇】之三:常见脚本函数

个人学习第三章节:常见脚本
 

1.Awake( )与Start()

Awake( )在游戏创建时调用,用于设置游戏初始化的参数

Start( )在脚本被调用,执行在所有Update( )之前,用于赋值变量

2.Update( )与FixedUpdate()

Update( )用于非刚体对象的运动,诸如定时器,检测输入等,Update受制于机器性能帧率

FixedUpdate()用于刚体对象运动,不受制于机器性能,有固定调用间隔。

3.Vector3.Dot( )与Vector3.Cross( )

Vector3.Dot( )用于表示两个向量的乘积,乘积为-1则为反方向,为0则两个向量垂直

Vector3.Cross( )用于计算两个向量交叉乘积,乘积结果为垂直于两个向量的新的向量

4.transform.Translate( )与transform.Rotate( )

transform.Translate( )用于指定向量的位移

transform.Rotate( )用于选定向量为旋转中心(视线为向量方向)的逆时针旋转

这里z轴正向为transform.forward 负向为transform.back

y轴正向为transform.up 负向为transform.down

x轴正向为transform.left 负向为transform.right

5.transform.LookAt(target)

transform.LookAt(target)用于指定物体z轴始终指向target,用于camera类似于第一人称视角

6.Math.Lerp( )

举例:float result = Mathf.Lerp (3f, 5f, 0.5f); 得到结果为4

Vector3 from = new Vector3 (1f, 2f, 3f);

Vector3 to = new Vector3 (5f, 6f, 7f);

Vector3 result = Vector3.Lerp (from, to, 0.75f); 得到结果为(4,5,6)

补充SmoothDamp( currentPosition ,targetPosition ,ref Velocity ,smoothTime )

currentPosition:我们正在的位置

targetPosition:目标移动位置

ref Velocity:不用管,Vector3 Velocity = Vector3.zero;

smoothTime:移动所要时间 注意这里smoothTime = 3f 并不为 3s(感觉大概是)

用于随着时间的推移,逐渐将向量改变为所需的目标。

7.Destory( )

用于gameobject或者compoent的清除

Destory(target,3f) 目标将在3s后摧毁

8.Input.GetButton( )与Input.GetKey( )

当按下按键,GetButtonDown( )仅按下触发一次为True,其他为False

GetButtonUp( )仅谈起触发一次为True,其他为False

GetButton( )只要按键按下未弹起,即为True

GetKey( ) GetKeyDown( ) GetKeyUp( )用法一样

Input.GetKey( )设定按键在Unity窗口Edit/Project Settings/Input中

9.GetAxis( )

GetAxis( )类似于GetKey( ),设定按键在Unity窗口Edit/Project Settings/Input中

主要用法GetAxis(“Vertical”),GetAxis(“Horizontal”)

按下方向键正向,GetAxis( )数值从0到1增加,然后松开返回值为0

在Input设置中修改Gravity大小,决定了换向时候变化的速度,数值越大速度越快

修改Sensitivity大小,决定了按键从数值变化的快慢,数值越大速度越快

GetAxisRaw( )该值的范围为-1 ... 1,当GetAxis处理物体移动有明显跳帧问题,则使用此方法平滑移动

10.OnMouseDown( )

OnMouseDown( )用于鼠标点击事件

其他鼠标事件还有

OnMouseEnter( )当鼠标移动到对象上触发

OnMouseOver( )当鼠标停在对象上触发

OnMouseExit( )当鼠标移动到对象后离开触发

OnMouseDrag( )当鼠标到对象上按住触发

11.GetComponent<>( )

可以用这个方法获取其他游戏部件,或者是其他的脚本。

当获取其他组件元素时候,建议利用方法获取组件,然后target.getCompent<BoxCollider>( )

还有一种获取游戏部件方式

利用tag标签:GameObject.FindGameObjectsWithTag(“targetName”)

12.Time.deltaTime( )

Time.deltaTime( )返回值为调用Update( )方法的间隔时间,即为帧间隔时间,所以在特定情况下,

乘以Time.deltaTime( )可以实现按照每一帧更改的目的,达到画面的连贯性。

13.Instantiate( )

Instantiate(gameObject,target.transform.position,target.transform.rotation)

用于实例化对象在指定位置角度

Rigidbody rb = Instantiate(gameObject,target.transform.position,target.transform.rotation) as Rigidbody;

这样可以对实例化后的对象进行操作,比如增加受力。rb.AddForce(Vector3)

14.Invoke(“函数名称”,time)

用于等待time后进行函数调用

15.InvokeReapting( )

InvokeReapting(“函数名称”,time,durTime)

Time为调用延迟时间

durTime为重复调用间隔时间

想要结束重复,调用方法CancelInvote(“函数名称”)

16.yield return new WaitForSenconds(3f)

使用这个等待方法的函数需要加上IEnumerator修饰符

在调用时利用StartCoroutine(函数名称)启动

17.delegate void Function();

Function() function;

用于方法的叠加调用。
---------------------
作者:唐三十胖子
来源:CSDN
原文:https://blog.csdn.net/iceSony/article/details/77890260
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/hj558558/p/10080375.html