控制相机跟随程序:
1 public class Follow : MonoBehaviour { 2 3 public Transform Player; 4 private Vector3 offset; 5 void Start () { 6 offset = transform.position - Player.position; 7 } 8 9 void Update () { 10 transform.position = Player.position + offset; 11 12 } 13 }
将该脚本挂载在照相机下,offset为相机和小球之间的空间距离,照相机的新位置为相机的位置和offset的矢量化和
控制小球移动程序:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 public class Move : MonoBehaviour { 7 private Rigidbody SpereRig; 8 private float ad; 9 private float sw; 10 public float Speed = 5; 11 private float score = 0; 12 13 public Text text; 14 public GameObject WinText; 15 void Start () { 16 SpereRig = GetComponent<Rigidbody>(); 17 } 18 void Update () { 19 ad = Input.GetAxis("Horizontal"); 20 sw = Input.GetAxis("Vertical"); 21 SpereRig.AddForce(new Vector3(ad,0,sw)*Speed); 23 } 24 //private void OnCollisionEnter(Collision collision) 25 //{ 26 // string name = collision.collider.name; 27 28 // if(collision.collider.tag == "PickUp") 29 // { 30 // Destroy(collision.collider.gameObject); 31 // } 32 //} 33 private void OnTriggerEnter(Collider other) 34 { 35 if(other.tag == "PickUp") 36 { 37 Destroy(other.gameObject); 38 score++; 39 text.text = score.ToString(); 40 if (score == 9) 41 { 42 WinText.SetActive(true); 43 } 44 } 45 } 46 }
三种检测方式:碰撞检测,触发检测,射线检测
SetActive()方法现实对象
ToString()方法将变量转换为字符串
小球旋转程序:
1 void Update () { 2 transform.Rotate(new Vector3(0, 1, 0)); 3 //调用一次旋转一度,一秒调用60次(FPS=60),即每秒旋转60度 4 }
心得体会:
文件打包生成两个文件,一个数据文件,一个可执行程序,运行时两个文件必须在一个文件目录下
设置为预制体后:给预制体添加组件和代码,则所有的场景中的预制体对象都将含有该主件和代码