基于Unity的2d动画游戏-------------------c#开发 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

基于Unity的2d动画游戏-------------------c#开发

发表时间:2020-10-19

发布人:葵宇科技

浏览次数:66

基于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>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>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>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>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>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);
            }
            

        }
    }
}

相关案例查看更多