目录
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);
}
……
}