[Unity3d]水果忍者-声音和刀光的实现

继续水果忍者制作,写一下今天的学习心得,主要就是实现了一个切水果的刀光以及声音的实现。

主要效果                                                                                                                     


   

 实现步骤                                                                                                                    

1.实现划线

原理:主要是用到Effet->Line Renderer组件(线渲染器),这个可以实现画出线条,具体可以参考官方的文档:http://game.ceeger.com/Script/LineRenderer/LineRenderer.html,也可以参考:http://blog.youkuaiyun.com/zuoyamin/article/details/8997729,下面会给出代码,我这里是给空物体GameObject动态添加的LineRenderer组件,然后实现绘图,下面我会贴出代码,注释掉的部分就是划线部分,下面是效果图。


2.制作刀光贴图的预设物体,并且带有声音

创建一个GameObject,命名knifeRay,添加上AudioSource组件,给他赋予一个刀光的贴图,设置好knifeRay的宽高比,然后直接拖放到Project中,就做成预设。


这里声音源我要说一下,关于2D和3D的声音,因为MainCamera上挂着AudioListener的组件,相当于人的耳朵,能够听到场景的声音,如果MainCamera离生源距离远,那么听到的声音就弱,反之则强,就跟3D现实中一样,如果是2D生源则跟Listener的远近是无关的,无论在哪儿,声音都是一样的效果。

关于声源的设置是在点击声音文件之后,然后在属性面板中有这个选项,只要将3D勾去掉就变成2D声音了。


3.源码

using UnityEngine; using System.Collections;  public class knifeRay01 : MonoBehaviour {  	//选择颜色 //    public Color mycolor; 	 	public GameObject myRay;  //这个是刀光的prefab      private Vector3 firstPosition;     private Vector3 secondPosition;     private Vector3 middlePosition;      private bool isClicked = false;      private LineRenderer lineRenderer; 	 	private  GameObject rayGameObject;  	// 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 (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);         } 		  		//鼠标提起         if (Input.GetMouseButtonUp(0))         {             isClicked = false;              secondPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));  //            lineRenderer.SetVertexCount(2); // //            lineRenderer.SetPosition(1, secondPosition); 			 			 			middlePosition = (firstPosition+secondPosition)/2; 			 			float angle = Mathf.Atan((secondPosition.y-firstPosition.y)/(secondPosition.x-firstPosition.x)); 			//创建划痕,这里旋转的是幅度 			rayGameObject = Instantiate(myRay,middlePosition,Quaternion.AngleAxis(angle*180/Mathf.PI,Vector3.forward)) as GameObject; 		 			Destroy(rayGameObject,1.0f);//一秒钟就去掉         } 	} } 

==================== 迂者 丁小未 优快云博客专栏=================

MyBlog:http://blog.youkuaiyun.com/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

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

 

转载请注明出处:http://blog.youkuaiyun.com/dingxiaowei2013/article/details/18527897

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















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366136,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值