Не совсем уверен в точном расчете, но в OnMouseDown
дополнительно сохраните текущую шкалу, как
private Vector3 startScale;
private void OnMouseDown()
{
dragStartingPoint = Input.mousePosition;
startScale = transform.localScale;
}
, а затем умножьте на это значение при масштабировании, используя Vector3.Scale
// No need to get the current localScale here as this is a scale factor now
temp = new Vector3(amountDragged / 100f, amountDragged / 100f, 1);
// Vector3.Scale multiplies both vectors component wise
transform.localScale = Vector3.Scale(temp, startScale);
// Basically the same as
//transform.localScale = new Vector3(temp.x * startScale.x, temp.y * startScale.y, temp.z * startScale.z);
В общем, вы go немного избыточны здесь. Я бы вообще не использовал Update
, а просто выполнял бы всю работу в OnMouseDrag
, не используя флаг dragging
, так как amyway
OnMouseDrag
вызывается каждый кадр, когда мышь нажата.
, поэтому просто введите код здесь
// This gets called while dragging the mouse
private void OnMouseDrag()
{
currentMousePosition = Input.mousePosition;
//find the distance between the dragStartingPoint and currentMousePosition and store in amountDragged variable
amountDragged = Vector3.Distance(currentMousePosition, dragStartingPoint);
// set minimum and maximum drag amount and store in clampedAmountDragged variable
clampedAmountDragged = Mathf.Clamp(amountDragged, 100f, 300f);
// set amountDragged to clampedAmount to apply minimum and maximum
amountDragged = clampedAmountDragged;
var temp = new Vector3(amountDragged / 100f, amountDragged / 100f, 1);
//make scale of object equal to temp variable
transform.localScale = Vector3.Scale(temp, startScale);
}
Также обратите внимание, что в настоящее время это позволяет только масштабировать объект больше. Невозможно вернуть;)
Набрано на телефоне, но я надеюсь, что идея проясняется