unity 罗盘 Android,Unity之一天一个技术点(十二)-罗盘的实现

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

指南针的实现(可据镜头旋转改变)

代码如下:

变量简述:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值