• Unity-3d Day03


    尼玛今天研究一天脚本啊    这个坑啊

    好多东西要记而且搞不太清楚哪能改哪是固定的  

    多用用应该会好很多吧

    这个是函数在脚本里的执行顺序

    using UnityEngine;
    using System.Collections;
    
    public class HelloScript : MonoBehaviour
    {
        //初始化写在awake或start
        private GameObject gameobj;
        private GameObject[] gameob;
        void Awake()
        {
            Debug.Log("Hello!~");
            print("awake");
        }
        void OnEnable()
        {
            print("onEnable");
        }
    
        // Use this for initialization
        void Start()
        {
            print("start1");
    
            //active = false;   //摄像机不启动
            //gameobj = GameObject.Find("Cube");
            gameob = GameObject.FindGameObjectsWithTag("Player");
    
        }
        //一般写一些力啊  什么的  物理方面的
        void FixedUpdate()
        {
            print("fixedpudate");
        }
        // Update is called once per frame
        //实时刷新的写在update
        void Update()
        {
            print("update");
            //gameobj.transform.Rotate(0f, 1f, 2f);
            foreach (GameObject item in gameob)
            {
                item.transform.Rotate(0f, 1f, 2f);
            }
        }
        void LateUpdate()
        {
            print("lateupdate");
        }
        void OnGUI()
        {
            print("onGUI");
        }
        void OnDisable()
        {
            print("onDisable");
        }
        void OnDestroy()
        {
            print("ondestroy");
        }
    
    }

    MonoBehavior类:
    MonoBehaviour 表示一个单一的行为。Unity中用户对游戏对象的操作被分割成若干个
    单一行为,每个单一行为都作为一MonoBehaviour类来封装。继承自MonoBehaviour的类,不需要自己创建它
    的实例,也不能自己创建(如 new 类名)。因为所有从MonoBehaviour继承过来的类,unity都会自动创建实例,并且调用被重载的方
    法,如我们经常用到的Awake,Start, Update等。而普通类,可以用new来创建实例了。

    Gameobject类:常用方法:

      SetActive( bool value)
      Find( String name)
      FindWithTag( string tag)
      FindGameObjectsWithTag( string tag)

    Input类:常用的  有键盘输入和鼠标输入

    void Update () {
    
            //鼠标输入
            if (Input.GetMouseButtonDown(0))
            {
                print("左键");
            }
            if (Input.GetMouseButton(1))
            {
                print("右键");
            }
            if (Input.GetMouseButton(2))
            {
                print("中键");
            }
            
    }
    void Update () {
    
            //键盘输入
            if (Input.GetKey(KeyCode.W))
            {
                transform.Translate(0f, 0f, -1f);
            }
            if (Input.GetKey(KeyCode.A))
            {
                transform.Translate(-1f, 0f, 0f);
            }
            if (Input.GetKey(KeyCode.S))
            {
                transform.Translate(0f, 0f, 1f);
            }
            if (Input.GetKey(KeyCode.D))
            {
                transform.Translate(1f, 0f, 0f);
            }
    
    }

    缓慢走的方法:

            Vector3 source = sphere.transform.position;
            Vector3 target = transform.position;
            Vector3 position = Vector3.Lerp(source, target, Time.deltaTime);
            sphere.transform.position = position;

    键盘输入控制角色的另一种方式,是不是有点屌

    void Update () {
    
            float horizontal = Input.GetAxis("Horizontal");
            float vertical = Input.GetAxis("Vertical");
            transform.position += Vector3.forward * vertical;
            transform.position += Vector3.right * horizontal;
        }


    今天呢还研究了一下简单的跟随  类似游戏里的宠物的行为

    void Update()
        {if (Vector3.Distance(transform.position, master.transform.position) > 4)
            {
                transform.LookAt(master.transform.position);
                transform.Translate(Vector3.forward);
            }
    
        }


     

  • 相关阅读:
    简单工厂模式&工厂方法模式&抽象工厂模式的区别及优缺点及使用场景
    JDK1.8的新特性
    在Button样式中添加EventSetter,理解路由事件
    关于C#低版本升级高版本时,项目中引用Microsoft.Office.Interop.Word,程序提示不存在类型或命名空间名office.
    无法安装或运行此应用程序。该应用程序要求首先在"全局程序集缓存(GAC)"中安装程序集
    C#winform跨窗体传值和调用事件的办法
    C#线程处理:七、线程实列
    C#线程处理:六、线程同步(三)
    C#线程处理:五、线程同步(二)
    C#线程处理:四、线程同步
  • 原文地址:https://www.cnblogs.com/little-sun/p/4367279.html
Copyright © 2020-2023  润新知