Unity学习笔记

来源:互联网 发布:手机淘宝个人中心在哪 编辑:程序博客网 时间:2024/06/06 01:50

一.碰撞

  1.A和B发生碰撞的最低要求是:A和B都要有一个collider,A和B至少有一个rigidbody组件.
  2.Is Trigger属性
    (0)官方解释:If enabled, this Collider is used for triggering events, and is ignored by the physics engine.
    (1)当没有勾选Is Trigger属性时,发生碰撞将调用:OnCollisionEnter.  
    (2)假如A勾选了Is Trigger属性时,A和B接触的时候将直接穿透,调用函数:OnTriggerEnter. 
    (穿透原因:根据官方解释,勾选了Is Trigge,那么这个物体将会被物理引擎所忽略掉,所以也就不会发生碰撞了)

二.Rigidbody组件

  1.作用
 Rigidbodies enable your GameObjects to act under the control of physics. The Rigidbody can receive forces and torque to make    your objects move in a realistic way. Any GameObject must contain a Rigidbody to be influenced by gravity, act under added forces  via scripting, or interact with other objects through the NVIDIA PhysX physics engine.
  2.要调用addforce函数或者添加Constant Force组件到一个GameObject,那么这个GameObject必须要包含Rigidbody组件.


三.RayCast函数

  1.Physics.Raycast(...)
  发出一条射线,如果和场景中的collider接触了,那么该函数返回true,否则返回false.其中这个函数可以返回hit的具体信息.

  2.Collider.Raycast()
  指定一条射线,判断该射线是否与Collider本身发生了碰撞,如果是返回true,如果不是返回false.
  (注:相比1,这个函数用于判断射线是否与指定的collider发生碰撞)


四. FixedUpdate 和 Update

  1.FixedUpate的调用是每间隔固定时间就调用一次,固定时间设置:Edit -> Project Settings -> Time ,然后可以在Inspector 面板下看到:Fixed TimeStep,这个就是设置
  FixedUpate调用的固定时间

  2.Update是每一帧调用一次,所以它被渲染的图形和机器的性能等因素所影响,它调用的时间不是固定的。

  3.测试代码
public class MoveObject : MonoBehaviour {    public GameObject TestObject;// Use this for initializationvoid Start () {    }// Update is called once per framevoid Update ()     {        //这里渲染图形,可以发现Update调用的时间间隔越来越长。        for (int i = 0; i< 99; ++i)        {            Instantiate(TestObject, new Vector3(-10 + i, -10 + i, 50), Quaternion.identity);        }        Debug.Log("Update time : " + Time.deltaTime);}    void FixedUpdate()    {        Debug.Log("FixedUpdate time : " + Time.deltaTime);    }}

五. 对象间的消息传递

  1.BroadcastMessage()
  作用:发送给自身和子对象

  2.SendMessage()
  作用:发送给自身

  3.SendMessageUpwards()
  作用:发送给自身和父对象







0 0
原创粉丝点击