新建工程
导入资源 导入NGUI3.6.8
声音转化为2D声音,apply一下
1,选中图片>右键NGUI>Open Atlas Maker>点击>Create创建图集放在文件夹下面.
NGUI>Open>Prefab Toolbar>拖一个background到Hierarchy视图中>切换到2D模式>用W工具>将 BackGround删除
2,选择UI Root>在Scene中右键Create创建Sprite>在Atlas中选择图集,讲图片调整成背景大小>将Anchors的Type设置为Unified,调整上下左右的距离为0;
把摄像机调小:选Gizmos>调整3D Gizmos的大小
创建一个空的GameObject下可以放置图片集
Play按钮,在Scene中右击选Attach>boxCollider;再Attach>Button Script;在UIButton的Sprites中的pressed中选择按下之后的图片效果
写switch方法的时候连按两下tap键,然后输入对应的枚举对象,按下回车键,编辑器会自动帮我们写出所有的case;
///游戏主菜单控制
public class GameMenuController : MonoBehaviour {
public GameObject menuPanel;
public GameObject operationPanel;
public GameObject diffcultyPanel;
public GameObject characterSelectPanel;
public Color operationPanelColor;
public void OnMenuPlay() {
menuPanel.SetActive(false);
operationPanel.SetActive(true);
}
public void OnOperationPlay() {
operationPanel.GetComponent<UISprite>().color = operationPanelColor;
diffcultyPanel.SetActive(true);
}
public void OnDiffcultyClick() {
operationPanel.SetActive(false);
diffcultyPanel.SetActive(false);
characterSelectPanel.SetActive(true);
}
public void OnCharacterSelect() {
Application.LoadLevel(1);
}
}
给背景分一下层,下面的层最先显示
卡车:将图片设置为2D精灵
选择一个图片精灵>在属性中点击Sprite Editor进行编辑
动画中两张精灵的位置有偏差可以通过修改精灵中心点的位置
///汽车的运动
public class Car : MonoBehaviour {
public Vector3 targetPos;
public Vector3 endTargetPos;
public int smoothing = 2;
public Wheel[] wheelArray;//三个车轮
public Damper damper;//门板转动
private bool isReaching = false;//表示是否达到目标位置
void Start() {
Invoke("PlaySound", 0.3f);
}
void Update () {
transform.position = Vector3.Lerp(transform.position, targetPos, smoothing * Time.deltaTime);
if (isReaching == false) {
if (Vector3.Distance(transform.position, targetPos) < 0.4f) {
isReaching = true;
OnReach();//达到目标位置之后,进行的操作
}
}
}
void PlaySound() {
audio.Play();
}
void OnReach() {
foreach (Wheel w in wheelArray) {
w.Stop();
}
damper.StartRotate();
//放下主角 TODO
//把车子开走
Invoke("GoOut", 1f);
}
void GoOut() {
targetPos = endTargetPos;
foreach (Wheel w in wheelArray) {
w.Start();
}
Destroy(this.gameObject, 1f);
}
}
///车轮旋转
public class Wheel : MonoBehaviour {
public float speed = 200;
private bool isRotate = true;
void Update () {
if(isRotate)
transform.Rotate(-Vector3.forward * speed * Time.deltaTime);
}
public void Stop() {
isRotate = false;
}
public void Start() {
isRotate = true;
}
}
///门板的旋转
public class Damper : MonoBehaviour {
public float speed = 120;
private bool isRotate = false;
private float angle = 0;
void Update () {
if (isRotate) {
angle += speed * Time.deltaTime;
transform.Rotate(Vector3.forward * speed * Time.deltaTime);
if (angle > 135) {
isRotate = false;
}
}
}
public void StartRotate() {
isRotate = true;
}
}
///角色在地面上的行走射击和蹲下行走射击共用一套代码
using UnityEngine;
using System.Collections;
public enum AnimStatus {
Idle,
Walk
}
public class PlayerGround : MonoBehaviour {
public float animSpeed = 10;//1秒播放10帧图片
private float animTimeInterval = 0;
public AnimStatus status = AnimStatus.Idle;//表示主角当前的状态
public SpriteRenderer upRenderer;//上半身的渲染器
public SpriteRenderer downRenderer;//下半身的渲染器
public Sprite[] idleUpSpriteArray;
private int idleUpIndex = 0;
private int idleUpLength = 0;
private float idleUpTimer = 0;
public Sprite idleDownSprite;
public Sprite[] walkUpSpriteArray;
private int walkUpIndex = 0;
private int walkUpLength = 0;
private float walkUpTimer = 0;
public Sprite[] walkDownSpriteArray;
private int walkDownIndex = 0;
private int walkDownLength = 0;
private float walkDownTimer = 0;
// Use this for initialization
void Start () {
animTimeInterval = 1 / animSpeed;//得到每一帧的时间间隔
idleUpLength = idleUpSpriteArray.Length;//上身动画精灵数
walkUpLength = walkUpSpriteArray.Length;
walkDownLength = walkDownSpriteArray.Length;
}
void Update () {
//判断主角的状态以及播放动画
switch (status) {
case AnimStatus.Idle:
idleUpTimer += Time.deltaTime;
if (idleUpTimer > animTimeInterval) {
idleUpTimer -= animTimeInterval;//当计时器减去一个周期的时间
idleUpIndex++;//当帧数自增(播放下一帧)
idleUpIndex %= idleUpLength;//(判断是否到达最大帧数)
upRenderer.sprite = idleUpSpriteArray[idleUpIndex];
}
downRenderer.sprite = idleDownSprite;
break;
case AnimStatus.Walk:
walkUpTimer += Time.deltaTime;
if (walkUpTimer > animTimeInterval) {
walkUpTimer -= animTimeInterval;
walkUpIndex++;
walkUpIndex %= walkUpLength;
upRenderer.sprite = walkUpSpriteArray[walkUpIndex];
}
walkDownTimer += Time.deltaTime;
if (walkDownTimer > animTimeInterval) {
walkDownTimer -= animTimeInterval;
walkDownIndex++;
walkDownIndex %= walkDownLength;
downRenderer.sprite = walkDownSpriteArray[walkDownIndex];
}
break;
}
}
public void Shoot(float v_h, bool isTopKeyDown, bool isBottomKeyDown) {
}
}
///角色的跳跃和射击动作
public class PlayerJump : MonoBehaviour {
public float animSpeed = 10;//1秒播放10帧图片
private float animTimeInterval = 0;
public AnimStatus status = AnimStatus.Idle;//表示主角当前的状态
public SpriteRenderer upRenderer;//上半身的渲染器
public SpriteRenderer downRenderer;//下半身的渲染器
public Sprite[] idleUpSpriteArray;
private int idleUpIndex = 0;
private int idleUpLength = 0;
private float idleUpTimer = 0;
public Sprite[] idleDownSpriteArray;
private int idleDownIndex = 0;
private int idleDownLength = 0;
private float idleDownTimer = 0;
public Sprite shootUpSprite;
public Sprite shootHorizontalSprite;
private bool shoot = false;
private ShootDir shootDir;//射击的方向
public GameObject projectilePrefab;
public Transform shootupPos;//射击的位置
public Transform shoothorizontalPos;
void Start() {
animTimeInterval = 1 / animSpeed;//得到每一帧的时间间隔
idleUpLength = idleUpSpriteArray.Length;
idleDownLength = idleDownSpriteArray.Length;
}
void Update() {
switch (status) {
case AnimStatus.Idle:
idleUpTimer += Time.deltaTime;
if (idleUpTimer > animTimeInterval) {//播放下一帧
idleUpTimer -= animTimeInterval;//当计时器减去一个周期的时间
idleUpIndex++;//当帧数自增(播放下一帧)
idleUpIndex %= idleUpLength;//(判断是否到达最大帧数)
upRenderer.sprite = idleUpSpriteArray[idleUpIndex];
}
idleDownTimer += Time.deltaTime;
if (idleDownTimer > animTimeInterval) {//播放下一帧
idleDownTimer -= animTimeInterval;//当计时器减去一个周期的时间
idleDownIndex++;//当帧数自增(播放下一帧)
idleDownIndex %= idleDownLength;//(判断是否到达最大帧数)
downRenderer.sprite = idleDownSpriteArray[idleDownIndex];
}
break;
}
}
void LateUpdate() {
if (shoot) {
shoot = false;
//进行射击//判断射击的位置
Vector3 pos = Vector3.zero;
if(shootDir==ShootDir.Top){
pos = shootupPos.position;
}
else if(shootDir==ShootDir.Left||shootDir==ShootDir.Right){
pos = shoothorizontalPos.position;
}
//判断射击的方向
int z_rotation=0;
switch (shootDir)
{
case ShootDir.Left:
upRenderer.sprite = shootHorizontalSprite;
z_rotation=180;
break;
case ShootDir.Right:
upRenderer.sprite = shootHorizontalSprite;
z_rotation=0;
break;
case ShootDir.Top:
upRenderer.sprite = shootUpSprite;
z_rotation=90;
break;
case ShootDir.Down:
z_rotation=270;
break;
default:
break;
}
GameObject.Instantiate(projectilePrefab, pos, Quaternion.Euler(0, 0, z_rotation));
}
}
//射击的方法
public void Shoot(float v_h, bool isTopKeyDown, bool isBottomKeyDown) {
shoot = true;
//得到射击的方向
if (isTopKeyDown == false && isBottomKeyDown == false) {
if (transform.localScale.x == 1) {
shootDir = ShootDir.Left;
} else if (transform.localScale.x == -1) {
shootDir = ShootDir.Right;
}
}
else {
if (isTopKeyDown) {
shootDir = ShootDir.Top;
}
else if (isBottomKeyDown) {
shootDir = ShootDir.Down;
}
}
}
}
//子弹
public class PlayerProjectile : MonoBehaviour {
public float speed = 10;
void Start() {
Destroy(this.gameObject, 3);
}
void Update () {
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
}
///进行射击
using UnityEngine;
using System.Collections;
public enum ShootDir{
Left,
Right,
Top,
Down
}
public class PlayerShoot : MonoBehaviour {
public int shootRate = 7;//代表每秒可以射击的次数
public PlayerGround playerGround;
public PlayerDown playerDown;
public PlayerJump playerJump;
private float shootTimeInterval = 0;
private float timer = 0;
private bool canShoot = true;
private PlayerMove playerMove;
private bool isTopKeyDown = false;
private bool isBottomKeyDown = false;
void Start() {
shootTimeInterval = 1f / shootRate;
playerMove = this.GetComponent<PlayerMove>();
}
void Update () {
if (canShoot == false) {
timer += Time.deltaTime;
if (timer >= shootTimeInterval) {
canShoot = true;
timer -= shootTimeInterval;
}
}
if (Input.GetKeyDown(KeyCode.W)) {
isTopKeyDown = true;
}
if (Input.GetKeyUp(KeyCode.W)) {
isTopKeyDown = false;
}
if (Input.GetKeyDown(KeyCode.S)) {
isBottomKeyDown = true;
}
if (Input.GetKeyUp(KeyCode.S)) {
isBottomKeyDown = false;
}
if (canShoot && Input.GetKeyDown(KeyCode.J)) {
//进行射击的操作
audio.Play();
switch (playerMove.state) {
case PlayerState.PlayerGround:
playerGround.Shoot(rigidbody.velocity.x, isTopKeyDown, isBottomKeyDown);
break;
case PlayerState.PlayerDown:
playerDown.Shoot(rigidbody.velocity.x, isTopKeyDown, isBottomKeyDown);
break;
case PlayerState.PlayerJump:
playerJump.Shoot(rigidbody.velocity.x, isTopKeyDown, isBottomKeyDown);
break;
}
}
}
}
///角色的移动
using UnityEngine;
using System.Collections;
public enum PlayerState {
PlayerGround,
PlayerDown,
PlayerJump
}
public class PlayerMove : MonoBehaviour {
public float speed = 3;
public float jumpSpeed = 3;
public PlayerState state = PlayerState.PlayerJump;
private bool isGround = false;
private int groundLayerMask;
private bool isBottomKeyClick = false;
public PlayerGround playerGround;
public PlayerDown playerDown;
public PlayerJump playerJump;
void Start() {
groundLayerMask = LayerMask.GetMask("Ground");
}
void Update () {
if (Input.GetKeyDown(KeyCode.S)) {
isBottomKeyClick = true;
}
if (Input.GetKeyUp(KeyCode.S)) {
isBottomKeyClick = false;
}
float h = Input.GetAxis("Horizontal");
Vector3 v = rigidbody.velocity;
rigidbody.velocity = new Vector3(h * speed, v.y, v.z);
v = rigidbody.velocity;
RaycastHit hitinfo;
isGround = Physics.Raycast(transform.position + Vector3.up * 0.1f, Vector3.down, out hitinfo, 0.2f, groundLayerMask);
//判断当前主角的状态 跳起 蹲下 正常
if (isGround == false) {
state = PlayerState.PlayerJump;
} else {
if (isBottomKeyClick) {
state = PlayerState.PlayerDown;
} else {
state = PlayerState.PlayerGround;
}
}
//控制主角的跳跃
if (isGround && Input.GetKeyDown(KeyCode.K)) {
rigidbody.velocity = new Vector3(v.x, jumpSpeed, v.z);
}
//根据状态来判定启用哪一个游戏状态
switch (state) {
case PlayerState.PlayerDown:
playerDown.gameObject.SetActive(true);
playerJump.gameObject.SetActive(false);
playerGround.gameObject.SetActive(false);
break;
case PlayerState.PlayerGround:
playerDown.gameObject.SetActive(false);
playerJump.gameObject.SetActive(false);
playerGround.gameObject.SetActive(true);
break;
case PlayerState.PlayerJump:
playerDown.gameObject.SetActive(false);
playerJump.gameObject.SetActive(true);
playerGround.gameObject.SetActive(false);
break;
}
//控制主角的朝向
float x = 1;
if (rigidbody.velocity.x > 0.05f) {
x = -1;
} else if (rigidbody.velocity.x < -0.05f) {
x = 1;
} else {
x = 0;
}
if (x != 0) {
playerGround.transform.localScale= new Vector3(x, 1, 1);
playerJump.transform.localScale = new Vector3(x, 1, 1);
playerDown.transform.localScale = new Vector3(x, 1, 1);
}
//控制主角在idle和walk状态的切换
if (Mathf.Abs(rigidbody.velocity.x) > 0.05f) {
playerGround.status = AnimStatus.Walk;
playerDown.status = AnimStatus.Walk;
} else {
playerGround.status = AnimStatus.Idle;
playerDown.status = AnimStatus.Idle;
}
if (Mathf.Abs(rigidbody.velocity.x) > 0.05f)
{
playerGround.status = AnimStatus.Walk;
playerDown.status = AnimStatus.Walk;
}
else
{
playerGround.status = AnimStatus.Idle;
playerDown.status = AnimStatus.Idle;
}
}
}