欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

基于Unity的2d動(dòng)畫游戲-------------------c#開發(fā) - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 技術(shù)分享 >

基于Unity的2d動(dòng)畫游戲-------------------c#開發(fā)

發(fā)表時(shí)間:2020-10-19

發(fā)布人:葵宇科技

瀏覽次數(shù):92

基于unity的2d動(dòng)畫制造----基于c#說話開辟,類似于《DNF》的2d界面,今朝只有一個(gè)游戲場(chǎng)景。結(jié)不雅圖UI如下圖所示在這里插入圖片描述

游戲結(jié)不雅視頻已經(jīng)上傳B站:

2dAnimation游戲

游戲開辟重要步調(diào):

1.素材收集(來自Unity的Asset Store)

2.UI設(shè)計(jì)(隨心所欲)

3.剛體碰撞規(guī)矩等(csharp代碼)

Hierarchy的┞符體構(gòu)造如下:每個(gè)物體的名稱和它的英文名大年夜概一致。

在這里插入圖片描述

腳本構(gòu)造如下:在這里插入圖片描述

部分PlayerController的代碼如下:

`//author:劉家誠(chéng):date:2020.10.16

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:劉家誠(chéng):date:2020.10.16
public class PlayerController : MonoBehaviour
{
    //控制角色的移動(dòng),生命,動(dòng)畫等
    public float speed = 5f;//移動(dòng)速度

    private int maxHealth = 5;//最大年夜生命值

    private int currentHealth;//當(dāng)前生命值

    private float invicibleTime = 2f;//無敵時(shí)光

    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是計(jì)算機(jī)襯著一幀所需時(shí)光
        float moveX = Input.GetAxisRaw("Horizontal");//控制程度移動(dòng)偏向A:-1,D:1
        float moveY = Input.GetAxisRaw("Vertical");//控制程度移動(dòng)偏向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);//根據(jù)向量大年夜小來斷定
       


        //=========移動(dòng)==========
        Vector2 position = rbody.position;
        //position.x += moveX * speed * Time.deltaTime;
        // position.y += moveY * speed * Time.deltaTime;
        position += moveVector * speed * Time.deltaTime;
        rbody.MovePosition(position);//實(shí)現(xiàn)剛體的移動(dòng)
        //==========無敵記時(shí)==========
        if (isInvincible)
        {
            invincibleTimer -= Time.deltaTime;
            if (invincibleTimer < 0)
            {
                isInvincible = false;//記錄時(shí)光,2s后無敵時(shí)光消掉
            }
        }
        //========按下j鍵進(jìn)行進(jìn)擊=========
        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);
    }
    //與玩家的碰撞檢測(cè)
    
}

```csharp
在這里插入代碼片

EnemyController的部分代碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:劉家誠(chéng):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;//移動(dòng)偏向

    private Animator anim;

    private bool isFixed;//是否被修復(fù)


    // 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()
    {
        //被修復(fù),不履行以下所有代碼
        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");//播放被修復(fù)的動(dòng)畫
    }
}

BulletController的代碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:劉家誠(chéng):date:2020.10.16
/// <summary>
/// 控制槍彈的移動(dòng)
/// </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);
    }
    //=====碰撞檢測(cè)=====
    private void OnCollisionEnter2D(Collision2D collision)
    {
        EnemyController ec = collision.gameObject.GetComponent<EnemyController>();
        if (ec != null)
        {
            Debug.Log("碰著仇敵了");
            ec.Fixed();//修復(fù)仇敵

        }
        Destroy(this.gameObject);
    }

    
}

Dangerous的代碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//author:劉家誠(chéng):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:劉家誠(chéng):date:2020.10.16
public class CollectAble : MonoBehaviour
{
    //草莓被玩家碰撞時(shí)檢測(cè)的相干類
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called>void Update()
    {
        
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        //檢測(cè)到有PlayerController
        PlayerController pc = collision.GetComponent<PlayerController>();
        if (pc != null)
        {
            if (pc.MyCurrentHealth < pc.MyMaxHealth) {
                pc.ChangeHealth(1);
                Destroy(this.gameObject);
            }
            

        }
    }
}

相關(guān)案例查看更多