当用户点击并按住鼠标按钮(拖动鼠标)时,我想使用鼠标位置创建一个预制件 - (类似于绘画中的画笔绘图 - 除了使用预制件更改画笔标记)。
当鼠标移动缓慢时,这可以正常工作。我遇到的问题是,如果用户移动鼠标太快,“更新”和“功能不会记录所有鼠标位置,每个预制件远离前一个预制件。
初始代码:
public class Draw : MonoBehaviour
{
public GameObject Brush;
private bool _isDraging = false;
private Vector3 _mousePos;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
_isDraging = true;
}
if (Input.GetMouseButtonUp(0))
{
_isDraging = false;
return;
}
if (_isDraging)
{
_mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
_mousePos.z = 0;
//I have added 'Physics.OverlapBox' so that the prefab doesn't overlap with the previous prefab
var collide = Physics.OverlapBox(_mousePos, new Vector3(0, 0, 0));
if (!collide.Any())
{
Debug.Log("Draw: " + _mousePos);
Instantiate(Brush, _mousePos, Quaternion.identity);
}
}
}
}
更新
void Update()
{
//I use this to set the initial mouse position
if (Input.GetMouseButtonDown(0))
{
_lastPlacedObjectLocation = _get2dMousePosition();
//_isDraging is now removed
}
if (Input.GetMouseButton(0))
{
var currentMousePosition = _get2dMousePosition();
var distanceTravelled = _lastPlacedObjectLocation - currentMousePosition;
var stepDistance = distanceTravelled.magnitude;
//I kept 1f - this is the distance between the prefabs - also my prefab size. Not sure if this is the best approach (the only way I could get it to work)
for (float i = 0; i < stepDistance; i += 1f)
{
float progress = i / stepDistance;
var placementPosition = Vector3.Lerp(_lastPlacedObjectLocation, currentMousePosition, progress);
//Removed Physics.OverlapBox - in some places we want an overlap otherwise there is a gap between the prefabs
Instantiate(Brush, placementPosition, Quaternion.identity);
}
_lastPlacedObjectLocation = _get2dMousePosition();
}
}
答案 0 :(得分:5)
因为您已经注意到,您的鼠标速度是可变的,并且更新速率不是(它可以是可变的,或者是固定的。无论如何,它都没有链接到光标的移动),这意味着您的鼠标光标可以在两次更新通话之间移动相当长的距离。
解决问题的最简单方法是遍历最后一个已知鼠标位置与当前鼠标位置之间的距离,并填写所有可用空间。
我还没有完全解决你的问题;还有改进的余地,我现在无法真正测试,但这应该让你朝着正确的方向前进。
$(function() {
if (window.isChinese === 'false') {
// Popup Modal using Bootstrap.
}
});
答案 1 :(得分:1)
您可以使用LineRenderer并将顶点位置放置在当前实例化预制件的位置,而不是像这样实例化预制件。
在LineRenderer的材质上,您可以调整x / y平铺以实现不断的图案重复。
此解决方案的缺点: