基于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 update
void Start()
{
//获取刚体组件
rbody = GetComponent<Rigidbody2D>();
currentHealth = 2;//初始生命值
invincibleTimer = 0;
rbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//Time.deltaTime是计算机渲染一帧所需时间
float moveX = Input.GetAxisRaw("Horizontal");//控制水平移动方向A:-1,D:1
float moveY = Input.GetAxisRaw("Vertical");//控制水平移动方向w:1,s:-1
Vector2 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 update
void Start()
{
rbody = GetComponent<Rigidbody2D>();//获取刚体
moveDirection = isVertical ? Vector2.up : Vector2.right;//判断是否垂直
changeTimer = changeDirectionTime;
anim = GetComponent<Animator>();//获取anim
isFixed = false;
}
// Update is called once per frame
void 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 update
void Awake()
{
rbody = GetComponent<Rigidbody2D>();
Destroy(this.gameObject, 2f);
}
// Update is called once per frame
void 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 update
void Start()
{
}
// Update is called once per frame
void 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 update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
//检测到有PlayerController
PlayerController pc = collision.GetComponent<PlayerController>();
if (pc != null)
{
if (pc.MyCurrentHealth < pc.MyMaxHealth) {
pc.ChangeHealth(1);
Destroy(this.gameObject);
}
}
}
}
一分一毛也是情,支持一下行不行 ~~
转载:https://blog.csdn.net/weixin_43795683/article/details/109142594
查看评论