继续水果忍者制作,写一下今天的学习心得,主要就是实现了一个切水果的刀光以及声音的实现。
主要效果
 
 
![[Unity3d]水果忍者-声音和刀光的实现_LineRenderer](https://file.cfanz.cn/uploads/png/2023/04/27/7/CfRMP66ZV2.png)
   
实现步骤
1.实现划线
原理:主要是用到Effet->Line Renderer组件(线渲染器),这个可以实现画出线条,具体可以参考官方的文档:http://game.ceeger.com/Script/LineRenderer/LineRenderer.html,下面会给出代码,我这里是给空物体GameObject动态添加的LineRenderer组件,然后实现绘图,下面我会贴出代码,注释掉的部分就是划线部分,下面是效果图。
![[Unity3d]水果忍者-声音和刀光的实现_3D_02](https://file.cfanz.cn/uploads/png/2023/04/27/7/D010da0V5d.png)
2.制作刀光贴图的预设物体,并且带有声音
创建一个GameObject,命名knifeRay,添加上AudioSource组件,给他赋予一个刀光的贴图,设置好knifeRay的宽高比,然后直接拖放到Project中,就做成预设。
![[Unity3d]水果忍者-声音和刀光的实现_3D_03](https://file.cfanz.cn/uploads/png/2023/04/27/7/24dScKfR62.png)
这里声音源我要说一下,关于2D和3D的声音,因为MainCamera上挂着AudioListener的组件,相当于人的耳朵,能够听到场景的声音,如果MainCamera离生源距离远,那么听到的声音就弱,反之则强,就跟3D现实中一样,如果是2D生源则跟Listener的远近是无关的,无论在哪儿,声音都是一样的效果。
关于声源的设置是在点击声音文件之后,然后在属性面板中有这个选项,只要将3D勾去掉就变成2D声音了。
![[Unity3d]水果忍者-声音和刀光的实现_LineRenderer_04](https://file.cfanz.cn/uploads/png/2023/04/27/7/GA3H5b618f.png)
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);//一秒钟就去掉
        }
	}
} 
====================== 相互学习,共同进步 ===================
欢迎关注我的微博:http://weibo.com/u/2590571922
 









