zoukankan      html  css  js  c++  java
  • Unity3D学习笔记(三):V3、运动、帧率、OnGUI

    盯着看:盯住一个点
    transform.LookAt(Vector3 worldPosition);
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class LookGirl : MonoBehaviour {
        GameObject girl;
           // Use this for initialization
           void Start () {
            girl = GameObject.Find("Girl");
        }
           
           // Update is called once per frame
           void Update () {
            //盯着看:盯住一个点
            transform.LookAt(girl.transform.position);
            //环绕旋转:point点,axis轴,angle每帧旋转角度
            transform.RotateAround(girl.transform.position,Vector3.up,1);
           }
    }
     
    环绕旋转:point点,axis轴,angle每帧旋转角度
    transform.RotateAround(Vector3 point, Vector3 axis, float angle);
    显示在检视面板的任何属性,可以直接修改。即使脚本变量也可以改变,而无需修改脚本本身。你可以在检视面板运行时修改变量,进行试验调试你的游戏。在脚本中,如果你定义了一个公共变量的对象类型(如游戏物体或变换),你可以拖动一个游戏物体或预制到检视面板对应的槽。
    公转
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    //可以在检视面板暴露枚举
    public enum Sex
    {
    Man,
    Woman
    }
    public class TurnPublic : MonoBehaviour {
        //在检视面板暴露一个对象变量,让游戏物体可以拖拽赋值
        public GameObject target;
        //在检视面板暴露Transform组件
        public Transform trans;
        //检视面板可以暴露以下
        public int level;
        public float health;
        public string path;
        public bool canMove;
        public Sex sex;
        public int[] array;
           // Use this for initialization
           void Start () {
                  
           }
           
           // Update is called once per frame
           void Update () {
            transform.RotateAround(target.transform.position, target.transform.up, 1);
        }
    }

    自转

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class TurnSelf : MonoBehaviour {
           // Use this for initialization
           void Start () {
                  
           }
           
           // Update is called once per frame
           void Update () {
            transform.Rotate(transform.up);
        }
    }
    监听鼠标和键盘的按下事件
    bool:Input.GetKey(KeyCode KeyCode)
    bool:Input.GetMouseButtonDown(int index)
    轴值
    Size:添加元素
    Horizontal:水平轴
    Vertical:垂直轴
    Mouse X:鼠标水平位移的轴值映射
    Mouse Y:鼠标垂直位移的轴值映射
    Mouse ScrollWheel:鼠标滚轮的轴值映射
    Time时间类,
    Time.deltaTime:代表每一帧所消耗的时间,平移的过程中让速度和此量相乘,就从每帧多少米变成每秒多少米。(两帧之间的间隔)
    Time.time:代表着从游戏一开始运行到当前时间点所经过的总时长,是从Time.deltaTime累加上来的。
    Time.fixedDeltaTime:每一物理帧所消耗的时间。
    Time.timeScale:时间缩放,可以影响Time.deltaTime,FixedUpdate,fixedTime。如果写在Update里的代码想要和时间缩放挂钩,就和Time.timeScale发生关系。(可以用作游戏暂停)
    Time.fixedTime:累加Time.deltaTime并乘以时间缩放,时间缩放为0,则值为0。
     
    Mathf是数学的结构体,其方法均为静态
    Deg2Rad:角度转弧度(2是to的意思)* PI / 180
    Rad2Deg:弧度转角度
    Abs:绝对值
    sin:正弦,Sin(1/2)=30度
    cos:余弦
    tan:正切
    Asin:反正弦,Sin(1/2)=60度
    Acos:反余弦
    Atan:反正切
    Ceil:向上取整数
    Floor:向下取整数
    Clamp:限值,Clamp(05),输入5.1,返回5,(可以把坐标限值在一定范围内,防止越界)
    Clamp01:
    Lerp:线性插值
    log:取对数
    Movetowards:是插值的一种
    Pow(float f , float p):f的p次方
    Sqrt:开平方
    PI:3.1415926
    Max:最大值
    Min:最小值
    三角函数曲线
    三角函数分镜
    16:9等比缩放
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class InputTest : MonoBehaviour
    {
        //拖拽资源文件夹里的
        public Texture image;
        int sum;
        int curNum;
        float h;
        float v;
        float mouse_h;
        float mouse_v;
        public float m_JumpSpeed = 400f;
        private bool m_jumping = false;
        private Rigidbody m_Rigidbody;
        private Animator m_Anim;
        private void Awake()
        {
            m_Anim = GetComponent<Animator>();
            m_Rigidbody = GetComponent<Rigidbody>();
        }
        private bool jumped = true;
        // Use this for initialization
        void Start()
        {
     
        }
        // Update is called once per frame
        void Update()
        {
            //实时监听
            //GetKey按住,GetKey.Down按下,GetKey.Up抬起,KeyCode键盘按键的枚举项
            if (Input.GetKey(KeyCode.W))
            {
                transform.Translate(Vector3.forward);
            }
            if (Input.GetKey(KeyCode.S))
            {
                transform.Translate(Vector3.back);
            }
            if (Input.GetKey(KeyCode.A))
            {
                transform.Rotate(Vector3.down);
            }
            if (Input.GetKey(KeyCode.D))
            {
                transform.Rotate(Vector3.up);
            }    
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("FUCK");
            }
            //轴值映射
            //GetAxis获取轴值,显示轴值的变化(-1~1)
            //水平:Horizontal
            //垂直:Vertical
            h = Input.GetAxis("Horizontal");
            v = Input.GetAxis("Vertical");
            //按帧移动,和电脑性能有关,帧速快的电脑,移动速快
            transform.Translate(Vector3.forward * 0.1f * v);
            transform.Rotate(Vector3.down * 1.0f * h);
            if (transform.position.z <-50 || transform.position.z > 50)
            {
                transform.position += new Vector3(0, 0, Mathf.Clamp(transform.position.z, -40, 40));
            }
            else
            {
                transform.Translate(Vector3.forward * 0.1f * v);
            }
            //鼠标有灵活度,根据快慢分等级,显示轴值的变化不是固定的
            mouse_h = Input.GetAxis("Mouse X");
            mouse_v = Input.GetAxis("Mouse Y");
            //按秒移动,和电脑性能无关,把每帧多少米变成每秒多少米
            //Time.deltaTime每帧刷新所需要的时间
            //Time.time是Time.deltaTime的累加(从游戏开始到现在,大致经过的时间),
            //Time.fixedDeltaTime是每一物理帧的时间间隔,固定值0.02s
            transform.Translate(Vector3.forward * 0.1f * v);
            transform.Rotate(Vector3.down * 1.0f * h);
            //每秒走的路程 = 每帧走1米(速度) * 每帧消耗的时间1/60秒(时间)
            transform.Translate(Vector3.forward * Time.deltaTime);
            //用Mathf.Clamp函数限值坐标的取值范围
            Clamp(float value, float min, float max);
            Mathf.Clamp(transform.position.x, -40, 40);
            Mathf.Clamp(transform.position.z, -40, 40);
            #region 限值物体移动范围
            #endregion
        }
    }
    
    //OnGUI商业已不用,现主要用于调试
    void OnGUI()
    {
        //两种方式:自定义布局方式,自动排版方式
        //自动排版方式
        if (curNum <= 1000)
        {
            sum += curNum;
            curNum++;
        }
        GUILayout.Label("当前和:" + sum);
        GUILayout.Button("FUCK");
        GUILayout.Box(image);
        GUILayout.Label("Horizontal:" + h);
        GUILayout.Label("Vertical:" + v);
        //GUILayout.Label("Mouse X:" + mouse_h);
        //GUILayout.Label("Mouse Y:" + mouse_v);
        GUILayout.Label("Time.deltaTime:" + Time.deltaTime);
        GUILayout.Label("Time.time:" + Time.time);
        GUILayout.Label("Time.fixedDeltaTime:" + Time.fixedDeltaTime);
        if (GUILayout.Button("FUCK"))
        {
            //timeScale是时间缩放,时间变慢了1倍(变成原来的0.5倍),只能缩放Time.deltaTime
            Time.timeScale = 0.5f;
        }
    }

    复习

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    // MonoBehaviour ->  Behaviour -> Component -> Object
    public class W05d4 : MonoBehaviour
    {
        public GameObject obj;   // Cube
        // Use this for initialization
        void Start()
        {
            // 如何获取组件
            // Component -> GetComponent
            Camera cam = GetComponent<Camera>();
            if (cam != null)
            {
                cam.depth = 0;
            }
            // 如何添加组件
            GameObject->AddComponent
            // 先寻找Cube
            Move cubeMove = obj.AddComponent<Move>();    // 返回值:添加的那个组件的对象引用
            cubeMove.moveSpeed = 0.1f;
            // 删除组件
            // UnityEngine.Object -> Destroy
            GameObject findObj = GameObject.Find("Player");
            if (findObj != null)
            {
                // 把移动速度变成0.5
                // 1 获取Move组件
                Move cubeMove = findObj.GetComponent<Move>();
                if (cubeMove != null)
                {
                    // 2 改Move组件的变量
                    cubeMove.moveSpeed = 0.5f;
                }
            }
            GameObject findObj = GameObject.Find("Player/Capsule/FirePos");
            if (findObj)    // findObj != null
            {
                Light light = findObj.AddComponent<Light>();
                light.type = LightType.Spot;
            }
        }
        // Update is called once per frame
        void Update()
        {
        }
    }
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class PlayerControl : MonoBehaviour
    {
        public Texture texture;
        float h;
        float v;
        float hRaw;
        float vRaw;
        float mouseH;
        float mouseV;
        float mouseScrollWheel;
        public float moveSpeed = 0.1f;
        public float rotSpeed = 1f;
        // Use this for initialization
        void Start()
        {
        }
        void Update()
        {
            transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime * Input.GetAxisRaw("Vertical"));
            transform.Rotate(Vector3.up * rotSpeed * Time.deltaTime * Input.GetAxisRaw("Horizontal"));
        }
        // Update is called once per frame
        void Update1()
        {
            // 按键输入
            // 1、键盘的 键值
            if (Input.GetKey(KeyCode.A))   // 按着A的时候 每帧都会检测到
            {
            }
            if (Input.GetKeyDown(KeyCode.A))   // 按下A的那一帧 会检测到
            {
            }
            if (Input.GetKeyUp(KeyCode.A))     // 抬起A的那一帧 会检测到
            {
            }
            // 2、       轴值
            // 按A、D 或者 方向键左、方向键右的时候会返回一个float值, 如果不按会返回0
            // 会有一个渐变的过程 假设我按了A 这时返回值会从 0 逐渐变化到 -1
            //                            D               0            1
            // 受时间缩放影响的
            h = Input.GetAxis("Horizontal");
            v = Input.GetAxis("Vertical");
            //不受时间缩放的影响
            mouseH = Input.GetAxis("Mouse X");
            mouseV = Input.GetAxis("Mouse Y");
            mouseScrollWheel = Input.GetAxis("Mouse ScrollWheel");
            //没有渐变的过程 按下A hRaw 变成了-1 按下D hRaw 变成了 1
            hRaw = Input.GetAxisRaw("Horizontal");
            vRaw = Input.GetAxisRaw("Vertical");
            // 3、       功能键
            if (Input.GetButtonDown("Jump"))
            {
                Debug.Log("功能键 Jump 被按下");
            }
            // 鼠标输入
            // 0 左键 1 右键 2 中键
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("鼠标左键 被按下");
            }
        }
        private void OnGUI()
        {
            GUI.Label(new Rect(100, 100, 200, 150), "Hello World");
            if (GUI.Button(new Rect(100, 300, 200, 150), "Button"))
            {
                GameObject target = GameObject.Find("Target1");
                if(target)
                {
                    transform.LookAt(target.transform);
                }
                GameObject target2 = GameObject.Find("Target2");
                if(target2)
                {
                    target2.transform.LookAt(transform);
                }
            }
            if(GUI.Button(new Rect(10, 10, 80, 80), texture))
            {
            }
            GUI.Label(new Rect(0, 0, 80, 50), "Width :" + Screen.width);
            GUI.Label(new Rect(0, 60, 80, 50), "Height :" + Screen.height);
            //GUI.DrawTexture(new Rect(10, 10, 80, 80), texture);
            //GUILayout.Label("H : " + h);
            //GUILayout.Label("mouseH : " + mouseH);
            //GUILayout.Label("mouseV : " + mouseV);
            //GUILayout.Label("mouseScrollWheel : " + mouseScrollWheel);
            //GUILayout.Label("hRaw : " + hRaw);
            //GUILayout.Label("vRaw : " + vRaw);
            //if (GUILayout.Button("TimeScale" + Time.timeScale))
            //{
            //    Time.timeScale = 1 - Time.timeScale;
            //}
        }
    }

    练习题

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Question : MonoBehaviour
    {
        public GameObject objA;
        public GameObject objB;
        public const float TimeInterval = 0.5f;   // 冷却时间
        public float timeCount = 0;
        public float currentTime = 0;           // 记录当前的Time.time
        #region Q1_如何注视
        // 1、游戏物体A 注视 游戏物体B
        // 2、当前脚本挂载的游戏物体 注视 游戏物体A
        #endregion
        #region Q2_如何围绕旋转
        // 1、游戏物体A 围绕 游戏物体B 绕世界坐标系的 Y轴 旋转  --每秒旋转60度
        // 2、游戏物体A 围绕 游戏物体B 的 Y轴 旋转
        #endregion
        #region Q3_响应键盘按键事件
        // 操控 当前挂载脚本的 游戏物体
        // 1、按下 数字键0 发射子弹(伪代码替代)
        // 2、按下 空格键 跳跃(伪代码替代)
        // 3、按下 W、S键 前后移动
        // 4、使用轴值的方式控制物体 绕世界的Y轴 旋转
        // 5、使用轴值的方式控制物体 绕自身的Y轴 旋转
        // 6、使用鼠标的XY轴值控制物体的 俯仰角(绕X轴旋转的角度)、偏航角(绕Y轴旋转的角度) //注: 俯仰角、偏航角、滚转角 --欧拉角/姿态角
        // 7、按下鼠标左键 发射子弹(伪代码替代)
        // 8、通过鼠标滚轮 实现物体 前后移动(自身坐标系)
        #endregion
        #region Q4_Time类 OnGUI
        // 1、按下鼠标右键可以发射子弹,子弹的发射间隔为 0.5秒
        // 2、使用自动布局的方式绘制按钮,按钮被按下以后,可以暂停游戏
        #endregion
        #region Q5_Mathf类
        // 1、求绝对值
        // 2、开方
        // 3、如何限制一个变量的取值范围
        // 4、向上、向下取整
        // 5、正弦、余弦函数
        #endregion
        void Start()
        {
            currentTime = Time.time;
        }
        void Update()
        {
            #region Q1_如何注视_Answer
            // 1、游戏物体A 注视 游戏物体B
            objA.transform.LookAt(objB.transform);
            // 2、当前脚本挂载的游戏物体 注视 游戏物体A
            transform.LookAt(objA.transform);
            #endregion
            #region Q2_如何围绕旋转_Answer
            // 1、游戏物体A 围绕 游戏物体B 绕世界坐标系的 Y轴 旋转  每秒旋转60度
            objA.transform.RotateAround(objB.transform.position, Vector3.up, 60 * Time.deltaTime);
            // 2、游戏物体A 围绕 游戏物体B 的 Y轴 旋转
            // objB.transform.up  物体自身的Y轴 在世界坐标系中的 向量
            objA.transform.RotateAround(objB.transform.position, objB.transform.up, 60 * Time.deltaTime);
            #endregion
            #region Q3_响应键盘按键事件_Answer
            // 1、按下 数字键0 发射子弹(伪代码替代)
            if (Input.GetKeyDown(KeyCode.Alpha0))
            {
                // 发射子弹
            }
            // 2、按下 空格键 跳跃(伪代码替代)
            if (Input.GetKeyDown(KeyCode.Space))
            {
                // 跳跃
            }
            // 3、按下 W、S键 前后移动
            if (Input.GetKey(KeyCode.W))
            {
                transform.Translate(Vector3.forward * 1 * Time.deltaTime, Space.Self);       // 往自身的前方
                transform.Translate(transform.forward * 1 * Time.deltaTime, Space.World);    // 往自身的前方
                transform.Translate(Vector3.forward * 1 * Time.deltaTime, Space.World);      // 往世界的前方
            }
            else if (Input.GetKey(KeyCode.S))
            {
                transform.Translate(Vector3.back * 1 * Time.deltaTime);
            }
            // 4、使用轴值的方式控制物体 绕世界的Y轴 旋转
            float h = Input.GetAxis("Horizontal");
            transform.Rotate(Vector3.up * 1 * Time.deltaTime * h, Space.World);    // 世界
            // 5、使用轴值的方式控制物体 绕自身的Y轴 旋转
            transform.Rotate(Vector3.up * 1 * Time.deltaTime * h, Space.Self);    // 自身  第2个参数不指定,默认 Space.Self
            // 6、使用鼠标的XY轴值控制物体的 俯仰角(绕X轴旋转的角度)、偏航角(绕Y轴旋转的角度) //注: 俯仰角、偏航角、滚转角 --欧拉角/姿态角
            float mouseX = Input.GetAxis("Mouse X");   // 偏航角
            float mouseY = Input.GetAxis("Mouse Y");
            Vector3 eu = transform.eulerAngles;
            eu.y += mouseX;
            eu.x += mouseY;
            transform.eulerAngles = eu;
            // 7、按下鼠标左键 发射子弹(伪代码替代)
            if (Input.GetMouseButtonDown(0))
            {
                // 发射子弹
            }
            // 8、通过鼠标滚轮 实现物体 前后移动(自身坐标系)
            float msw = Input.GetAxis("Mouse ScrollWheel");
            transform.Translate(Vector3.forward * 1 * Time.deltaTime * msw);
            #endregion
            #region Q4_Time类 OnGUI_Answer
            // 1、按下鼠标右键可以发射子弹(伪代码),子弹的发射间隔为 0.5秒
            timeCount += Time.deltaTime;
            if (timeCount >= TimeInterval)
            {
                if (Input.GetMouseButtonDown(1))
                {
                    // 发射子弹
                    // 重置时间
                    timeCount = 0;
                }
            }
            if (Time.time - currentTime >= TimeInterval)
            {
                // 发射子弹
                if (Input.GetMouseButtonDown(1))
                {
                    // 发射子弹
                    // 重置时间
                    currentTime = Time.time;
                }
            }
            // 2、使用自动布局的方式绘制按钮,按钮被按下以后,可以暂停游戏
            #endregion
            #region Q5_Mathf类_Answer
            // 1、求绝对值
            float num1 = -10.2f;
            //num1 = Mathf.Abs(num1);
            // 2、开方
            //num1 = Mathf.Sqrt(num1);
            // 3、如何限制一个变量的取值范围
            num1 = Mathf.Clamp(num1, 0, 100);
            // 4、向上、向下取整
            num1 = Mathf.Ceil(num1);     // 向上取整  -10
            num1 = Mathf.Floor(num1);    // 向下取整  -11
            // 5、正弦、余弦函数
            num1 = Mathf.Sin(num1);
            num1 = Mathf.Cos(num1);
            #endregion
        }
        private void OnGUI()
        {
            // 2、使用自动布局的方式绘制按钮,按钮被按下以后,可以暂停游戏
            if (GUILayout.Button("暂停游戏"))
            {
                Time.timeScale = 0;
            }
        }
    }
  • 相关阅读:
    UIView添加手势
    UIView常见属性设置汇总
    关于页面传值
    有关segue的简介
    alloc
    如何定义静态方法
    一座小城
    清明
    开通博客
    iOS学习之界面间传值
  • 原文地址:https://www.cnblogs.com/vuciao/p/10362759.html
Copyright © 2011-2022 走看看