内容为学习siki学院课程后做的笔记,感兴趣可以查看原课程。
水体是由一个个水滴构成的,所以先实现一个水滴。
2D Object -> Sprites ->Circle创建一个2D circle作为一个水滴,调整到适合大小,修改颜色为蓝色。

添加三个Sorting Layers,用于控制显示的先后顺序,下面的优先级高,显示在前面,水滴的Sorting Layers改为Water。
添加Circle Collider 2D,Rigidbody 2D,Trail Renderer三个组件。

Circle Collider 2D的碰撞体范围改小一些,使得多个水滴接触时有相溶的感觉。


Rigidbody 2D主要是添加重力效果,新建一个2D物理材质添加到这个Rigidbody 2D的材质上,修改弹性为0.2,使得它落到障碍物上时有一点弹性。

Trail Renderer用于实现水滴掉落时拖尾的效果,修改颜色和水滴一致,调整拖尾线的宽度,前面粗后面细,Time是拖尾存在时间,改短一点即可,材质使用默认材质。

将这个水滴保存为prefab。

弄一个简单场景,上面水龙头用来指示出水的地方,下面的Square用做障碍物,添加Box Collider 2D,Sorting Layers改为Block,新建如下脚本挂在相机上。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class WaterSimulation : MonoBehaviour
{
//水龙头
public GameObject tap;
//水滴
public GameObject water;
public int waterNum;
private Vector3 _tapPos;
private WaitForSeconds _waitForSeconds = new WaitForSeconds(0.02f);
void Start()
{
_tapPos = tap.transform.position;
}
private void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 50),"生成水"))
{
StartCoroutine(SpawnWater());
}
}
private IEnumerator SpawnWater()
{
for (int i = 0; i < waterNum; ++i)
{
Vector3 pos = new Vector3(Random.Range(_tapPos.x - 0.03f, _tapPos.x - 0.03f),
_tapPos.y - 0.3f, 0);
Instantiate(water, pos, Quaternion.identity, transform);
yield return _waitForSeconds;
}
}
}

场景中添加一个杯子,Sorting Layers改为Glass,加上刚体和Edge Colloder 2D,刚体质量改大一些,防止被水冲走,边缘碰撞器由线段组成,可以自由调整成任何形状,首尾不需要相连。


在场景中添加一个圆和菱形,分别添加Circle Collider 2D和Polygon Collider 2D,都添加Rigidbody 2D,圆的mass设为50,菱形的mass设为1,质量大的不容易被推动。

新建两个Square和一个Circle,分别添加对应的Collider ,置于一个空物体下面,在根节点上添加Rigidbody 2D和Hinge Joint 2D,铰链的Anchor要对齐到圆中心。

铰链可以使两个有刚体的GameObject产生相互作用,发生碰撞时会有旋转的效果。

