一、步骤一创建一个空GameObject、js
二、代码
#pragma strict
var myColor:Color;
var firstPosition:Vector3;//鼠标点击的第一个点
var secondPosition:Vector3;//鼠标放开的位置
var middlePosition:Vector3;
var isClick:boolean=false;
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
} function Update () {
var isMouseDown=Input.GetMouseButton(0);//获取按钮
if(isMouseDown&&!isClick){
firstPosition=Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,1));
lineRenderer.SetVertexCount(1);//设置线段数
lineRenderer.enabled=true;
lineRenderer.SetPosition(0,firstPosition);
isClick=true;
}
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)){
isClick=false;
secondPosition=Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,1));
lineRenderer.SetVertexCount(2);
lineRenderer.SetPosition(1,secondPosition);
}
}
三、实现划线功能
四、实现刀光剑影
编辑好位置
调用刀光剑影
最终代码
#pragma strict
//var myColor:Color;
var firstPosition:Vector3;//鼠标点击的第一个点
var secondPosition:Vector3;//鼠标放开的位置
var middlePosition:Vector3;
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
//} function Update () {
var isMouseDown=Input.GetMouseButton(0);//获取按钮
if(isMouseDown&&!isClick){
firstPosition=Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,1));
//lineRenderer.SetVertexCount(1);//设置线段数
//lineRenderer.enabled=true;
//lineRenderer.SetPosition(0,firstPosition);
isClick=true;
}
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)){
isClick=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.0;
var angle:float=Mathf.Atan((secondPosition.y-firstPosition.y)/(secondPosition.x-firstPosition.x));
rayGameObject=Instantiate(myRay,middlePosition,Quaternion.AngleAxis(angle*100/3.14,Vector3.forward));
Destroy(rayGameObject,1.0);
} }