so basically i have a function that when called in update scales the passed parameter GameObject by clicking and dragging the mouse. the only problem is that its very jittery and bounces values. can someone tell me why? is there a better way to do this?
void Scale(GameObject o)
{
tempScale = o.transform.localScale;
tempScale = localScaleOnMouseUp + new Vector3 ((lastMousePosition.x - mousePositionWhenClicked.x) * Time.deltaTime * scaleSensitivity,
(lastMousePosition.y - mousePositionWhenClicked.x) * Time.deltaTime * scaleSensitivity,
1);
o.transform.localScale = tempScale;
if(Input.GetMouseButtonUp(0))
{
localScaleOnMouseUp = o.transform.localScale;
isScaling = false;
}
if(Input.GetMouseButtonDown(0))
{
mousePositionWhenClicked = Input.mousePosition;
}
lastMousePosition = Input.mousePosition;
}
You are using Time.deltaTime in your calculation. There is nothing here that needs to be frame rate independent. So take out your deltaTime calls and adjust your sensitivity (i.e. it will likely be 1/60 of its current value). You only want the size to depend on the delta of the mouse. You may in the long term want to make the code screen resolution independent, but that is not causing your jitter. Also is Scale() getting called every frame or is it conditionally called from Update()?
The use of deltaTIme is a good thing. It makes it frame-rate independent if this is being called from Update().
This code will give you more what you are looking for:
void Scale(GameObject o)
{
if (Input.GetMouseButtonDown(0)) {
lastMousePosition = Input.mousePosition;
}
if (Input.GetMouseButton(0)) {
o.transform.localScale += ((Input.mousePosition - lastMousePosition) * Time.deltaTime * scaleSensitivity);
lastMousePosition = Input.mousePosition;
}
}