基于unity的2d动画制作----基于c#语言开发

基于unity的2d动画制作----基于c#语言开发,类似于《冒险岛》,只有一个游戏场景。成果图UI如图1所示。游戏成果视频已经上传B站:https://www.bilibili.com/video/BV1Cr4y1c75W

2dAnimation

在这里插入图片描述

                                                            图1

素材来源:Unity的Asset Store,Asset Store里包含许多开源库。

主要game的对象如右图所示:在这里插入图片描述
主要用到的脚本有:在这里插入图片描述

player的脚本主要代码如下:`

//author:刘家诚, last time:2020.10.15
private float x;
private float y;
public float speed = 5;
private Rigidbody2D  _rigidbody_2D;
private Animator _animator;
// Start is called before the first frame update
void Start()
{
    //2d的组件
    _rigidbody_2D = GetComponent<Rigidbody2D>();
    _animator = GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{
    x = Input.GetAxis("Horizontal");
    y = Input.GetAxis("Vertical");
    //正方向行走
    if(x > 0)
    {
        _rigidbody_2D.transform.eulerAngles = new Vector3(0,0,0);
        _animator.SetBool("run", true);

    }
    //反方向行走
    if(x < 0)
    {
        _rigidbody_2D.transform.eulerAngles = new Vector3(0, 180, 0);
        _animator.SetBool("run", true);
    }
    if(x < 0.001f && x > -0.001f)
    {
        _animator.SetBool("run", false);
    }
    Run();
}

private void Run()
{
    Vector3 movement = new Vector3(x, y, 0);
    _rigidbody_2D.transform.position += movement * speed * Time.deltaTime;
}
//player碰撞检测用player做,并且要设置tag
private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Spike")
    {
        //如果碰撞到障碍物,那么player消失即可。并且调用GameController的ShowGameOverPanel方法

        GameController.Instance.ShowGameOverPanel();
        Destroy(gameObject);
    }
}

`
项目网盘链接地址,之后会传到github上:链接:https://pan.baidu.com/s/1lxSTiGQdZ-0Ji6B0z2XMMw
提取码:jc60
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V3的分享

猜你喜欢

转载自blog.csdn.net/weixin_43795683/article/details/109103230
今日推荐