Android Game Examples

时间:2023-03-09 13:26:56
Android Game Examples

Game2

 using UnityEngine;
 using System.Collections;

 public class Game2_Player : MonoBehaviour {

     public GameObject m_bulletPrefab;   //子弹预设
     private Transform m_fireTransform;  //子弹的发射位置

     private Camera m_mainCamera; //摄像机

     public float m_moveSpeed = 5.0f;    //移动速度
     private Vector3 m_moveDirection;    //移动方向

     private Vector3 m_rotateFrom;   //旋转起点
     private Vector3 m_rotateTo; //

     public float m_fireInterval = 0.3f;   //射击时间间隔
     private float m_fireTimer;  //射击计时器

     private AudioSource m_audioSource;

     void Start () {
         m_mainCamera = Camera.main;
         m_fireTransform = transform.Find("BulletSpawner");
         m_audioSource = GetComponent<AudioSource>();
     }

     void Update () {
         Move();
         Rotate();
         )) {
             Fire();
         }
     }

     void LateUpdate() {
         //相机跟随玩家
         m_mainCamera.transform.position = ,transform.position.z);
     }

     //移动
     void Move() {
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
          || v != ) {
             m_moveDirection = ,v);
         } else {
             m_moveDirection = Vector3.zero;
         }
         transform.Translate(m_moveDirection * m_moveSpeed * Time.smoothDeltaTime,Space.World);
     }

     //旋转
     void Rotate() {
         transform.rotation = Quaternion.LookRotation(GetRotation());
     }

     //射击
     void Fire() {
         if(m_fireTimer >= m_fireInterval) {
             m_fireTimer = 0f;
             GameObject bulletGo = Instantiate(m_bulletPrefab,m_fireTransform.position,Quaternion.LookRotation(GetRotation())) as GameObject;
             if(!m_audioSource.isPlaying) {
                 m_audioSource.Play();
             }
         } else {
             m_fireTimer += Time.smoothDeltaTime;
         }
     }

     //获取旋转角度
     Vector3 GetRotation() {
         //视角从Y轴向下看,x轴不变,y轴为z轴.旋转起点为屏幕*,旋转终点为鼠标在屏幕中的位置
         m_rotateFrom = ,Screen.height * 0.5f);
         m_rotateTo = Input.mousePosition;
         m_rotateTo.z = m_rotateTo.y;
         m_rotateTo.y = ;

         //获得旋转方向
         Vector3 rotateDirection = m_rotateTo - m_rotateFrom;
         return rotateDirection;
     }
 }

Game2_Player

 using UnityEngine;
 using System.Collections;

 public class Game2_Enemy : MonoBehaviour {

     public float m_moveSpeed = 5.0f;    //移动速度

     private Transform m_playerTransform;    //玩家的位置
     private Rigidbody m_rigidBody;

     ;   //血量

     void Start () {
         m_playerTransform = GameObject.Find("Player").transform;
         m_rigidBody = GetComponent<Rigidbody>();

     }

     void Update () {
         if(Vector3.Distance(m_playerTransform.position,transform.position) > 1.0f) {
             Chase();
         } else {
             //m_rigidBody.Sleep();
         }
     }

     void OnDrawGizmos() {
         Gizmos.color = Color.red;
         Gizmos.DrawLine(transform.position,transform.position + transform.forward);
     }

     //追逐玩家
     void Chase() {
         transform.rotation = Quaternion.LookRotation(m_playerTransform.position - transform.position);
         transform.Translate(Vector3.forward * m_moveSpeed * Time.smoothDeltaTime);
     }

     //被打中
     public void OnHit(int damage) {
         ) {
             m_hp -= ;
         } else {
             Destroy(gameObject);
         }
     }

 }

Game2_Enemy

 using UnityEngine;
 using System.Collections;

 public class Game2_EnemySpawner : MonoBehaviour {

     public GameObject m_enemyPrefab;    //敌人预制
     public float m_spawnTime = 10.0f;   //生成敌人时间间隔
     private float m_startTime;  //初始时间

     void Start() {
         m_startTime = Time.time;
     }

     void Update() {
         if(Time.time - m_startTime >= m_spawnTime) {
             Spawn();
             m_startTime = Time.time;
         }
     }

     void OnDrawGizmos() {
         Gizmos.color = Color.green;
         Gizmos.DrawSphere(transform.position,0.2f);
     }

     //每10秒生成一次敌人
     void Spawn() {
         Instantiate(m_enemyPrefab,transform.position,Quaternion.identity);
     }

 }

Game2_EnemySpawner

 using UnityEngine;
 using System.Collections;

 public class Game2_Bullet : MonoBehaviour {

     public float m_moveSpeed = 30.0f;   //移动速度

     void Start () {
         StartCoroutine(Destroy());
     }

     void Update () {
         Move();
     }

     //子弹碰到敌人,减血
     void OnCollisionEnter(Collision collision) {
         if(collision.collider.CompareTag("Enemy")) {
             collision.gameObject.GetComponent<Game2_Enemy>().OnHit();
             Destroy(gameObject);
         }
     }

     //移动子弹
     void Move() {
         transform.Translate(Vector3.forward * m_moveSpeed * Time.smoothDeltaTime);
     }

     //删除子弹
     IEnumerator Destroy(float delayTime) {
         yield return new WaitForSeconds(delayTime);
         Destroy(gameObject);
     }
 }

Game2_Bullet

Android Game Examples

项目:https://pan.baidu.com/s/1o7UqEPs