基于unity的2d动画制作----基于c#语言开发,类似于《DNF》的2d界面,目前只有一个游戏场景。成果图UI如下图所示
游戏成果视频已经上传B站:
2dAnimation游戏
游戏开发主要步骤:
1.素材收集(来自Unity的Asset Store)
2.UI设计(随心所欲)
3.刚体碰撞规则等(csharp代码)
Hierarchy的整体结构如下:每个物体的名称和它的英文名大概一致。
脚本构造如下:
部分PlayerController的代码如下:
`//author:刘家诚:date:2020.10.16
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:刘家诚:date:2020.10.16
public class PlayerController : MonoBehaviour
{//控制角色的移动,生命,动画等public float speed = 5f;//移动速度private int maxHealth = 5;//最大生命值private int currentHealth;//当前生命值private float invicibleTime = 2f;//无敌时间private float invincibleTimer;//无敌计时器private bool isInvincible;//是否无敌public GameObject bulletPrefab;//子弹//=====玩家的朝向=====private Vector2 lookDirection = new Vector2(1, 0);//不希望公开,但希望访问属性值public int MyMaxHealth{get{return maxHealth;}}public int MyCurrentHealth{get{return currentHealth;}}Rigidbody2D rbody;Animator anim;// Start is called before the first frame updatevoid Start(){//获取刚体组件rbody = GetComponent<Rigidbody2D>();currentHealth = 2;//初始生命值invincibleTimer = 0;rbody = GetComponent<Rigidbody2D>();anim = GetComponent<Animator>();}// Update is called once per framevoid Update(){//Time.deltaTime是计算机渲染一帧所需时间float moveX = Input.GetAxisRaw("Horizontal");//控制水平移动方向A:-1,D:1float moveY = Input.GetAxisRaw("Vertical");//控制水平移动方向w:1,s:-1Vector2 moveVector = new Vector2(moveX, moveY);//防止松手朝向改变if (moveVector.x != 0 || moveVector.y != 0){lookDirection = moveVector;}anim.SetFloat("Look X", lookDirection.x);anim.SetFloat("Look Y",lookDirection.y);anim.SetFloat("Speed", moveVector.magnitude);//根据向量大小来判断//=========移动==========Vector2 position = rbody.position;//position.x += moveX * speed * Time.deltaTime;// position.y += moveY * speed * Time.deltaTime;position += moveVector * speed * Time.deltaTime;rbody.MovePosition(position);//实现刚体的移动//==========无敌记时==========if (isInvincible){invincibleTimer -= Time.deltaTime;if (invincibleTimer < 0){isInvincible = false;//记录时间,2s后无敌时间消失}}//========按下j键进行攻击=========if (Input.GetKeyDown(KeyCode.J)){anim.SetTrigger("Launch"); GameObject bullet = Instantiate(bulletPrefab, rbody.position+Vector2.up*0.5f, Quaternion.identity);BulletController bc = bullet.GetComponent<BulletController>();if (bc != null){bc.Move(lookDirection, 300);}}}//改变玩家生命值public void ChangeHealth(int amount){//收到伤害if (amount < 0){if (isInvincible == true)//如果是无敌,则退出{return;}isInvincible = true;//变成无敌invincibleTimer = invicibleTime;}Debug.Log(currentHealth + "/" + maxHealth);//用Mathf约束一下currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);Debug.Log(currentHealth + "/" + maxHealth);}//与玩家的碰撞检测}```csharp
在这里插入代码片
EnemyController的部分代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:刘家诚:date:2020.10.16
//敌人控制相关
public class EnemyController : MonoBehaviour
{private float speed = 7;private Rigidbody2D rbody;//是否垂直public bool isVertical;public float changeDirectionTime = 2;private float changeTimer;//计时器private Vector2 moveDirection;//移动方向private Animator anim;private bool isFixed;//是否被修复// Start is called before the first frame updatevoid Start(){rbody = GetComponent<Rigidbody2D>();//获取刚体moveDirection = isVertical ? Vector2.up : Vector2.right;//判断是否垂直changeTimer = changeDirectionTime;anim = GetComponent<Animator>();//获取animisFixed = false;}// Update is called once per framevoid Update(){//被修复,不执行以下所有代码if (isFixed) return;changeTimer -= Time.deltaTime;if (changeTimer < 0){moveDirection *= -1;changeTimer = changeDirectionTime;}Vector2 position = rbody.position;position.x += moveDirection.x * speed * Time.deltaTime;position.y += moveDirection.y * speed * Time.deltaTime;rbody.MovePosition(position);anim.SetFloat("movex", moveDirection. x);anim.SetFloat("movey", moveDirection.y);}private void OnCollisionEnter2D(Collision2D collision){PlayerController pc = collision.gameObject.GetComponent<PlayerController>();if (pc != null){pc.ChangeHealth(-1);}}public void Fixed(){isFixed = true;rbody.simulated = false;anim.SetTrigger("fix");//播放被修复的动画}
}
BulletController的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:刘家诚:date:2020.10.16
/// <summary>
/// 控制子弹的移动
/// </summary>
public class BulletController : MonoBehaviour
{Rigidbody2D rbody;// Start is called before the first frame updatevoid Awake(){rbody = GetComponent<Rigidbody2D>();Destroy(this.gameObject, 2f);}// Update is called once per framevoid Update(){}public void Move(Vector2 moveDirection,float moveForce){rbody.AddForce(moveDirection * moveForce);}//=====碰撞检测=====private void OnCollisionEnter2D(Collision2D collision){EnemyController ec = collision.gameObject.GetComponent<EnemyController>();if (ec != null){Debug.Log("碰到敌人了");ec.Fixed();//修复敌人}Destroy(this.gameObject);}}
Dangerous的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:刘家诚:date:2020.10.16
public class Dangerous : MonoBehaviour
{//伤害陷阱// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}public void OnTriggerStay2D(Collider2D collision){PlayerController pc = collision.GetComponent<PlayerController>();if (pc != null){pc.ChangeHealth(-1);}}
}
Collectable的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:刘家诚:date:2020.10.16
public class CollectAble : MonoBehaviour
{//草莓被玩家碰撞时检测的相关类// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){}private void OnTriggerEnter2D(Collider2D collision){//检测到有PlayerControllerPlayerController pc = collision.GetComponent<PlayerController>();if (pc != null){if (pc.MyCurrentHealth < pc.MyMaxHealth) {pc.ChangeHealth(1);Destroy(this.gameObject);}}}
}