纳金网
标题:
unity3d 鼠标点击 控制人物行走
[打印本页]
作者:
markq
时间:
2012-5-11 11:00
标题:
unity3d 鼠标点击 控制人物行走
unity3d 鼠标点击 控制人物行走
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
RayControl();
}
if(flagMove)
{
if(Vector3.Distance(transform.position,mousePos)>1)
{
transform.Translate(transform.worldToLocalMatrix* transform.forward * Time.deltaTime*5);
}
else
{
flagMove=false;
}
}
}
void RayControl()
{
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit,200))
{
GameObject targetPos=GameObject.CreatePrimitive(PrimitiveType.Sphere);
targetPos.transform.localScale=new Vector3(0.5f,0.5f,0.5f);
mousePos=hit.point;
mousePos.y=transform.position.y;
targetPos.transform.position=mousePos;
targetDir=mousePos-transform.position;
Vector3 tempDir=Vector3.Cross(transform.forward,targetDir.normalized);
float dotValue=Vector3.Dot(transform.forward,targetDir.normalized);
float angle=Mathf.Acos(dotValue)*Mathf.Rad2Deg;
if(tempDir.y<0)
{
angle=angle*(-1);
}
print(tempDir.y);
print("2:"+angle);
transform.RotateAround(transform.position,Vector3.up,angle);
flagMove=***e;
}
}
欢迎光临 纳金网 (http://rs.narkii.com/club/)
Powered by Discuz! X2.5