void Update () {if (Input.GetMouseButton(0)){Vector3 ms = Input.mousePosition;ms = Camera.main.ScreenToWorldPoint(ms);//获取鼠标相对位置//对象的位置Vector3 gunPos = this.transform.position;float fireangle;//发射角度//计算鼠标位置与对象位置之间的角度Vector2 targetDir = ms - gunPos;fireangle = Vector2.Angle(targetDir, Vector3.up);if (ms.x > gunPos.x){fireangle = -fireangle;}this.transform.eulerAngles = new Vector3(0, 0, fireangle);}
}
将该脚本挂在需要改变的对象上,获取鼠标点击位置,计算对象与鼠标点击位置之间的角度,即为需要对象转向的角度值。