功能实现_实现指北针(UI图片)效果
using?System.Collections.Generic;
using?UnityEngine;
public?class?Compass?:?MonoBehaviour
{
????public?GameObject?camera;?????????? //3D場(chǎng)景中需要旋轉(zhuǎn)的物體
????public?Transform?compassSprite;???? //指北針的UI圖片
????private?float?cameraLastEular;????? //攝像機(jī)上一幀的歐拉旋轉(zhuǎn)
????private?float?cameraCurrentEular;?? //攝像機(jī)當(dāng)前幀的歐拉旋轉(zhuǎn)
????private?float?cameraEularValue;???? //攝像機(jī)的歐拉旋轉(zhuǎn)值
????private?float?compassEularValue;??? //指北針的歐拉旋轉(zhuǎn)值
????public?int?frameCount=0;????????????//幀的數(shù)量
????public?int?frameNumber?=?10;????????//每秒10幀
????public??float?timer;????????????????//計(jì)時(shí)器
????
????void?FixedUpdate?()
????{
????????cameraEularValue?=?camera.transform.eulerAngles.y;
????????compassEularValue?=?compassSprite.transform.eulerAngles.z;
????????compassEularValue?=?-cameraEularValue;
????????LookNorth();
????}
????void?LookNorth()
????{
????????//上一幀,上一旋轉(zhuǎn)角度????? **********這里必須為歐拉角,錯(cuò)誤代碼為下面寫法************
????????cameraLastQua?=?camera.transform.eulerAngles.y;
????????timer?+=?Time.deltaTime;
????????//判斷時(shí)間>=一秒/每秒執(zhí)行的幀數(shù)
????????if?(timer?>=?1.0f?/?frameNumber)
????????{
????????????//幀的數(shù)量在增加
????????????frameCount++;
????????????timer?-=?1.0f?/?frameNumber;
????????????//下一幀,當(dāng)前旋轉(zhuǎn)角度
????????????cameraCurrentQua?=?camera.transform.eulerAngles.y;
????????}
????????//判斷上一幀的旋轉(zhuǎn)角度和當(dāng)前幀的旋轉(zhuǎn)角度是否一樣,讓指北針圖片進(jìn)行旋轉(zhuǎn)
????????{
??????????? //當(dāng)前旋轉(zhuǎn)角度小于0? 和? 上一幀旋轉(zhuǎn)角度小于等于0
????????????{
??????????????? //如果當(dāng)前角度小于上一幀角度
????????????????{
??????????????????? //指北針使用歐拉角進(jìn)行旋轉(zhuǎn)
????????????????}
????????????????else
????????????????{
??????????????????? //最后面乘-1是避免會(huì)出現(xiàn)正的角度
????????????????}
????????????}
????????????if?(cameraCurrentEular>0.0f?&&?cameraLastEular>=0.0f)
????????????{
????????????????if?(cameraCurrentEular?>?cameraLastEular)
????????????????{
????????????????????compassSprite.transform.eulerAngles?=?new?Vector3(0.0f,?0.0f,?compassEularValue*-1);
????????????????}
????????????????else
????????????????{
????????????????????compassSprite.transform.eulerAngles?=?new?Vector3(0.0f,?0.0f,?-compassEularValue);
????????????????}
????????????}
????????}
????}
????/*?? 這段代碼是錯(cuò)誤的
????void?LookNorth()
????{
????????//上一幀,上一旋轉(zhuǎn)角度
????????cameraLastQua?=?camera.transform.rotation;
????????timer?+=?Time.deltaTime;
????????//判斷
????????if?(timer?>=?1.0f/frameNumber)
????????{
????????????//幀的數(shù)量在增加
????????????frameCount++;
????????????timer?-=?1.0f?/?frameNumber;
????????????//下一幀,當(dāng)前旋轉(zhuǎn)角度
????????????cameraCurrentQua?=?camera.transform.rotation;
????????}
????????//判斷上一幀的旋轉(zhuǎn)角度和當(dāng)前幀的旋轉(zhuǎn)角度是否一樣,讓指北針圖片進(jìn)行旋轉(zhuǎn)
????????if?(cameraCurrentQua?!=?cameraLastQua)
????????{
????????????if?(cameraLastQua.y>0.0f?&&?cameraCurrentQua.y>0.0f)
????????????{
????????????????if?(cameraCurrentQua.y?>?cameraLastQua.y)
????????????????{
????????????????????compassSprite.transform.Rotate(0.0f,?0.0f,?compassRotateValue);
????????????????????//compassSprite.transform.localRotation?=?Quaternion.Euler(0.0f,?0.0f,?compassRotateValue);
????????????????????//compassSprite.transform.eulerAngles?=?new?Vector3(0.0f,?0.0f,?compassRotateValue);
????????????????}
????????????????else
????????????????{
????????????????????compassSprite.transform.Rotate(0.0f,?0.0f,?-compassRotateValue);
????????????????????//compassSprite.transform.localRotation?=?Quaternion.Euler(0.0f,?0.0f,?-compassRotateValue);
????????????????????//compassSprite.transform.eulerAngles?=?new?Vector3(0.0f,?0.0f,?-compassRotateValue);
????????????????}
????????????}
????????????if(cameraLastQua.y?<?0.0f?&&?cameraCurrentQua.y?<?0.0f)
????????????{
????????????????if?(cameraCurrentQua.y?<cameraLastQua.y)
????????????????{
????????????????????compassSprite.transform.Rotate(0.0f,0.0f,compassRotateValue);
????????????????????//compassSprite.transform.localRotation?=?Quaternion.Euler(0.0f,?0.0f,?-compassRotateValue);
????????????????????//compassSprite.transform.eulerAngles?=?new?Vector3(0.0f,?0.0f,?-compassRotateValue);
????????????????}
????????????????else
????????????????{
????????????????????compassSprite.transform.Rotate(0.0f,?0.0f,-compassRotateValue);
????????????????????//compassSprite.transform.localRotation?=?Quaternion.Euler(0.0f,?0.0f,?compassRotateValue);
????????????????????//compassSprite.transform.eulerAngles?=?new?Vector3(0.0f,?0.0f,?compassRotateValue);
????????????????}
????????????}
????????}
????}
????*/
總結(jié)
以上是生活随笔為你收集整理的功能实现_实现指北针(UI图片)效果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CMOS与CCD的区别是什么?
- 下一篇: 湘南学院计算机专业近两年分数线,湘南学院