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

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

效果图                                                                                                                         




实现步骤                                                                                                                     

1.原理分析

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

2.半边苹果的制作

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

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



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

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



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

4.源代码

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

knifeRay01    //如果射线碰撞到苹果,就显示出两半苹果,并且给添加两个方向相反的方向力
[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class knifeRay01 : MonoBehaviour  
  5. {  
  6.   
  7.     //选择颜色  
  8.     //    public Color mycolor;  
  9.   
  10.     public GameObject myRay;  //这个是刀光的prefab  
  11.   
  12.     public AudioClip knifeSound;  
  13.   
  14.     public bool isHit = false;  
  15.     public Vector3 rayPosition;  
  16.   
  17.     public bool isRay = false;  
  18.   
  19.     public GameObject firstFruit;  
  20.     public GameObject secondFruit;  
  21.   
  22.     private GameObject myFirstFruit;  
  23.     private GameObject mySecondFruit;  
  24.   
  25.   
  26.     private Vector3 firstPosition;  
  27.     private Vector3 secondPosition;  
  28.     // private Vector3 middlePosition;  
  29.   
  30.     private bool isClicked = false;  
  31.   
  32.     private LineRenderer lineRenderer;  
  33.   
  34.     private GameObject rayGameObject;  
  35.   
  36.     private float angle;  
  37.   
  38.     // Use this for initialization  
  39.     //  void Start () {  
  40.     //        lineRenderer = gameObject.AddComponent<LineRenderer>();//添加一个划线的组件  
  41.     //        //设置颜色和宽度  
  42.     //        lineRenderer.material.color = mycolor;  
  43.     //        lineRenderer.SetWidth(0.1f, 0.1f);  
  44.     //  }  
  45.   
  46.     // Update is called once per frame  
  47.     void Update()  
  48.     {  
  49.   
  50.         bool isMouseDown = Input.GetMouseButton(0);//判断鼠标是否左击  
  51.   
  52.   
  53.         if (isHit)  
  54.         {  
  55.             if (isMouseDown && !isClicked)  
  56.             {  
  57.                 //屏幕坐标转化成空间坐标  
  58.                 firstPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));  
  59.   
  60.                 //            lineRenderer.SetVertexCount(1);  
  61.                 //  
  62.                 //            lineRenderer.enabled = true;  
  63.                 //            lineRenderer.SetPosition(0,firstPosition);  
  64.   
  65.                 isClicked = true;  
  66.             }  
  67.   
  68.             else if (isMouseDown)  
  69.             {  
  70.                 secondPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));  
  71.   
  72.                 //            lineRenderer.SetVertexCount(2);  
  73.                 //  
  74.                 //            lineRenderer.SetPosition(1, secondPosition);  
  75.             }  
  76.   
  77.             //鼠标提起  
  78.             else if (Input.GetMouseButtonUp(0))  
  79.             {  
  80.                 isRay = true;  
  81.   
  82.   
  83.   
  84.                 secondPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));  
  85.   
  86.                 if (secondPosition.x != firstPosition.x)  
  87.                 {  
  88.                     angle = Mathf.Atan((secondPosition.y - firstPosition.y) / (secondPosition.x - firstPosition.x));  
  89.                     print("角度:" + angle * 180 / Mathf.PI);  
  90.                 }  
  91.                 else  
  92.                 {  
  93.                     angle = 0;  
  94.                 }  
  95.   
  96.                 //创建划痕,这里旋转的是幅度  
  97.                 rayGameObject = Instantiate(myRay, rayPosition, Quaternion.AngleAxis(angle * 180 / Mathf.PI, Vector3.forward)) as GameObject;  
  98.                 //两个被切的水果  
  99.                 myFirstFruit = Instantiate(firstFruit, transform.position, Quaternion.AngleAxis(Random.Range(50, 180) * 180 / Mathf.PI, Vector3.forward)) as GameObject;  
  100.                 mySecondFruit = Instantiate(firstFruit, transform.position, Quaternion.AngleAxis(Random.Range(80, 150) * 180 / Mathf.PI, Vector3.forward)) as GameObject;  
  101.   
  102.                 //              myFirstFruit.rigidbody.velocity = new Vector2(Mathf.Sin(angle)*10,-Mathf.Cos(angle)*10);  
  103.                 //              mySecondFruit.rigidbody.velocity = new Vector2(Mathf.Sin(angle)*10,Mathf.Cos(angle)*10);  
  104.   
  105.                 //这里方向是随机出现的,其实正确的应该是计算切线的角度来计算苹果切开两半的一个飞溅的方向  
  106.                 if (Random.Range(1, 10) > 5)  
  107.                 {                       //给加一个力,方向相反  
  108.                     myFirstFruit.rigidbody.velocity = new Vector2(5, 10);  
  109.                     mySecondFruit.rigidbody.velocity = new Vector2(-8, -10);  
  110.                     myFirstFruit.rigidbody.velocity = new Vector2(0, 10);  
  111.                     mySecondFruit.rigidbody.velocity = new Vector2(0, -10);  
  112.                 }  
  113.                 else  
  114.                 {  
  115.                     myFirstFruit.rigidbody.velocity = new Vector2(-5, 10);  
  116.                     mySecondFruit.rigidbody.velocity = new Vector2(8, -10);  
  117.                 }  
  118.   
  119.                 Physics.gravity = new Vector3(0, -20, 0);  
  120.   
  121.                 Destroy(myFirstFruit, 2.0f);  
  122.                 Destroy(mySecondFruit, 2.0f);  
  123.   
  124.                 if (audio.isPlaying)  
  125.                 {  
  126.                     audio.Stop();  
  127.                 }  
  128.                 else  
  129.                 {  
  130.                     PlaySound(knifeSound);  
  131.                     print("播放声音");  
  132.                 }  
  133.   
  134.                 Destroy(rayGameObject, 0.2f);//立马释放刀光  
  135.   
  136.                 //            lineRenderer.SetVertexCount(2);  
  137.                 //  
  138.                 //            lineRenderer.SetPosition(1, secondPosition);  
  139.   
  140.   
  141.                 isClicked = false;  
  142.   
  143.                 isHit = false;  
  144.   
  145.                 //middlePosition = (firstPosition+secondPosition)/2;  
  146.   
  147.   
  148.                 Destroy(rayGameObject, 1.0f);//一秒钟就去掉  
  149.             }  
  150.         }  
  151.   
  152.         else  
  153.         {  
  154.             isRay = false;  
  155.         }  
  156.     }  
  157.   
  158.     void PlaySound(AudioClip soundName)  
  159.     {  
  160.         if (!audio.isPlaying)  
  161.         {  
  162.             AudioSource.PlayClipAtPoint(soundName, new Vector3(0, 0, -10));//在指定位置播放      
  163.         }  
  164.     }  
  165. }  

hitByKnife//用于判断射线是否碰撞到苹果
[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class hitByKnife : MonoBehaviour  
  5. {  
  6.   
  7.     private bool isClicked = false;  
  8.   
  9.     // Use this for initialization  
  10.     void Start()  
  11.     {  
  12.   
  13.     }  
  14.   
  15.     // Update is called once per frame  
  16.     void Update()  
  17.     {  
  18.         bool isMouseDown = Input.GetMouseButton(0);//判断鼠标是否左击  
  19.         if (!isClicked)  
  20.         {  
  21.             if (isMouseDown)  
  22.             {  
  23.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);  
  24.                 RaycastHit hit;  
  25.                 if (collider.Raycast(ray, out hit, 1000f))  
  26.                 {  
  27.                     transform.GetComponent<knifeRay01>().isHit = true;  
  28.                     transform.GetComponent<knifeRay01>().rayPosition = hit.transform.position;  
  29.                 }  
  30.             }  
  31.         }  
  32.     }  
  33. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值