using UnityEngine;
using System.Collections;
public class DragAndDrop : MonoBehaviour
{
private bool _mouseState;
private GameObject target;
private float counter = 1f;
public Vector3 screenSpace;
public Vector3 offset;
// Use this for initialization
void Start()
{
}
void OnMouseDrag()
{
}
// Update is called once per frame
void Update()
{
// Debug.Log(_mouseState);
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
target = GetClickedObject(out hitInfo);
if (target != null)
{
_mouseState = true;
screenSpace = Camera.main.WorldToScreenPoint(target.transform.position);
offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
}
}
if (Input.GetMouseButtonUp(0))
{
_mouseState = false;
}
if (_mouseState)
{
//keep track of the mouse position
var curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
//convert the screen mouse position to world point and adjust with offset
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
//update the position of the object in the world
target.transform.position = curPosition;
counter += 0.5f;
target.transform.localScale = new Vector3(counter, 1
, 1);
}
}
GameObject GetClickedObject(out RaycastHit hit)
{
GameObject target = null;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
target = hit.collider.gameObject;
}
return target;
}
}
What it does now when i press the mouse button the left mouse button down none stop it’s changing the object localScale on the x axis. But it’s changing the size to both sides so the mouse is in the middle.
-
What i want to do is that it will change the axis x size only to the right side. And not both sides.
-
Second thing is i want to make that only when i click down the mouse button and then drag the mouse it will change the localScale. Not to drag the object but to press none stop the button and drag the mouse to the right and then it will change the localScale.