[Unity3d]水果忍者-切水果功能

简介: 继续今天的切水果游戏之切苹果的实现,主要功能就是,有一个苹果放在场景中,然后通过手滑过苹果,就将苹果切成两半,从原理上分析,就是制作两张贴图,分别表示分开的两半苹果,然后在当前位置出现,并且给这两半苹果加上刚体属性,然后分别给这两半苹果加上一个相反的力使其自由落体...

继续今天的切水果游戏之切苹果的实现,主要功能就是,有一个苹果放在场景中,然后通过手滑过苹果,就将苹果切成两半,从原理上分析,就是制作两张贴图,分别表示分开的两半苹果,然后在当前位置出现,并且给这两半苹果加上刚体属性,然后分别给这两半苹果加上一个相反的力使其自由落体!

效果图                                                                                                                         




实现步骤                                                                                                                     

1.原理分析

就是通过摄像机发出一条射线碰撞到具备碰撞器的苹果,也就是说要给苹果添加上BoxCollider属性,然后苹果检测到碰撞,然后在当前位置出现两个具有刚体(物理)属性的半边苹果,方向是随机出现,然后随机给添加上向量力,使得这两个半个苹果做自由下落。

2.半边苹果的制作

创建一个材质球,然后给材质球赋予贴图属性,就像下图所示的红色区域的图片

然后添加上BoxCollider和Rigidbody两个属性,勾选下图所示的红色区域中的选项



IsTrigger:勾选上的时候,触发器不会碰撞刚体,但当刚体退出或进入触发器的时候,将会发送OnTriggerEnter,OnTriggerExit和OnTriggerStay消息。
UseGravity:字面解释就是使用重力
Constraints:是约定冻结旋转和移动,这里冻结了Z轴的移动,也就是不允许苹果前后位置的移动,冻结了X,Y轴方向的旋转,也就是不能让苹果前后翻转或者左右旋转。
然后将这半个苹果做成prefeb,删除掉场景中的半个苹果的GameObject。

3.完整苹果的属性的设置



主要是让这个主苹果具备BoxCollider属性,使其能够让射线碰撞到,主要要加上AudioSource不然会报错。

4.源代码

该两个代码附加在Apple00上,也就是主苹果上。

knifeRay01    //如果射线碰撞到苹果,就显示出两半苹果,并且给添加两个方向相反的方向力
using UnityEngine;
using System.Collections;

public class knifeRay01 : MonoBehaviour
{

    //选择颜色
    //    public Color mycolor;

    public GameObject myRay;  //这个是刀光的prefab

    public AudioClip knifeSound;

    public bool isHit = false;
    public Vector3 rayPosition;

    public bool isRay = false;

    public GameObject firstFruit;
    public GameObject secondFruit;

    private GameObject myFirstFruit;
    private GameObject mySecondFruit;


    private Vector3 firstPosition;
    private Vector3 secondPosition;
    // private Vector3 middlePosition;

    private bool isClicked = false;

    private LineRenderer lineRenderer;

    private GameObject rayGameObject;

    private float angle;

    // Use this for initialization
    //	void Start () {
    //        lineRenderer = gameObject.AddComponent<LineRenderer>();//添加一个划线的组件
    //        //设置颜色和宽度
    //        lineRenderer.material.color = mycolor;
    //        lineRenderer.SetWidth(0.1f, 0.1f);
    //	}

    // Update is called once per frame
    void Update()
    {

        bool isMouseDown = Input.GetMouseButton(0);//判断鼠标是否左击


        if (isHit)
        {
            if (isMouseDown && !isClicked)
            {
                //屏幕坐标转化成空间坐标
                firstPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));

                //            lineRenderer.SetVertexCount(1);
                //
                //            lineRenderer.enabled = true;
                //            lineRenderer.SetPosition(0,firstPosition);

                isClicked = true;
            }

