【Unity植物大战僵尸】向日葵动画、天空中阳光的生成与掉落(二)

目录

3、创建动画目录并生成向日葵动画

4、生成阳光动画

5、管理阳光

6、阳光下落

测试


3、创建动画目录并生成向日葵动画

将预先准备好的向日葵精灵放到场景中,会默认保存为动画

新建Plant图层并设置

测试

4、生成阳光动画

按照第三节的操作同样操作

5、管理阳光

并将阳光保存为预制体

在SkySunManager.cs中添加代码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkySunManager : MonoBehaviour
{
    // 阳光预制体
    private GameObject prefabSun;
    
    // 创建阳光时候的Y坐标
    private float createSunPosY;
    // 创建阳光时最左和最右的X轴坐标,在这个范围内随机
    private float createSunMaxPosX = 3f;
    private float createSunMinPosX = -7.5f;
    
    // 阳光下落时最大和最小的Y轴坐标,在这个范围内随机
    private float sunDownMaxPosY = 2.5f;
    private float sunDownMinPosY = -3.7f;
    private void Start()
    {
        prefabSun = Resources.Load<GameObject>("Prefabs/Sun");
        InvokeRepeating("CreateSun", 3,3);
    }
    void CreateSun()
    {
        GameObject.Instantiate(prefabSun, Vector3.zero, Quaternion.identity);
    }
}

有一个阳光生成的控制的脚本了,那么如何控制阳光下落,就得再新建一个脚本,绑定在阳光上

在Sun.cs中添加代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sun : MonoBehaviour
{
    // 下落的目标点Y
    private float downTargetPosY;
    public void InitForSky(float downTargetPosY, float createPosX, float createPosY)
    {
        this.downTargetPosY = downTargetPosY;
        transform.position = new Vector3(createPosX, createPosY, 0);
    }
    
}

在SkySunManager.cs中更新代码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class SkySunManager : MonoBehaviour
{
    ……
    void CreateSun()
    {
        // 实例化后并获得Sun脚本对象
        Sun sun = GameObject.Instantiate(prefabSun, Vector3.zero, Quaternion.identity).GetComponent<Sun>();
        // 下落点Y和开始点X的随机
        float donwY = Random.Range(sunDownMaxPosY, sunDownMinPosY);
        float createX = Random.Range(createSunMinPosX, createSunMaxPosX);
        sun.InitForSky(donwY, createX, createSunPosY);
    }
}

6、阳光下落

在Sun.cs中更新代码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sun : MonoBehaviour
{
    // 下落的目标点Y
    private float downTargetPosY;
    private void Update()
    {
        if (transform.position.y <= downTargetPosY)
        {
            Invoke("DestroySun",5);
            return;
        }
        transform.Translate(Vector3.down * Time.deltaTime);
    }
    private void DestroySun()
    {
        Destroy(gameObject);
    }
    
    ……
   
}

测试

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~Lomiss~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值