一、创建一个Apple00,拖拽Prfabs下的Sprite命名为Apple00,添加碰撞器Box Collider,编写代码Knife Rey1.js
二、做被切的苹果
步骤和一是一样的,但是被切的水果需要有重力,添加了一个刚体RigidBody,注意:位置Z轴不需要移动,旋转:x、y不需要旋转
三、代码
遇到一个很奇葩的问题“js调用出现问题” 找了半天也没发现什么问题啊!原来在js中头部有个““#pragma strict” 需要把这个去掉即可!
#pragma strict
//var myColor:Color;
private var firstPosition:Vector3;//鼠标点击的第一个点
private var secondPosition:Vector3;//鼠标放开的位置
var middlePosition:Vector3;
private var isClick:boolean=false; var myRay:GameObject;
private var rayGameObject:GameObject; //private var lineRenderer:LineRenderer; //function Start () {
// lineRenderer=gameObject.AddComponent(LineRenderer);
// lineRenderer.material.color=myColor;
// lineRenderer.SetWidth(0.1,0.1);//设置宽度 起点宽度和终点宽度 function SetWidth (start : float, end : float) : void
//} //begin 3
var knifeSound:AudioClip;//切水果声音
var isHit:boolean=false;//判断是否被切
var rayPosition:Vector3;//切水果的坐标
var isRay:boolean=false;//是不是射线
var firstFruit:GameObject;//被切的苹果
var secondFruit:GameObject;//被切两半
private var myFirstFruit:GameObject;//实例化被切的苹果
private var mySecondFruit:GameObject;//实例化被切两半
private var angle:float;//角度
//end
function Update () {
var isMouseDown=Input.GetMouseButton(0);//获取按钮
if(isHit){
if(isMouseDown&&!isClick)
{
//获取鼠标按下的第一个位置
firstPosition=Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,1));
isClick=true;
}
else if(isMouseDown)
{
secondPosition=Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,1));
}
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=(1.0/3.14)*180*Mathf.Atan((secondPosition.y-firstPosition.y)/(secondPosition.x-firstPosition.x));
//Mathf.Atan((secondPosition.y-firstPosition.y)/(secondPosition.x-firstPosition.x));
}
else
{
angle=0;
}
//创建划痕,这里旋转的是幅度
//rayGameObject=Instantiate(myRay,rayPosition,Quaternion.AngleAxis(angle*180/Mathf.PI,Vector3.forward));
rayGameObject=Instantiate(myRay,rayPosition,Quaternion.AngleAxis(angle*180/3.14,Vector3.forward));
//生成两个被切的水果
myFirstFruit=Instantiate(firstFruit,transform.position,Quaternion.AngleAxis(Random.Range(50,180)*100/3.14,Vector3.forward));
mySecondFruit=Instantiate(secondFruit,transform.position,Quaternion.AngleAxis(Random.Range(80,150)*100/3.14,Vector3.forward));
if(Random.Range(1,10)>5.0)//左边、右边 是随机出现的,其实正确的应该是计算切线的角度来计算苹果切开两半的一个飞溅的方向
{
myFirstFruit.rigidbody.velocity=new Vector2(5,10);
mySecondFruit.rigidbody.velocity=new Vector2(-8,-10);
}
else
{
myFirstFruit.rigidbody.velocity=new Vector2(-5,-10);
mySecondFruit.rigidbody.velocity=new Vector2(8,10);
}
Physics.gravity=Vector3(0,-20,0);//重力加速度大些
Destroy(myFirstFruit,2.0);
Destroy(mySecondFruit,2.0);
if(audio.isPlaying)
{
audio.Stop();
}
else
{
PlaySound(knifeSound);
}
Destroy(rayGameObject,0.2);//切的划痕释放刀光 isClick=false;
isHit=false;
Destroy(rayGameObject,1.0);//一秒钟就去掉
}
}
else
{
isRay=false;
}
}
//在指定位置播放剪辑
function PlaySound(soundName)
{
if(!audio.isPlaying)
{
AudioSource.PlayClipAtPoint(soundName,Vector3(0,0,-10));
}
}
private var isClick:boolean=false; function Update () {
var isMouseDown:boolean=Input.GetMouseButton(0);
if(!isClick){
if(isMouseDown){
var ray:Ray=Camera.main.ScreenPointToRay(Input.mousePosition);
var hit:RaycastHit;
if(collider.Raycast(ray,hit,100.0)){//Collider.Raycast 光线投射 function Raycast (ray : Ray, out hitInfo : RaycastHit, distance : float) : bool
transform.GetComponent("knifeRey1").isHit=true;
transform.GetComponent("knifeRey1").rayPosition=hit.transform.position;
}
}
}
}
四、效果
他娘的,还没搞好,看下面