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

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

Android Game Examples的更多相关文章

  1. Android添加代码检查权限

    1,首先创建一个项目,然后创建一个类,hello.java public class hello { public static final String PERMISSION_SAY_HELLO = ...

  2. [转载]Android开发必备的21个免费资源和工具

    转载自: http://blog.csdn.net/shimiso/article/details/6788375 Android移动开发平台现在不是一个“火”字能形容的,今年Android平台在市场 ...

  3. [Artoolkit] ARToolKit's SDK Structure on Android

    Most applications on Android are developed in Java, and Android provides a rich framework of classes ...

  4. Android Volley 库的使用

    本文内容 什么是 Volley 库 Volley 能做什么 Volley 架构 环境 演示 Volley 库的使用 参考资料 Android 关于网络操作一般都会介绍 HttpClient 以及 Ht ...

  5. Android Volley 库通过网络获取 JSON 数据

    本文内容 什么是 Volley 库 Volley 能做什么 Volley 架构 环境 演示 Volley 库通过网络获取 JSON 数据 参考资料 Android 关于网络操作一般都会介绍 HttpC ...

  6. COSC2309/2347 Semester 1, 2019

    Mobile Application DevelopmentCOSC2309/2347 Semester 1, 2019Movie Night PlannerAssignment 1 (20 mark ...

  7. FGX Native library功能介绍

    Hot news from the fields of the cross-platform library "FGX Native" development. New Engli ...

  8. [转]Java Code Examples for android.util.JsonReader

    [转]Java Code Examples for android.util.JsonReader The following are top voted examples for showing h ...

  9. bazel build //tensorflow/examples/android:tensorflow_demo报错: fatal error: 'cuda_runtime.h' file not found

    In file included from ./third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:external/eigen_archive/u ...

随机推荐

  1. ADS的go to命令

    我们有时候在一个函数右键没有看到“go to”选项,这是因为没有Make,要先Make之后才能使用go to 命令

  2. php memcached缓存集群

    一.需求描述 一linode xen vps 1G RAM,40+sites,IO频繁,openfiles ulimit已65535 US时间访问量大增,nginx不定时502 二.解决办法 page ...

  3. oracle 11g使用deferred_segment_creation 延迟段创建特性时遇到的问题总结

    总结,下面是两个问题.问题1是用户可以在所有表空间创建表;问题2是exp不能导出空表 问题1: 版本:oracle 11.2.0.1.0 select * from v$version; 创建用户aa ...

  4. 图的匹配问题与最大流问题(三)——最大流问题Ford-Fulkerson方法Java实现

    上篇文章主要介绍了Ford-Fulkerson方法的理论基础,本篇给出一种Java的实现. 先借助伪代码熟悉下流程 FORD-FULKERSON(G,t,s) 1 for each edge(u,v) ...

  5. 第2章 开始入手 —— 01 创建第一个 Android 应用程序

    创建一个新的 Android 项目 操作步骤: (1) 选择 File | New | Android Application Project ,弹出 New Android Application ...

  6. Netty轻量级对象池实现分析

    什么是对象池技术?对象池应用在哪些地方? 对象池其实就是缓存一些对象从而避免大量创建同一个类型的对象,类似线程池的概念.对象池缓存了一些已经创建好的对象,避免需要时才创建对象,同时限制了实例的个数.池 ...

  7. 开发团队在TFS中使用Git Repository (一)

    在研发团队中,代码版本管理是最为基础的必要工具.个人使用过的版本管理工具有SVN.VSS.ClearCase.TFS.Git,从团队的角度和使用角度来说,个人倾向于与使用TFS作为团队的基础工具.首先 ...

  8. ubuntu16 网络设置

    打开Ubuntu的终端,输入:sudo gedit /etc/network/interfaces表示使用gedit编辑器打开interfaces文件. 在打开的文件中,若有内容,先全部删除.然后输入 ...

  9. c#注释

    c#的注释分为:这里不能不说一下什么是注释. 注释本身不会执行,只是说明性文字,只供程序员阅读. 注释又分为:单行注释,多行注释,文档注释. 单行注释://开始 多行注释:/*开始, */结束. 文档 ...

  10. 一步步优化JVM四:决定Java堆的大小以及内存占用

    到目前为止,还没有做明确的优化工作.只是做了初始化选择工作,比如说:JVM部署模型.JVM运行环境.收集哪些垃圾回收器的信息以及需要遵守垃圾回收原则.这一步将介绍如何评估应用需要的内存大小以及Java ...