1.血条注视屏幕
- void Update () {
- transform.LookAt(Camera.main.transform);
- }
2.角色变色
- GetComponent<MeshRenderer>().material.color = Color.blue;
3.随机生成8个不同角度的敌人
- public GameObject enemy;
- public int enemyCount = 8;
- for (int i = 0; i < enemyCount; i++) {
- float randomX = Random.Range(-5.0f, 5.0f);
- float randomY = Random.Range(0, 360.0f);
- float randomZ = Random.Range(-5.0f, 5.0f);
- Vector3 pos = transform.position+new Vector3(randomX,0,randomZ);
- Quaternion rot = Quaternion.Euler(0, Random.Range(0f, 360f), 0);
- GameObject enemyClone = Instantiate(enemy,pos,rot) as GameObject;
- NetworkServer.Spawn(enemyClone);
4.掉血
PlayerHealth脚本
- public int healthMax = 100; //最大血量
- public int healthCurrent; //当前血量
- void Start() {
- healthCurrent = healthMax; //当前血量等于最大血量
- }
- public void HurtMe(int damage) { //血条减少
- healthCurrent -= damage;
- if (healthCurrent <= 0) { //如果小于次血量
- healthCurrent = healthMax; //满血复活
- }
- healthBar.sizeDelta = new Vector2(healthCurrent, healthBar.sizeDelta.y); //血条变了
- }
Bullet脚本
- public class Bullet : MonoBehaviour {
- private void OnCollisionEnter(Collision collision) {
- PlayerHealth playerHealth= collision.gameObject.GetComponent<PlayerHealth>();
- playerHealth.HurtMe(10);
- Destroy(this.gameObject);}}
5.角色人物移动
- public float traSpeed = 3; //移动的速度
- public float rotSpeed = 120; //一秒旋转的角度
- void Update () {
- float h = Input.GetAxis("Horizontal");
- float v = Input.GetAxis("Vertical");
- transform.Translate(Vector3.forward * v * traSpeed * Time.deltaTime); //朝某个方向移动 transform.Rotate(Vector3.up * h * rotSpeed * Time.deltaTime); //围绕某轴旋转
6.player发射
- public GameObject bulletPre; //子弹的prefab
- public Transform bulletTrans; //生成子弹的位置
- void Update(){
- if (Input.GetKeyDown(KeyCode.Space))
- {
- ameObject bullet = Instantiate(bulletPre, bulletTrans.position, Quaternion.identity) as GameObject;
- bullet.GetComponent<Rigidbody>().velocity = transform.forward * 10;
- Destroy(bullet, 2); //2秒后销毁子弹 } }}
7.跨类调用脚本
- private <span style="color:#33cc00;">GameControl</span> shoots; //GameControl是需要调用脚本的类
- void Start() {
- shoots = GameObject.Find("GameController").GetComponent<GameControl>(); //物体名,脚本名
- }
- void Ffff(){
- shoots. ()
- }
8.射线
- if (Input.GetMouseButtonDown(0)) {
- //Vector3 rayPosition = new Vector3(Camera.main.pixelWidth * 0.5f, Screen.height * 0.5f);
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //定义准备一条射线
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit)) {
- if (hit.transform.tag == "bbb") {
- Instantiate(g,hit.transform.position, Quaternion.identity);
- }
- }
- }
注意要在加碰撞器
- Camera.main.ScreenPointToRay(Input.mousePosition) 这里是从鼠标点击的位置垂直在game窗口生成一条射线,用于点击物体生成物体
- Vector3 rayPosition = new Vector3(Camera.main.pixelWidth * 0.5f, Screen.height * 0.5f);
- Ray ray = Camera.main.ScreenPointToRay(rayPosition); 从game窗口屏幕中间垂直生成一条射线,用于FPS,准星位置
9.防止连续点击按钮实例化出多个预置体
- bool isBuilding=false;
- if (isBuilding == false) {
- Instantiate(g, hit.transform.position, Quaternion.identity);
- isBuilding = true;
- }
10.同一个场景切换游戏界面
Gamecontrolle脚本:
- bool started = false;
然后在开始进行游戏的前面加上:
- if (! started)
- {
- return;
- }
11.使用数组清除一些数据
- ArrayList pot2Arr;
- void Start()
- {
- pot2Arr = new ArrayList();
- }
- if (item.movable)
- {
- Item pottt=CreatePot(pot2, item.rowIndex, item.columnIndex);
- pot2Arr.Add(pottt);
- public void Startgame(){
- for(int i = 0; i < pot2Arr.Count; i++)
- {
- Item pot2 = pot2Arr[i] as Item;
- Destroy(pot2.gameObject);
- }
- pot2Arr = new ArrayList();}
*************************************************
人物靠近门时,自动开门:
- private void OnTriggerEnter(Collider other) {
- if (other.transform.tag == "Player") {
- transform.position = transform.position + new Vector3(0, 0, -2.0f);
- Invoke("Guanmen", 3.0f);
- }
- }
- void Guanmen() {
- transform.position = transform.position + new Vector3(0, 0, 2.0f);
- }