不入门的碰撞

碰撞练习

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

分为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的场景和拖拽就不截图了

转载于:https://www.cnblogs.com/wuxth/p/9964286.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值