我一直在搜索整个Google,但是找不到任何解决方案。基本上,我要寻找的是我有一个立方体对象和一个箭头对象,我想通过用鼠标拖动箭头对象来调整立方体对象的大小。
我已经尝试了几乎所有可以找到的部分完成的解决方案,但无济于事。
private GameObject mainObject;
private float positionZ = 5.0f;
public float sizingFactor = 0.03f;
private float startSize;
private float startX;
private float startY;
private float startZ;
// Use this for initialization
void Start()
{
mainObject = GameObject.Find("Cube");
}
// Use this when user has clicked on object and still holding it
void OnMouseDrag()
{
//When user has left clicked
if (Input.GetMouseButton(0))
{
//Move the blue box by mouse
Vector3 positionRight = mainObject.transform.position;
Vector3 mousePosition = new Vector3(Input.mousePosition.x,
Input.mousePosition.y, Input.mousePosition.z);
startX = mousePosition.x;
startY = mousePosition.y;
startZ = mousePosition.z;
Vector3 objPosition =
Camera.main.ScreenToWorldPoint(mousePosition);
//Set the blue box by x axis of mouse and y,z axises of empty-
object right one
transform.position = new Vector3(objPosition.x, objPosition.y,
enter code hereobjPosition.z);
startSize = mainObject.transform.localScale.x;
}
if (Input.GetMouseButton(0))
{
//Scale green box by blue box
Vector3 scale = mainObject.transform.localScale;
scale.x = startSize + (Input.mousePosition.x - startX) *
sizingFactor;
Debug.Log(scale.x);
scale.y = startSize + (Input.mousePosition.y - startY) *
sizingFactor;
scale.z = startSize + (Input.mousePosition.z - startZ) *
sizingFactor;
Debug.Log(scale.z);
mainObject.transform.localScale = scale;
}
}
void OnMouseUp()
{
}
这是我发现并尝试修改的内容,但无济于事。 任何帮助将不胜感激。
编辑:
Vector2 prevMousePosition;
private GameObject mainObject;
public float sizingFactor = 0.03f;
Vector3 minimumScale;
private void Start()
{
mainObject = GameObject.Find("Cube");
minimumScale = new Vector3(1.0f, 1.0f, 1.0f);
}
void OnMouseDrag()
{
Vector2 mousePosition = Input.mousePosition;
if (Input.GetMouseButton(0))
{
transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
Vector3 scale = mainObject.transform.localScale;
scale.x = scale.x + (mousePosition.x - prevMousePosition.x) * sizingFactor;
scale.y = scale.x;
scale.z = scale.x;
mainObject.transform.localScale = scale;
if (scale.x < minimumScale.x)
{
mainObject.transform.localScale = minimumScale;
}
}
prevMousePosition = mousePosition;
}
这现在将适用于所有轴。但是,似乎有一个错误导致多维数据集在所有3个轴上最多缩放10个。
代码在以下位置时:scale.x-(mousePositon.x-prevMousePosition.x)* sizingFactor
该多维数据集可以正常工作并从1.0f开始缩放。
答案 0 :(得分:0)
我认为您走在正确的道路上-您只是稍微误解了更新循环。在一个OnMouseDrag回调中,您的代码看起来像是试图查看鼠标位置之间的差异,但是它们的值相同:
startX = mousePosition.x;
// [...]
scale.x = startSize + (Input.mousePosition.x - startX) * sizingFactor;
这将产生startSize + (x - x) * y
,与startSize + 0
相同,而startSize
当然与OnMouseDrag()
相同,这意味着没有变化。
您需要区分帧之间的mousePosition ,即Vector2 prevMousePosition;
void OnMouseDrag()
{
Vector2 mousePosition = Input.mousePosition;
if (Input.GetMouseButton(0))
{
transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
Vector3 scale = mainObject.transform.localScale;
scale.x = scale.x * (mousePosition.x - prevMousePosition.x) * sizingFactor;
scale.y = scale.y * (mousePosition.y - prevMousePosition.y) * sizingFactor;
mainObject.transform.localScale = scale;
}
prevMousePosition = mousePosition;
}
回调之间的区别:
{{1}}
因此,基本上,这段代码可以完成2件事:
在您的代码中,您还试图管理鼠标何时在z轴上移动。我不知道您打算如何在二维平面上的三维空间中移动鼠标-所以我删除了:)
我也借此机会使代码尽可能简单易读。如果我删除了需要的内容,我深表歉意,但是我认为大多数代码行都已过时。
我还没有尝试过此代码,请告诉我它是否未按预期进行编译或工作。