26821人加入学习
(150人评价)
Unity2D 初级案例 - 坦克大战(Unity2017.1)

制作于2017年12月23日

价格 免费

碰撞事件,需要有碰撞器,至少有一方是有刚体的,最好是运动的一方

防止抖动方法,FixedUpdate

[展开全文]

两个碰撞体

box 2d

private void fixedupdate(){}

[展开全文]

碰撞检测的两个必要条件

1、组件身上要有碰撞器

2、其中一方要有刚体(最好是运动的一方)。

原因:刚体长时间不运动会休眠就不会发生碰撞检测。

每一帧受力是相同的,不能发生变化。所以放到物理帧中:FixedUpdate();

 

[展开全文]

碰撞器的生成

二者都要有碰撞器

其中一方(运动的一方)要有缸体

fixedupdate 物理针

是每一秒受力固定

缸体碰撞受力使物体发生抖动,添加物理针处理

未加物理针

void Update ()
    {
        float h = Input.GetAxisRaw("Horizontal");
        transform.Translate(Vector3.right * h * movespeed * Time.deltaTime, Space.World);
        if (h < 0)
        {
            sr.sprite = tanksprite[3];
        }
        else if (h > 0)
        {
            sr.sprite = tanksprite[1];
        }
        float v = Input.GetAxisRaw("Vertical");
        transform.Translate(Vector3.up * v * movespeed * Time.deltaTime, Space.World);
        if (v < 0)
        {
            sr.sprite = tanksprite[2];
        }
        else if (v > 0)
        {
            sr.sprite = tanksprite[0];
        }

    }

添加物理针

  private void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        transform.Translate(Vector3.right * h * movespeed * Time.fixedDeltaTime, Space.World);
        if (h < 0)
        {
            sr.sprite = tanksprite[3];
        }
        else if (h > 0)
        {
            sr.sprite = tanksprite[1];
        }
        float v = Input.GetAxisRaw("Vertical");
        transform.Translate(Vector3.up * v * movespeed * Time.fixedDeltaTime, Space.World);
        if (v < 0)
        {
            sr.sprite = tanksprite[2];
        }
        else if (v > 0)
        {
            sr.sprite = tanksprite[0];
        }
    }

[展开全文]

授课教师

SiKi学院老师

课程特色

图文(2)
视频(31)
下载资料(1)