原理是在LineRenderer相邻点之间添加碰撞体,根节点上添加刚体,补充上面代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class WaterSimulation : MonoBehaviour
{
//水龙头
public GameObject tap;
//水滴
public GameObject water;
public int waterNum;
public LineRenderer lineRenderer;
private Vector3 _tapPos;
private WaitForSeconds _waitForSeconds = new WaitForSeconds(0.02f);
private Camera _camera;
private List<Vector2> _pointList = new List<Vector2>();
private HashSet<Vector2> _pointHashSet = new HashSet<Vector2>();
private bool _canDraw = true;
void Start()
{
_tapPos = tap.transform.position;
lineRenderer.positionCount = 0;
_camera = Camera.main;
}
private void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 100, 50),"生成水"))
{
StartCoroutine(SpawnWater());
}
}
private IEnumerator SpawnWater()
{
for (int i = 0; i < waterNum; ++i)
{
Vector3 pos = new Vector3(Random.Range(_tapPos.x - 0.03f, _tapPos.x - 0.03f), _tapPos.y - 0.3f, 0);
Instantiate(water, pos, Quaternion.identity, transform);
yield return _waitForSeconds;
}
}
private void Update()
{
if (Input.GetMouseButton(0) && _canDraw)
{
//鼠标的位置从屏幕空间转换到世界空间
Vector2 pos = _camera.ScreenToWorldPoint(Input.mousePosition);
if (!_pointHashSet.Contains(pos))
{
_pointHashSet.Add(pos);
_pointList.Add(pos);
//添加点
int pointCount = ++lineRenderer.positionCount;
lineRenderer.SetPosition(pointCount - 1, pos);
//在两点之间添加碰撞体
if (pointCount > 1)
{
Vector2 point1 = _pointList[pointCount - 2];
Vector2 point2 = _pointList[pointCount - 1];
GameObject go = new GameObject("Collider");
go.transform.parent = lineRenderer.transform;
//碰撞体中心在两点之间
go.transform.localPosition = (point1 + point2) / 2;
var boxCollider = go.AddComponent<BoxCollider2D>();
//碰撞体长度等于两点之间连线的长度,宽度等于线的宽度
boxCollider.size = new Vector2((point2 - point1).magnitude, lineRenderer.startWidth);
//碰撞体的右侧朝向线的方法
go.transform.right = (point2 - point1).normalized;
}
}
}
if (Input.GetMouseButtonUp(0))
{
_canDraw = false;
if (lineRenderer.gameObject.GetComponent<Rigidbody2D>() == null)
lineRenderer.gameObject.AddComponent<Rigidbody2D>();
}
}
}
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
?博客主页:https://xiaoy.blog.csdn.net?本文由呆呆敲代码的小Y原创,首发于CSDN??学习专栏推荐:Unity系统学习专栏?游戏制作专栏推荐:游戏制作?Unity实战100例专栏推荐:Unity实战100例教程?欢迎点赞?收藏⭐留言?如有错误敬请指正!?未来很长,值得我们全力奔赴更美好的生活✨------------------❤️分割线❤️-------------------------
本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01 客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02 数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit
目录1.AdmobSDK下载地址2.将下载好的unityPackagesdk导入到unity里编辑 3.解析依赖到项目中
Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u
假设我在Store的模型中有这个非常简单的方法:defgeocode_addressloc=Store.geocode(address)self.lat=loc.latself.lng=loc.lngend如果我想编写一些不受地理编码服务影响的测试脚本,这些脚本可能已关闭、有限制或取决于我的互联网连接,我该如何模拟地理编码服务?如果我可以将地理编码对象传递到该方法中,那将很容易,但我不知道在这种情况下该怎么做。谢谢!特里斯坦 最佳答案 使用内置模拟和stub的rspecs,你可以做这样的事情:setupdo@subject=MyCl
在ruby中,你可以这样做:classThingpublicdeff1puts"f1"endprivatedeff2puts"f2"endpublicdeff3puts"f3"endprivatedeff4puts"f4"endend现在f1和f3是公共(public)的,f2和f4是私有(private)的。内部发生了什么,允许您调用一个类方法,然后更改方法定义?我怎样才能实现相同的功能(表面上是创建我自己的java之类的注释)例如...classThingfundeff1puts"hey"endnotfundeff2puts"hey"endendfun和notfun将更改以下函数定
我有一个gem,它有一个根据Rails.env的不同行为的方法:defself.envifdefined?(Rails)Rails.envelsif...现在我想编写一个规范来测试这个代码路径。目前我是这样做的:Kernel.const_set(:Rails,nil)Rails.should_receive(:env).and_return('production')...没关系,只是感觉很丑。另一种方法是在spec_helper中声明:moduleRails;end而且效果也很好。但也许有更好的方法?理想情况下,这应该有效:rails=double('Rails')rails.sho
我有一个rspec模拟对象,一个值赋给了属性。我正在努力在我的rspec测试中满足这种期望。只是想知道语法是什么?代码:defcreate@new_campaign=AdCampaign.new(params[:new_campaign])@new_campaign.creationDate="#{Time.now.year}/#{Time.now.mon}/#{Time.now.day}"if@new_campaign.saveflash[:status]="Success"elseflash[:status]="Failed"endend测试it"shouldabletocreat
我正在尝试测试命令行工具的输出。如何使用rspec来“伪造”命令行调用?执行以下操作不起作用:it"shouldcallthecommandlineandreturn'text'"do@p=Pig.new@p.should_receive(:run).with('my_command_line_tool_call').and_return('resulttext')end如何创建stub? 最佳答案 使用newmessageexpectationsyntax:规范/虚拟规范.rbrequire"dummy"describeDummy