Unity仿作植物大战僵尸 学习笔记(一)

【【Unity开发植物大战僵尸】1. 豌豆射手】https://www.bilibili.com/video/BV13N4y1c7rG?vd_source=fe688d296a49c21a47090f9730a67ab4

此学习内容根据up主制作

导入资源

在创建好2D工程文件打开后,可以直接双击运行资源包运行在编辑器打开。

创建场景并进行布置

背景

添加背景图片并将摄像机的视角大小设置为300

创建豌豆射手

全选所有图片拖拽进场景unity会帮我们创建一个带有由这些图片组成的序列帧动画的物体。

可能出现的问题

1.在播放动画的过程中物体消失

这是由于图层渲染顺序的问题

解决方法

将Order in layer默认0设置为1 ,大于背景图层即可

我们可以通过调整sorting layer

2.我们会用同样的方法导入新的物体的时候会出现同样的问题

这时候我们便可以通过默认预设来解决,每一个组件都会默认应用我们的预设

看成功了

实现太阳花生产太阳功能

1.首先我们创建太阳花发光与不发光两个动画,还是直接拖拽进场景自动创建动画即可。

2.配置好太阳花的动画的切换

3.在代码逻辑上实现动画切换和太阳生成

在脚本中创建物体

创建好太阳的预制体,把预制体拖拽进组建字段,实现生成太阳。

总体代码一览

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

public class SunFlower : MonoBehaviour
{
    private Animator animator;
    
    [SerializeField]
    private float generateSunIntervalTime = 5f;
    private float countGenerateSunIntervalTime;


   
    
    [SerializeField]
    private string parameter;

    [SerializeField]
    private GameObject SunPrefab;
    // Start is called before the first frame update
    void Start()
    {
        
        animator = GetComponent<Animator>();
        parameter = EnumType.EnumToString(EnumType.AnimatorParameters.IsReady);

        countGenerateSunIntervalTime = generateSunIntervalTime;
    }

    // Update is called once per frame
    void Update()
    {
        if (animator.GetBool(parameter)== false)
        {
            countGenerateSunIntervalTime -= Time.deltaTime;
            if(countGenerateSunIntervalTime <= 0)
            {
                animator.SetBool(parameter, true);
                countGenerateSunIntervalTime = generateSunIntervalTime;
            }
        }
    }
    public void GenerateSun()
    {
        
        animator.SetBool(parameter, false);
        Instantiate(SunPrefab,transform.position + (Vector3)UnityEngine.Random.insideUnitCircle*50, Quaternion.identity);
          
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PeaBullet : MonoBehaviour
{
    [SerializeField]
    private float speed = 1f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position += Vector3.right*speed;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Peashooter : MonoBehaviour
{
    [SerializeField]
    private GameObject Bullet;
    [SerializeField]
    private float attackIntevalTime = 5f;
    private float countTime = 0;
    [SerializeField]
    private Vector3 bulletPosition;

    // Start is called before the first frame update
    void Start()
    {
        countTime = attackIntevalTime;
    }

    // Update is called once per frame
    void Update()
    {
        if (countTime > 0)
        {
            countTime -= Time.deltaTime;
        }
        else
        {
            Instantiate(Bullet, bulletPosition, Quaternion.identity, transform);
            countTime = attackIntevalTime;
        }
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sun : MonoBehaviour
{
    [SerializeField]
    private float destroyTime = 5f;
    private float countDestroyTime;

    
    // Start is called before the first frame update
    void Start()
    {
        countDestroyTime = destroyTime;
    }

    // Update is called once per frame
    void Update()
    {
        countDestroyTime -= Time.deltaTime;
        if (countDestroyTime <= 0)
        {
            Destroy(gameObject);
        }
    }
}

对应类名挂载到对应物体上。

效果一览

这是我第一次写文章,发现有很多的问题,当然代码写得也不是很好,有很多预期效果没有实现好。谢谢你的阅读

在后面的笔记中我们详细记录我遇到学习问题,不会过多的记录,毕竟视频做得已经很好了,可以查漏补缺。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值