Scaling an Object with mouse
Hi so I'm not quite sure what to do, I'm trying to make an object scale while another object is being dragged by the mouse.
Here's my snippet:
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, objPosition.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()
{
}
But when I test it, nothing happens. Any help would be kindly appreciated!
Answer by Deadshot1994 · Jul 05, 2019 at 10:28 AM
I've successfully made the code to work and scale the object on all 3 axis by parsing the x axis as value.
Vector2 prevMousePosition;
private GameObject mainObject;
public float sizingFactor = 0.03f;
Vector3 minimumScale;
private void Start()
{
// Parsing the object we want to modify to mainObject
mainObject = GameObject.Find("Cube");
// Setting the minimum scale of the mainObject
minimumScale = new Vector3(1.0f, 1.0f, 1.0f);
}
void OnMouseDrag()
{
Vector2 mousePosition = Input.mousePosition;
if (Input.GetMouseButton(0))
{
// Set this object's position to where the mouse is being held down by the left click.
transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
// Change the scale of mainObject by comparing previous frame mousePosition with this frame's position, modified by sizingFactor.
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;
// Checking if current scale is less than minimumScale, if yes, mainObject scales takes value from minimumScale
if (scale.x < minimumScale.x)
{
mainObject.transform.localScale = minimumScale;
}
}
prevMousePosition = mousePosition;
}
}
This now works, however there is still one problem, which is that the object immediately scales to a value of 10 on all axes, instead of waiting to scale when the dragging of the mouse begins.
Your answer
Follow this Question
Related Questions
Get Velocity of Rigidbody Object Below Another Object 0 Answers
How do I pass in a GameObject as a parameter when I'm instantiating a class? 0 Answers
Unintentional hidden Objects 0 Answers
How to resize terrain? 2 Answers
Characters are small in playmode 0 Answers