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 ...
随机推荐
- 我是实践派之mongo的一主多从
mongo一主多从 为什么要做一主多从? mongodb天生就是为了分布式而生的,为了保证数据读写分离和数据安全,把数据放在不同的机子上,可以减少主节点的读压力,而让从节点去承受读请求压力. 主节点用 ...
- Python中Generators教程
转载至:https://www.bytelang.com/article/content/NQbmUaRIXyA= 要想创建一个iterator,必须实现一个有__iter__()和__next__( ...
- php之简单的文件管理(基本功能)
(1)先要想好要操作哪个文件? (2)确定文件的路径? (3)要有什么文件管理功能? 一.先做一下简单的查看文件功能,文件中的文件和文件夹都显示,但是双击文件夹可以显示下一级子目录,双击"返 ...
- zabbix 布署实践【7 H3C网络设备监控模版制作思路】
我们知道,zabbix安装后自带Template OS Linux 模版已满足了绝大部分Linux服务器的基础环境监控,只是我们在其模版上稍微修改,可配合将SWAP监控取消,另存为一个叫OS Linu ...
- Python学习--10 面向对象编程
面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 本节对于面向对象的概念不做 ...
- Microsoft Edge与Google Chrome那些不同的举止
以下针对14393版本Edge与Chrome 54 html dom/select的如果options里没有符合的值时edge会选择第一个,chrome(54)会置空选项 html dom/input ...
- 【Linux学习三】Linux系统目录架构
主要包括: ●bin:保存的是可执行文件,二进制,就是命令 ●boot:引导目录,操作系统的启动加载,包含版本内核文件.greb引导程序- ●dev:硬件设备文件,如硬盘.网卡.声卡.终端.显卡,每一 ...
- tab切换☆☆☆☆☆
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- Tomcat启动极慢问题
启动后,catalina.out日志中会有如下打印: INFO: Creation of SecureRandom instance ,] milliseconds 具体原因,有兴趣的可以自己百度一下 ...
- pyhon的数据类型
1.数字 整型和浮点型 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647在64位系统上,整数的位数为64位,取值范围为-2** ...