您的位置:首页 > 移动开发 > Unity3D

Unity之一天一个技术点(十二)---指南针的实现

2015-02-09 09:48 441 查看
指南针的实现(可据镜头旋转改变)

代码如下:

变量简述:

compassGUISkin皮肤用来显示指南针贴图

标签Label贴图用来作为指南针背景 Box贴图用来作为箭头贴图

把脚本赋予给主镜头即可

GUI.skin = compassGUISkin;

var compassAngle : float = transform.rotation.eulerAngles.y;//得到镜头的y轴旋转角度
var compassDiameter : float = 128;
var compassWidth : float = 16;
var compassWindowRect : Rect = Rect (compassPos.x,compassPos.y, compassDiameter, compassDiameter);
GUI.Label (compassWindowRect, "");
var cosAngle : float = Mathf.Cos(compassAngle*Mathf.PI/180);
var sinAngle : float = Mathf.Sin(compassAngle*Mathf.PI/180);
var xMove : float = (compassDiameter*sinAngle/2.0 + compassWidth*cosAngle/2.0);
var yMove : float = (-compassDiameter*cosAngle/2.0 + compassWidth*sinAngle/2.0);
var compassPos : Vector3 = new Vector3(compassPos.x + compassDiameter/2.0 + xMove,compassPos.y + compassDiameter/2.0  + yMove, 0); //position for matrix
var compassQuat : Quaternion = Quaternion.identity; //rotation for matrix
compassQuat.eulerAngles = Vector3(0, 0, compassAngle + 90); //set the rotation to something - rotate around z!
GUI.matrix = Matrix4x4.TRS(compassPos, compassQuat, Vector3.one); //Apply the matrix
GUI.Box(Rect(0, 0, compassDiameter, compassWidth), ""); //notice how the rect starts at 0/0 and the matrix handles the position
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: