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
项目:https://pan.baidu.com/s/1o7UqEPs
Android Game Examples的更多相关文章
- Android添加代码检查权限
1,首先创建一个项目,然后创建一个类,hello.java public class hello { public static final String PERMISSION_SAY_HELLO = ...
- [转载]Android开发必备的21个免费资源和工具
转载自: http://blog.csdn.net/shimiso/article/details/6788375 Android移动开发平台现在不是一个“火”字能形容的,今年Android平台在市场 ...
- [Artoolkit] ARToolKit's SDK Structure on Android
Most applications on Android are developed in Java, and Android provides a rich framework of classes ...
- Android Volley 库的使用
本文内容 什么是 Volley 库 Volley 能做什么 Volley 架构 环境 演示 Volley 库的使用 参考资料 Android 关于网络操作一般都会介绍 HttpClient 以及 Ht ...
- Android Volley 库通过网络获取 JSON 数据
本文内容 什么是 Volley 库 Volley 能做什么 Volley 架构 环境 演示 Volley 库通过网络获取 JSON 数据 参考资料 Android 关于网络操作一般都会介绍 HttpC ...
- COSC2309/2347 Semester 1, 2019
Mobile Application DevelopmentCOSC2309/2347 Semester 1, 2019Movie Night PlannerAssignment 1 (20 mark ...
- FGX Native library功能介绍
Hot news from the fields of the cross-platform library "FGX Native" development. New Engli ...
- [转]Java Code Examples for android.util.JsonReader
[转]Java Code Examples for android.util.JsonReader The following are top voted examples for showing h ...
- 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 ...
随机推荐
- Python学习入门基础教程(learning Python)--6.4 Python的list与函数
list是python下的一种数据类型,他和其他类型如整形.浮点型.字符串等数据类型一样也可作为函数的型参和实参来使用! 1.list作为参数 list数据类型可以作为函数的参数传递给函数取做相应的处 ...
- BIOS详解:什么是BIOS ?BIOS的作用?CMOS及其与BIOS的关系?
1.什么是BIOS ? BIOS是英文Basic Input Output System的缩略语,直译过来后中文名称就是基本输入输出系统.它的全称应该是ROM-BIOS,意思是只读存储器基本输入输出系 ...
- 官网.jar包下载技巧
最近在学Java,但是往往要用到好多jar包,只能从网上下载,但是网上找实在太费事了,而且有时候给我们带来了许多的不便.找不到jar包程序就运行不了,心里真不是个滋味.可是自己就是不甘心.经过我的不懈 ...
- zoom:1的作用
兼容IE6.IE7.IE8浏览器,经常会遇到一些问题,可以使用zoom:1来解决,有如下作用:触发IE浏览器的haslayout解决ie下的浮动,margin重叠等一些问题. 如下使用DIV做一行两列 ...
- 【.NET-EF】Entity Framework学习笔记1 - VS2013没有EF的解决方法
解决方法:我本来也没有,百度了一下,在C:\ProgramData\Package Cache\{08AEF86A-1956-4846-B906-B01350E96E30}v12.0.20912.0\ ...
- trove命令翻译(上)(只做翻译,未实验效果)
The trove client is the command-line interface (CLI) for the Database service API and its extensions ...
- java基础<迷你DVD系统>
一.写在main方法中 import java.util.*; public class MyDVDMgr { public static void main(String[] args){ Scan ...
- C# EnumHelper
using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection ...
- Graph - leetcode [图]
207. Course Schedule 有向图的环检测 bool canFinish(int numCourses, vector<pair<int, int>>& ...
- 我喜欢的快捷键 webstorm
1.打开设置 ctrl+alt+s 2.重命名 rename ctrl+r