不入门的碰撞

碰撞练习

 物体碰撞不同颜色小球实现分数的加减

分为4部分

  • 物体的移动

  • 球的随机生成

  • 碰撞后球消失(分数的加减)

  • 分数的显示(分数的加减)

下面为具体代码

  1. 实现脚本挂载物体前后移动 左右旋转 刚体组件:

    this.gameObject.AddComponent<Rigidbody>();
    // 先在 Start 中追加刚体组件// 可以直接在物体上add
    this.transform.Translate(new Vector3(0,0,Input.GetAxis("Vertical"))*0.1f);
    // 前后移动
    this.transform.Rotate(0,Input.GetAxis("Horizontal"),0);
    // 左右转向
    if (Input.GetKey(KeyCode.Space))
    {
        this.transform.Translate(Vector3.up*0.3f);
                // 检测是否按下空格键  然后跳
    }

     

  2. 在Plane上随机位置,随机颜色生成小球;

    Color[] color= new Color[]{Color.red,Color.blue,Color.black};
    // 颜色数组 
    private float SphereTimer = 0;
    // 先在 Update 外部创建计时器
    ---------------------------------------------
    SphereTimer += Time.deltaTime;
    // 时间增长
    // print(SphereTimer);
    if (SphereTimer > 3)
    {
       GameObject Spheretemp = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        // 创建小球
       Spheretemp.transform.position = new Vector3(Random.Range(-4.5f, 4.5f)
                                                   , 0.5f, Random.Range(-4.5f, 4.5f));
        // 随机位置
        Spheretemp.GetComponent<MeshRenderer>().material.color = 
                                                            color[Random.Range(0, 3)];
        // 随机颜色
        Destroy(Spheretemp, 6);
        // 延迟销毁
       SphereTimer = 0;
        // 小球的计时器清零
    }

     

  3. 碰撞消除及加分逻辑

    public int Score;
    // 公开分数 方便在显示类中获取
    --------------------------------------------------
    private void OnCollisionEnter(Collision collision)// 碰撞检测 
    {
        // 碰到不同颜色的球 给加分方法传入不同的参数
        if (collision.gameObject.GetComponent<MeshRenderer>().
                                                    material.color == Color.red)
        {
            AddScore(1);
        }
        if (collision.gameObject.GetComponent<MeshRenderer>().
                                                    material.color == Color.blue)
        {
            AddScore(2);
        }
        if (collision.gameObject.GetComponent<MeshRenderer>().
                                                    material.color == Color.black)
        {
            AddScore(3);
        }
        // 如果检测到碰撞物体的名字为球  则销毁这个物体
        if (collision.gameObject.name == "Sphere")
        {
            Destroy(collision.gameObject);
        }
    }
        // 这是一个加分方法  可以再本类中写  也可以在别的类中写
        private void AddScore(int score)
        {
            Score += score;
            // print(Score);
        }

     

  4. 分数及时间的显示

    public Text Scores;
    public Text Timer;
    float time = 0;
    // 公开声明Text 方便拖拽,在外部声明计时器
    --------------------------------------------------
    int Score = TankCollder.Score;
    // 获取TankCollder中的分数 
    Scores.text = string.Format("{0:d2}", Score);
    // 显示 分数
    time+= Time.deltaTime;
    // 计时
    Timer.text = string.Format("{0:d2}",(int)time);
    // 显示时间 ,时间一定要强转成整数哦~

     

Unity的场景和拖拽就不截图了

posted @ 2018-11-15 16:21  武xth  阅读(108)  评论(0编辑  收藏  举报