            else if (isMouseDown)
            {
                secondPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));

                //            lineRenderer.SetVertexCount(2);
                //
                //            lineRenderer.SetPosition(1, secondPosition);
            }

            //鼠标提起
            else if (Input.GetMouseButtonUp(0))
            {
                isRay = true;



                secondPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));

                if (secondPosition.x != firstPosition.x)
                {
                    angle = Mathf.Atan((secondPosition.y - firstPosition.y) / (secondPosition.x - firstPosition.x));
                    print("角度:" + angle * 180 / Mathf.PI);
                }
                else
                {
                    angle = 0;
                }

                //创建划痕,这里旋转的是幅度
                rayGameObject = Instantiate(myRay, rayPosition, Quaternion.AngleAxis(angle * 180 / Mathf.PI, Vector3.forward)) as GameObject;
                //两个被切的水果
                myFirstFruit = Instantiate(firstFruit, transform.position, Quaternion.AngleAxis(Random.Range(50, 180) * 180 / Mathf.PI, Vector3.forward)) as GameObject;
                mySecondFruit = Instantiate(firstFruit, transform.position, Quaternion.AngleAxis(Random.Range(80, 150) * 180 / Mathf.PI, Vector3.forward)) as GameObject;

                //				myFirstFruit.rigidbody.velocity = new Vector2(Mathf.Sin(angle)*10,-Mathf.Cos(angle)*10);
                //				mySecondFruit.rigidbody.velocity = new Vector2(Mathf.Sin(angle)*10,Mathf.Cos(angle)*10);

                //这里方向是随机出现的,其实正确的应该是计算切线的角度来计算苹果切开两半的一个飞溅的方向
                if (Random.Range(1, 10) > 5)
                {						//给加一个力,方向相反
                    myFirstFruit.rigidbody.velocity = new Vector2(5, 10);
                    mySecondFruit.rigidbody.velocity = new Vector2(-8, -10);
                    myFirstFruit.rigidbody.velocity = new Vector2(0, 10);
                    mySecondFruit.rigidbody.velocity = new Vector2(0, -10);
                }
                else
                {
                    myFirstFruit.rigidbody.velocity = new Vector2(-5, 10);
                    mySecondFruit.rigidbody.velocity = new Vector2(8, -10);
                }

                Physics.gravity = new Vector3(0, -20, 0);

                Destroy(myFirstFruit, 2.0f);
                Destroy(mySecondFruit, 2.0f);

                if (audio.isPlaying)
                {
                    audio.Stop();
                }
                else
                {
                    PlaySound(knifeSound);
                    print("播放声音");
                }

                Destroy(rayGameObject, 0.2f);//立马释放刀光

                //            lineRenderer.SetVertexCount(2);
                //
                //            lineRenderer.SetPosition(1, secondPosition);


                isClicked = false;

                isHit = false;

                //middlePosition = (firstPosition+secondPosition)/2;


                Destroy(rayGameObject, 1.0f);//一秒钟就去掉
            }
        }

        else
        {
            isRay = false;
        }
    }

    void PlaySound(AudioClip soundName)
    {
        if (!audio.isPlaying)
        {
            AudioSource.PlayClipAtPoint(soundName, new Vector3(0, 0, -10));//在指定位置播放	
        }
    }
}

hitByKnife//用于判断射线是否碰撞到苹果
using UnityEngine;
using System.Collections;

public class hitByKnife : MonoBehaviour
{

    private bool isClicked = false;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        bool isMouseDown = Input.GetMouseButton(0);//判断鼠标是否左击
        if (!isClicked)
        {
            if (isMouseDown)
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (collider.Raycast(ray, out hit, 1000f))
                {
                    transform.GetComponent<knifeRay01>().isHit = true;
                    transform.GetComponent<knifeRay01>().rayPosition = hit.transform.position;
                }
            }
        }
    }
}

==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:375151422       cocos2dx QQ群:280818155

====================== 相互学习,共同进步 ===================

 

转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/18626365

欢迎关注我的微博:http://weibo.com/u/2590571922



相关文章
|
5月前
|
前端开发 小程序 编译器
有意思,圣诞节自己做一个装饰圣诞帽头像的APP!
有意思,圣诞节自己做一个装饰圣诞帽头像的APP!
|
9月前
|
JavaScript
水果消消乐总结
项目介绍 利用原生js实现,主要分为三个部分。 导入图片 定义层级数 格式 第一部分为格式的生成,定义层数,行数,列数,元素组数以及图片数组,动态生成div,把利用模板字符串图片插入到动态生成的div里,再利用随机数打乱排列。
48 2
|
9月前
酷炫代码雨
酷炫代码雨
41 0
|
算法 开发者
再学一道算法题:水果忍者
再学一道算法题:水果忍者
再学一道算法题:水果忍者
程序人生 - 马住这份水果攻略,以后再也不怕挑不到好吃的水果啦!
程序人生 - 马住这份水果攻略,以后再也不怕挑不到好吃的水果啦!
73 0
程序人生 - 马住这份水果攻略,以后再也不怕挑不到好吃的水果啦!