zoukankan      html  css  js  c++  java
  • 重力感应控制对象的旋转和方向

    别的不多说上干货

    using UnityEngine;
    //           玩家控制器
    
    public class PlayerController : MonoBehaviour 
    {
        //●前向推力,飞行速度的作用值之一
        public float forwardForce = 50.0f;
        //●速度比
        private float normalizedSpeed = 0.2f;
        //●UI图标
        public Transform guiSpeedElement = null;
    
        //●旋转速度
        public float turnSpeed = 10.0f;
        //●重力敏感
        public float sensitivity = 1.0f;
        //●计算用欧拉角
        private Vector3 euler = Vector3.zero;
    
        //●俯冲、爬升速度(飞机抬头低头的旋转速度)
        private float maxTilt = 50.0f;
        //●左摇、右摆速度(飞机Z轴转速)
        private float maxTurnLaen = 50.0f;
    
        void Start()
        {//■开始■
            //屏幕朝向 = 屏幕朝向.设置自动旋转
            Screen.orientation = ScreenOrientation.AutoRotation;
            
            //更新UI坐标
            float x = guiSpeedElement.position.x;
            float y = normalizedSpeed * Screen.height;
            float z = guiSpeedElement.position.z;
            guiSpeedElement.position = new Vector3(x, y, z);
        }
    
        void Update()
        {//■更新■
            //判断所有触碰点(触碰点迭代器,遍历触碰点数组)
            foreach (Touch touch in Input.touches)
            {
                //phase事件(痱子)
                if (touch.phase == TouchPhase.Moved)
                {
                    //获取触碰
                    guiSpeedElement.position = new Vector3(0, touch.position.y, 0);
    
                    //更新速度
                    normalizedSpeed = touch.position.y / Screen.height;
                }
            }//foreach
        }
    
        void FixedUpdate()
        {//■固定更新■
            //飞机向前飞行(位移)
            Rigidbody rb = GetComponent<Rigidbody>();
            //计算:向前力 = 比率 × 前力
            float z = normalizedSpeed * forwardForce;
            //添加相对力
            rb.AddRelativeForce(0, 0, z);
    
            Vector3 acc = Input.acceleration;
    
            //飞机的转向(旋转)
            //战机X轴:爬升与俯冲(低头抬头),受手机Y轴影响
            euler.x = Mathf.Lerp(euler.x, Input.acceleration.y * maxTilt, .2f);
            //战机Y轴:左转与右转,受手机X轴影响
            euler.y += Input.acceleration.x * turnSpeed;
            //战机Z轴:左右倾斜,受手机负X轴影响;位移飞机之后,故取逆
            euler.z = Mathf.Lerp(euler.z, -(Input.acceleration.x * maxTurnLaen), 0.2f);
            //四元数线性差值,更新旋转角度
            //a原始角度  b新角度  t敏感度
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(euler), sensitivity);
        }
    
    
    }
  • 相关阅读:
    mysql
    jvm--动态对象年龄判定
    JVM中的STW和CMS
    jvm中的垃圾收集器serial、parNew、parallel Scavenge;serial old、parallel old、CMS、G1
    concurrenthashmap在1.8和1.7里面有什么区别
    java中的深浅拷贝
    JDK动态代理和CGLIB代理的区别
    java 锁的底层原理
    死锁
    jvm新生代和老年代分别使用什么垃圾回收算法
  • 原文地址:https://www.cnblogs.com/WindMan/p/9050810.html
Copyright © 2011-2022 走看看