本案例素材和教程都来自Siki学院,十分感谢教程中的老师
本文仅作学习笔记分享交流,不作任何商业用途
预制体
在这个小案例中,水可以做成圆形但是带碰撞体,碰撞体比图形小一圈,顺便加上Trail renderer组件
材质
将碰撞材质的friction为0,bonciness可以按照需要修改
脚本
生成水珠的脚本并不难, 只是需要创建大量实例化对象的时候,加一个携程函数不卡一些(事实上,水的预制体如果太小的话,还是会卡,所以可以对携程函数进一步的优化)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AboutWater : MonoBehaviour
{
public GameObject water;
public int waterNumber;
// Start is called before the first frame update
void Start()
{
this.GetComponent<AudioSource>().Play();
StartCoroutine(CreatWater());
}
// Update is called once per frame
void Update()
{
}
private IEnumerator CreatWater()
{
for (int i = 0; i < waterNumber; i++)
{
Vector3 pos = new Vector3(Random.Range(transform.position.x-0.02f,
transform.position.x + 0.02f),transform.position.y-0.3f,0);
Instantiate(water,pos,Quaternion.identity,transform);
yield return new WaitForSeconds(0.02f);
}
}
}
标签:HappyGlass,0.02,碰撞,transform,2D,Unity,position,using,public
From: https://blog.csdn.net/2301_77947509/article/details/141829944