自己做的一个超级简单的小游戏
2017-04-14 10:14
309 查看
自己做了一个简单的uniy3d小游戏
方向键控制一个小球躲避别的小球,撞到指定的墙胜利,被别的球撞到失败。
初学者练手
Playermove
Enemy
move
Islose_Trigger
Spawn
Camera
follow_player
UI
Wall
IsWin
游戏图片
代码
player
player_move 控制小球移动
using System.Collections; using System.Collections.Generic; using UnityEngine; public class playermove : MonoBehaviour { public float speed = 20f; Vector3 movement; Rigidbody playerRigidbody; void Awake() { playerRigidbody=GetComponent<Rigidbody>(); //获取player的Rigidbody } void FixedUpdate() { float h = Input.GetAxisRaw("Horizontal"); float v = Input.GetAxisRaw("Vertical"); move(h,v); } void move(float h,float v) { movement.Set(h,0f,v); //移动的方向 movement = movement.normalized * speed * Time.deltaTime;//normalized :将向量长度归一 transform.position = movement + transform.position; playerRigidbody.MovePosition(transform.position); } /* 手机中重力感应控制小球行动(不知道能不能实现) void Update() { Vector3 dir = Vector3.zero; dir.x = -Input.acceleration.y; dir.z = Input.acceleration.x; if (dir.sqrMagnitude > 1) dir.Normalize(); dir *= Time.deltaTime; transform.Translate(dir * speed); } */ }
Camera
Camerafollow 摄像机跟随小球
using System.Collections; using System.Collections.Generic; using UnityEngine; public class camerafollow : MonoBehaviour { public Transform target; public float speed = 5f; Vector3 offset; void Awake() { offset = transform.position - target.position; } void FixedUpdate() { Vector3 targetcampos = target.position + offset; transform.position = Vector3.Lerp(transform.position,targetcampos,speed*Time.deltaTime); } }
Enemy
Enemy_move
敌人移动(跟随小球)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class enemymove : MonoBehaviour { Transform player; UnityEngine.AI.NavMeshAgent nav; //要在unity编辑器中给enemy设置NavMeshAgent void Awake() { player = GameObject.FindGameObjectWithTag("Player").transform; nav = GetComponent<UnityEngine.AI.NavMeshAgent>(); } // Use this for initialization void Start () { } // Update is called once per frame void Update () { nav.SetDestination(player.position); //给敌人设置自动寻路的目标 } }
Islose
如果player撞到enemy,显示lose,并且将playermove设置为false
Enemy的碰撞器上的IsTrigger打开
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Islose : MonoBehaviour { public GameObject player; playermove playermove; Text text; // Use this for initialization void Awake() { text = GameObject.Find("Canvas/Text").GetComponent<Text>(); playermove = player.GetComponent<playermove>(); } // Update is called once per frame void Update() { } void OnTriggerEnter(Collider other)//如果撞到则。。。 { if (other.gameObject == player) { text.text = "LOSE!"; playermove.enabled = false; } } }
Spawnenemy
生成敌人
using System.Collections; using System.Collections.Generic; using UnityEngine; public class spwanenemy : MonoBehaviour { public GameObject enemy; //敌人 public float spawntime = 3f; //生成速度 public Transform[] spawnpoints;//敌人刷新点 // Use this for initialization void Start () { 4000 InvokeRepeating("Spawn",spawntime,1f); //InvokeRepeating("函数名称",调用时间,调用间隔) //InvokeRepeating函数用来重复调用某一函数 } void Spawn() { int spawnPointIndex = Random.Range(0, spawnpoints.Length); Instantiate(enemy, spawnpoints[spawnPointIndex].position, spawnpoints[spawnPointIndex].rotation); } }
Wall
IsWin
小球撞到指定的墙上,显示Win
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //导入UI包 public class Iswin : MonoBehaviour { public GameObject player; playermove playermove; Text text; void Awake() { text = GameObject.Find("Canvas/Text").GetComponent<Text>(); playermove = player.GetComponent<playermove>(); } void Update () { } void OnTriggerEnter(Collider other) { if (other.gameObject == player) { text.text = "WIN!"; playermove.enabled = false; } } }
代码参考官方教程
新手练手,还请大拿多多指教
相关文章推荐
- 不再使用自动编号了。自己写了一个Id生成器,超级简单
- 不再使用自动编号了。自己写了一个Id生成器,超级简单
- 一个简单的联动菜单!简单,超级简单
- 自己写的一个简单得shell得菜单
- ::自己写的一个超简单的ASP分页代码::
- 自己设计的一个简单高速加密算法
- 自己做的一个小游戏《刹那》
- 那时的ID:ZTE南研BBS上,大家简单地在自己的id前缀上一个//,不用注册,也不会有人盗用,黄金时代!
- 想自己做一个简单的搜索引擎
- 自己写了一个简单的数据库访问类
- 自己动手,丰衣足食——一个简单却高效的图像旋转算法
- 自己写的一个简单mt图片幻灯类
- 原来这么简单就可以申请Blog了,害我还自己做了一个~
- 转发一个小游戏:看看自己像哪位名人?
- 自己做的一个简单的用户管理(PHP)!
- 一个超级简单的网页播放器
- 自己写的一个JSP小游戏21点,记录一下
- 发布自己写的一个简单的新闻列表WebPart
- 自己写的一个简单ASP调用存储过程查询
- 自己写的一个简单的IP组类