Unity3d fruit ninja production

Unity3d fruit ninja production

Then yesterday's update, the understanding and implementation of the code implementation module, here will not explain too much about the ui and the sound part, mainly explain the implementation function of the code in the game scene:

The ingenious use and deletion of prefabs in a game production process can help us save a lot of time, storage space, and code volume, which is more prominent in the implementation of fruit-slicing games. Fruits need to be constantly updated and cut. The deletion of fruits and the use of prefabs can save us a lot of work. Therefore, in the production process, we need to set all kinds of fruits, swords, swords and reward icons as prefabs so that we can continuously create references and delete them in the process.
insert image description here
There is also a prefab that is cut in half, for us to be divided into two pieces after cutting the fruit.
insert image description here
Ok, let's start to cut the fruit and sort out and implement the score feedback code:

Some are shown below 内联代码片.

	void Update () {
    
    
        if (Input.GetMouseButton(0))
        {
    
    //OverlapPoint方法用于检测2D场景中参数坐标上所有的碰撞体,这里我们的参数是鼠标点击的位置,
        //因此我们用于判断是否点击到了水果来完成信息的反馈更新
            if (Collider.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition)))
            {
    
    //cs0.是仓鼠对象,这里是玩家有五条生命格,如果切到了仓鼠则减去一条生命,如果不是的话则进入加分和其他的一些操作。
                if (name != "cs0.(Clone)")
                {
    
    //jpg金苹果 作为游戏彩蛋切割到了加二十分
                    if (name == "jpg0.(Clone)")
                    {
    
    
                        GameObject add = Instantiate(GoldTip) as GameObject;//复制彩蛋特校为add对象
                        add.transform.position = transform.position;
                        //add彩蛋对象位于金苹果被切的位置,且在一秒后删除
                        //(这里的逻辑和方法希望大家能够仔细的去理解因为后面的代码实现功能都有这几句代码的影子)
                        Destroy(add, 1);                                
                        AudioSource.PlayClipAtPoint(GoldClip, transform.position);
                        ScoreManage1.scorenumber+=20;
                        //加20分在计分类scoremanage1的静态变量scorenumber中,同时播放一次彩蛋的音乐提示
                    }
                    else//如果不是金苹果彩蛋对象而是普通的水果则加一分
                        ScoreManage1.scorenumber+=1;
                }
                else//如果是仓鼠的话则扣去一条生命值且播放扣除生命的音乐提示
                {
    
    
                    AudioSource.PlayClipAtPoint(FailClip, transform.position);
                    LifeManager1.life--;
                }
//下面两句是将预制体调用起来的代码,大家只要理解刚刚创建金苹果的彩蛋对象下面都是一样的功能实现,首先是刀光的预制体复制;
//instantiate方法里参数一:是预设 参数二:实例化预设的坐标(即被切的水果位置出现刀光)  参数三:实例化预设的旋转角度(为了游戏美观选择一定角度的随机数)
//Quaternion.AngleAxis的第二个参数就是旋转轴,意思是在垂直相机平面的轴为旋转轴随机度数旋转
                knifeRay = Instantiate(KnifeRay, transform.position, Quaternion.AngleAxis(Random.Range(-30f, 30f), Vector3.back)) as GameObject;
                Destroy(knifeRay, 1);
                //以下代码为创建水果代码同上不做赘述,写好后挂载在所有的水果预制体上即可
                //下面两个apple变量是水果切除后原来的完整水果的预制体删除,同时将切成两半的水果预制体复制出来达到切除显示效果。
                Destroy(this.gameObject);
                GameObject apple1 = Instantiate(Apple1, transform.position, Quaternion.AngleAxis(Random.Range(-30f, 30f), Vector3.back)) as GameObject;
                GameObject apple2 = Instantiate(Apple2, transform.position, Quaternion.AngleAxis(Random.Range(-30f, 30f), Vector3.back)) as GameObject;
                apple1.GetComponent<Rigidbody2D>().velocity = new Vector2(Random.Range(-10, -5),Random.Range(2,3));
                apple2.GetComponent<Rigidbody2D>().velocity = new Vector2(Random.Range(10, 5), Random.Range(2, 3));
                GameObject wzt = Instantiate(wz[Random.Range(0, 3)]) as GameObject;
                wzt.transform.position = transform.position;
                //wzt是切除的果汁特效,这几句代码虽然很长但是理解了的话其实都是一个方法多次使用最后把所有的预制体都删除整个的游戏切除水果代码就完成了。
                Destroy(wzt, 1);
                Destroy(apple1, 2);
                Destroy(apple2, 2);
            }
        }
	}

In the whole process of obsession with the game, the workload is relatively small because of the raw materials of the teaching materials. I personally think that this section is the core code of the entire game, and the information feedback and UI update code for the life value and score are not repeated here. Just give you a reference, because I just refer to this section of code in the textbook, and the rest of the code is implemented according to my own ideas and code. I feel that there are many methods and codes that can be implemented. It is not very difficult, so I don’t do too much Let’s go over it and refer to it to write the code you like.

Some are shown below 内联代码片.

public class ScoreManage1 : MonoBehaviour {
    public Image[] score;
    public Sprite[] number;
    public static int scorenumber = 0;
	// Use this for initialization
	void Start () {
        
 	}
	public void UpdateScore()
    {
        
        score[0].GetComponent<Image>().sprite = number[scorenumber / 100];
        score[1].GetComponent<Image>().sprite = number[scorenumber / 10];
        score[2].GetComponent<Image>().sprite = number[scorenumber % 10];
    }
	// Update is called once per frame
	void Update () {
        UpdateScore();
	}
}
public class LifeManager1 : MonoBehaviour {
    
    
    public Image[] lifeImage;
    public int lifeLength;
    public static int life=5;
	// Use this for initialization
	void Start () {
    
    
        lifeLength = lifeImage.Length;       
	}
	public void SetLife()
    {
    
    
        for(int i=0; i<5-life;  i++)
        {
    
    
            lifeImage[i].color = Color.red;
        }          
    }
	// Update is called once per frame
	void Update () {
    
    
        SetLife();
        if(life==0)
        {
    
    
            Application.LoadLevel("gameover");
        }
	}
}

Well, this is the code part of the whole game, the ui code of the main page is also very simple, just write it yourself.

Guess you like

Origin blog.csdn.net/weixin_50746193/article/details/116791002
Recommended