功能实现_实现指北针(UI图片)效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Compass : MonoBehaviour
{
    public GameObject camera;           //3D场景中需要旋转的物体
    public Transform compassSprite;     //指北针的UI图片
    private float cameraLastEular;      //摄像机上一帧的欧拉旋转
    private float cameraCurrentEular;   //摄像机当前帧的欧拉旋转
    private float cameraEularValue;     //摄像机的欧拉旋转值
    private float compassEularValue;    //指北针的欧拉旋转值
    public int frameCount=0;            //帧的数量
    public int frameNumber = 10;        //每秒10帧
    public  float timer;                //计时器
    
    void FixedUpdate ()
    {
        cameraEularValue = camera.transform.eulerAngles.y;
        compassEularValue = compassSprite.transform.eulerAngles.z;
        compassEularValue = -cameraEularValue;
        LookNorth();
    }

    void LookNorth()
    {
        //上一帧,上一旋转角度      **********这里必须为欧拉角,错误代码为下面写法************
        cameraLastQua = camera.transform.eulerAngles.y;

        timer += Time.deltaTime;
        //判断时间>=一秒/每秒执行的帧数
        if (timer >= 1.0f / frameNumber)
        {
            //帧的数量在增加
            frameCount++;
            timer -= 1.0f / frameNumber;
            //下一帧,当前旋转角度
            cameraCurrentQua = camera.transform.eulerAngles.y;
        }

        //判断上一帧的旋转角度和当前帧的旋转角度是否一样,让指北针图片进行旋转

        if (cameraCurrentQua != cameraLastQua)

        {

            //当前旋转角度小于0  和  上一帧旋转角度小于等于0

            if (cameraCurrentEular<0.0f && cameraLastEular<=0.0f)

            {

                //如果当前角度小于上一帧角度

                if (cameraCurrentEular < cameraLastEular)

                {

                    //指北针使用欧拉角进行旋转

                    compassSprite.transform.eulerAngles = new Vector3(0.0f, 0.0f, -compassEularValue);
                }
                else

                {

                    //最后面乘-1是避免会出现正的角度

                    compassSprite.transform.eulerAngles = new Vector3(0.0f, 0.0f, compassEularValue*-1);
                }
            }

            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);
                }
            }
        }

    }




    /*   这段代码是错误的
    void LookNorth()
    {

        //上一帧,上一旋转角度
        cameraLastQua = camera.transform.rotation;

        timer += Time.deltaTime;
        //判断
        if (timer >= 1.0f/frameNumber)
        {
            //帧的数量在增加
            frameCount++;
            timer -= 1.0f / frameNumber;
            //下一帧,当前旋转角度
            cameraCurrentQua = camera.transform.rotation;
        }

        //判断上一帧的旋转角度和当前帧的旋转角度是否一样,让指北针图片进行旋转
        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);
                }
            }
        }
    }
    */

}

你可能感兴趣的:(自己的代码,功能的实现)