Unity-3d Day03
尼瑪今天研究一天腳本啊??? 這個(gè)坑啊
好多東西要記而且搞不太清楚哪能改哪是固定的??
多用用應(yīng)該會(huì)好很多吧
這個(gè)是函數(shù)在腳本里的執(zhí)行順序
using UnityEngine; using System.Collections;public class HelloScript : MonoBehaviour {//初始化寫在awake或startprivate GameObject gameobj;private GameObject[] gameob;void Awake(){Debug.Log("Hello!~");print("awake");}void OnEnable(){print("onEnable");}// Use this for initializationvoid Start(){print("start1");//active = false; //攝像機(jī)不啟動(dòng)//gameobj = GameObject.Find("Cube");gameob = GameObject.FindGameObjectsWithTag("Player");}//一般寫一些力啊 什么的 物理方面的void FixedUpdate(){print("fixedpudate");}// Update is called once per frame//實(shí)時(shí)刷新的寫在updatevoid 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 表示一個(gè)單一的行為。Unity中用戶對(duì)游戲?qū)ο蟮牟僮鞅环指畛扇舾蓚€(gè)
單一行為,每個(gè)單一行為都作為一MonoBehaviour類來封裝。繼承自MonoBehaviour的類,不需要自己創(chuàng)建它
的實(shí)例,也不能自己創(chuàng)建(如 new 類名)。因?yàn)樗袕腗onoBehaviour繼承過來的類,unity都會(huì)自動(dòng)創(chuàng)建實(shí)例,并且調(diào)用被重載的方
法,如我們經(jīng)常用到的Awake,Start, Update等。而普通類,可以用new來創(chuàng)建實(shí)例了。
Gameobject類:常用方法:
SetActive( bool value)
Find( String name)
FindWithTag( string tag)
FindGameObjectsWithTag( string tag)
Input類:常用的? 有鍵盤輸入和鼠標(biāo)輸入
void Update () {//鼠標(biāo)輸入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;?
鍵盤輸入控制角色的另一種方式,是不是有點(diǎn)屌
void Update () {float horizontal = Input.GetAxis("Horizontal");float vertical = Input.GetAxis("Vertical");transform.position += Vector3.forward * vertical;transform.position += Vector3.right * horizontal;}
今天呢還研究了一下簡單的跟隨? 類似游戲里的寵物的行為
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/little-sun/p/4367279.html
總結(jié)
以上是生活随笔為你收集整理的Unity-3d Day03的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript之值传递与引用传递
- 下一篇: Monkey与MonkeyRunner之