gpt4 book ai didi

c# - 在 Unity 中通过鼠标拖动创建预制件

转载 作者:太空宇宙 更新时间:2023-11-03 18:56:21 27 4
gpt4 key购买 nike

当用户单击并按住鼠标按钮(拖动鼠标)时,我想使用鼠标位置创建一个预制件 - (类似于油漆中的画笔绘图 - 除了用预制件更改画笔标记)。

当鼠标缓慢移动时,效果很好。我遇到的问题是,如果用户移动鼠标太快,“更新”功能不会记录所有鼠标位置,并且每个预制件的位置都离前一个预制件很远。

enter image description here

初始代码:

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();
}
}

最佳答案

既然你已经注意到,你的鼠标速度是可变的,而更新率不是(它可以是可变的,也可以是固定的。无论如何,它与光标的移动无关),这意味着你的鼠标光标可以在两个更新调用之间移动相当远的距离。

该问题最简单的解决方案是遍历最后已知的鼠标位置与当前位置之间的距离,并填充所有可用空间。

我还没有为你彻底解决问题;有改进的余地,我现在无法真正测试它,但这应该会让您朝着正确的方向前进。

public class Draw : MonoBehaviour
{
public GameObject Brush;

private bool _isDraging = false;

//we are going to keep track of the location where we last placed an object.
private Vector3 _lastPlacedObjectLocation;

private Vector3 _get2dMousePosition()
{
Vector3 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
newPos.z = 0;
return newPos;
}

void Update()
{
if (Input.GetMouseButtonDown(0))
{
_isDraging = true;
}

if (Input.GetMouseButtonUp(0))
{
_isDraging = false;

//keep 'old' mouse position up to date, so we do not draw in between dragging the mouse and releasing it.
_lastPlacedObjectLocation = _get2dMousePosition();
return;
}

if (_isDraging)
{
Vector3 currentMousePosition = _get2dMousePosition();

//now, we find how far the mouse has moved since the last step.
Vector3 distanceTravelled = _lastPlacedObjectLocation - currentMousePosition;

//since we know the starting and ending point, all we have to do now is add the prefab instances, neatly spaced over the travelled distance.
Vector3 stepDistance = distanceTravelled / spacing;

//then, we interpolate between the start and end vector
//you could make this a lot faster by determining how often your object fits in this space, but i'd like to leave you a little bit of a challenge, so i'm abusing the magnitude of the stepDistance ;)
for(float i =0; i < stepDistance.magnitude; i+=0.1f)
{
float progress = i / stepDistance.magnitude;
Vector3 placementPosition = Vector3.Lerp(_lastPlacedObjectLocation, currentMousePosition, progress);
placePrefab(placementPosition);
}



//and then we update the last-placed-object location
_lastPlacedObjectLocation = _get2dMousePosition();
}
}

void placePrefab(Vector3 location)
{
//I have added 'Physics.OverlapBox' so that the prefab doesn't overlap with the previous prefab
var collide = Physics.OverlapBox(location, new Vector3(0, 0, 0));

if (!collide.Any())
{
Debug.Log("Draw: " + location);
Instantiate(Brush, location, Quaternion.identity);
}
}
}

关于c# - 在 Unity 中通过鼠标拖动创建预制件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44452780/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com