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=true; } }