zoukankan      html  css  js  c++  java
  • 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);
            }
    
        }


     

  • 相关阅读:
    jQuery中的观察者模式(Observer Pattern)
    jQuery异步获取json数据的2种方式
    jQuery制作水平多级下拉菜单
    简单说明CGI和动态请求是什么
    五种IO模型透彻分析
    不可不知的socket和TCP连接过程
    零复制(zero copy)技术
    编译httpd细节
    ansible编译httpd playbook示例
    翻译:CREATE DATABASE语句
  • 原文地址:https://www.cnblogs.com/little-sun/p/4367279.html
Copyright © 2011-2022 走看看