0 GP
Unity3d-筆記
作者:Red月│2018-01-19 15:02:23│巴幣:0│人氣:172
- 1、3D人物移動旋轉 (鍵盤A、D、W、S)
- float translation = Input.GetAxis("Vertical") * 10f *Time.deltaTime;
- float rotation = Input.GetAxis("Horizontal") * 100f *Time.deltaTime;
- transform.Translate(0, 0, translation);
- transform.Rotate(0, rotation, 0);
- 2、3D人物跟隨目標前移動
- if (transform.position == target)
- transform.position = target;
- else
- transform.LookAt (target);
- transform.position = Vector3.MoveTowards(transform.position,target , 0.2f);
- 3、滑鼠左鍵點擊座標(Vector3)
- Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
- RaycastHit hit;
- if (Input.GetMouseButtonDown (0))
- {
- if (Physics.Raycast (ray, out hit))
- target = new Vector3(hit.point.x, 0 ,hit.point.z);
- }
- 4、生成子物件附在父物件上
- public Transform trs;
- public Image mag;
- void Start ()
- {
- trs = GameObject.Find ("Canvas").transform;
- mag = Instantiate (mag ,new Vector3 (0, 0, 0), Quaternion.identity);
- mag.transform.SetParent (trs.transform);
- }
- 5、迴圈函式
- float starttime;
- float looptime;
- void Start ()
- {
- InvokeRepeating("TestPoints",starttime,looptime);
- }
- void TestPoints(){}
- 6、攝影機跟隨目標
- public Transform target;
- void Update ()
- {
- transform.position=newVector3(target.position.x,target.position.y,target.position.z);
- transform.rotation = Quaternion.Euler (30, 0, 0);
- }
- 7、多人連線
- using UnityEngine.Networking;
- public class NetWork : NetworkBehaviour
- {
- void Start ()
- {
- if(!isLocalPlayer)
- GetComponent<PlayerMove> ().enabled = false;
- }
- }
- 8、Resources Sprite圖片
- public Sprite s;
- public Sprite[] myall;
- void Start ()
- {
- s = Resources.Load ("1", typeof(Sprite)) as Sprite;
- myall = Resources.LoadAll<Sprite> ("");
- }
- 9、Color 循環
- public Image lerpedColor;
- void Update()
- {
- lerpedColor.color = Color.Lerp(Color.white, Color.clear,
- Mathf.PingPong(Time.time, 0.5f));
- }
- 10、物件開關
- gameObject.SetActive(true);
- gameObject.enabled = false;
- 11、物件拖移
- IDragHandler ,IBeginDragHandler
- Vector3 pos;
- public void OnBeginDrag(PointerEventData eventData)
- {
- pos = new Vector3(transform.position.x - Input.mousePosition.x,
- transform.position.y - Input.mousePosition.y);
- }
- public void OnDrag(PointerEventData eventData)
- {
- transform.position = (Input.mousePosition + pos);
- }
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3859497
All rights reserved. 版權所有,保留一切權利