- Home /
How to create an object and resize it with mouse drag???
Hey Everyone,
I know this question is asked many many so many times ;) .. Searching all day long unfortunatly I still couldn't find what I want..
on left click I want to create an object on mouse position and I want to rescale it only on x axis (for ex: if obj. is created on left click and resize its x axis with on mouse drag )
Hope someone understood what I what :)
(maybe)I have done Half of the work :
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour
{
public GameObject CubePrefab;
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
float positionZ = 10.0f;
Vector3 position = new Vector3 (Input.mousePosition.x, Input.mousePosition.y,positionZ);
Instantiate (CubePrefab, Camera.main.ScreenToWorldPoint (position),transform.rotation);
}}
this script is attached to main camera with a cubePrefab... this creates an object but now how can I rescale it???
Best.. .J
Answer by robertbu · Mar 01, 2014 at 10:27 PM
Here is a bit of code that expands on your code. Note that the object growth will not map to the growing size, and note that, since it depends on screen coordinates, you will likely have to do something to recalculate 'sizingFactor' if you target multiple devices.
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
public GameObject cubePrefab;
public float sizingFactor = 0.02f;
private GameObject lastSpawn = null;
private float startSize;
private float startX;
void Update () {
if (Input.GetMouseButtonDown (0)) {
float positionZ = 10.0f;
Vector3 position = new Vector3 (Input.mousePosition.x, Input.mousePosition.y,positionZ);
startX = position.x;
position = Camera.main.ScreenToWorldPoint(position);
lastSpawn = Instantiate (cubePrefab, position,transform.rotation) as GameObject;
startSize = lastSpawn.transform.localScale.z;
}
if (Input.GetMouseButton (0)) {
Vector3 size = lastSpawn.transform.localScale;
size.x = startSize + (Input.mousePosition.x - startX) * sizingFactor;
lastSpawn.transform.localScale = size;
}
}
}
Thanks @robertbu .. :) thanks a alot ... this solved my problem .. Now the object expands from both side what should I add to expand it only in one side..
Thanks again ...
Anyone have a solution for transfor$$anonymous$$g an object from an anchor point (I$$anonymous$$ Top Left) and not the midpoint?
Just move the object half of the amount added to scale, in the opposite direction.
Answer by geomorillo · Dec 07, 2016 at 02:26 PM
thanks that formula really helped in a similar case resizing a quad with mouse drag nice size.x = startSize + (Input.mousePosition.x - startX) * sizingFactor;
Your answer
Follow this Question
Related Questions
How do I put a link to a website when I click with the mouse an object or word? 6 Answers
3d Object attach to mouse 1 Answer
How to pick up an object using left mouse button 2 Answers
Select/Drag/Drop Objects with a mouse 0 Answers
Resize / Scale, Drag, Move a Plane at Runtime by Mouse action 2 Answers