- Home /
Question by
grzegorzperun · Nov 18, 2018 at 05:42 PM ·
2dscaleresizequadempty game object
Resizing only one side of GameObject with mouse drag
Hello! I'm stuck in a problem. I have a Quad, I am making it resizable by mouse drag. So far it works good, but it resizes two sides, that's why I want to make it scaling only one side. And this is where the problem begins...
I try to use the empty GameObject method mentioned here (method 2) and somewhere else... But in my case, it just doesn't work. It doesn't do anything.
Can anybody help me?
My code
// If attached to normal Quad, resizes two sides
// If attached to empty GO, doesn't do anything
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class ResizeObject : MonoBehaviour
{
public GameObject gameObject;
Vector3 fixedMousePosition;
bool canBeResized;
float rightBorder, leftBorder;
void Update()
{
fixedMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
rightBorder = gameObject.transform.position.x + (gameObject.transform.localScale.x / 2);
leftBorder = gameObject.transform.position.x - (gameObject.transform.localScale.x / 2);
}
void OnMouseDown()
{
if (fixedMousePosition.x >= rightBorder - 0.1 && fixedMousePosition.x <= rightBorder)
{
canBeResized = true;
}
}
void OnMouseDrag()
{
if (canBeResized)
{
if (Input.GetAxis("Mouse X") > 0)
{
if (fixedMousePosition.x > rightBorder)
{
gameObject.transform.localScale = new Vector3(fixedMousePosition.x - leftBorder, 1, 1);
}
}
}
}
void OnMouseUp()
{
if (canBeResized)
{
canBeResized = false;
}
}
}
Scene structure (script is attached to GameObject, which is of course positioned)
przechwytywanie.png
(3.7 kB)
Comment